pulsar/spec/app/range-spec.coffee
2013-01-30 13:49:10 -08:00

40 lines
2.2 KiB
CoffeeScript

Range = require 'range'
Point = require 'point'
describe "Range", ->
describe "constructor", ->
it "ensures that @start <= @end", ->
range1 = new Range(new Point(0, 1), new Point(0, 4))
expect(range1.start).toEqual(row: 0, column: 1)
range2 = new Range(new Point(1, 4), new Point(0, 1))
expect(range2.start).toEqual(row: 0, column: 1)
describe ".isEmpty()", ->
it "returns true if @start equals @end", ->
expect(new Range(new Point(1, 1), new Point(1, 1)).isEmpty()).toBeTruthy()
expect(new Range(new Point(1, 1), new Point(1, 2)).isEmpty()).toBeFalsy()
describe ".intersectsWith(otherRange)", ->
it "returns true if the ranges intersect or share an endpoint", ->
expect(new Range([1, 1], [2, 10]).intersectsWith(new Range([2, 1], [3, 10]))).toBeTruthy()
expect(new Range([2, 1], [3, 10]).intersectsWith(new Range([1, 1], [2, 10]))).toBeTruthy()
expect(new Range([2, 1], [3, 10]).intersectsWith(new Range([2, 5], [3, 1]))).toBeTruthy()
expect(new Range([2, 5], [3, 1]).intersectsWith(new Range([2, 1], [3, 10]))).toBeTruthy()
expect(new Range([2, 5], [3, 1]).intersectsWith(new Range([3, 1], [3, 10]))).toBeTruthy()
expect(new Range([3, 1], [3, 10]).intersectsWith(new Range([2, 5], [3, 1]))).toBeTruthy()
expect(new Range([2, 5], [3, 1]).intersectsWith(new Range([3, 2], [3, 10]))).toBeFalsy()
expect(new Range([3, 2], [3, 10]).intersectsWith(new Range([2, 5], [3, 1]))).toBeFalsy()
describe ".union(otherRange)", ->
it "returns the union of the two ranges", ->
expect(new Range([1, 1], [2, 10]).union(new Range([2, 1], [3, 10]))).toEqual [[1, 1], [3, 10]]
expect(new Range([2, 1], [3, 10]).union(new Range([1, 1], [2, 10]))).toEqual [[1, 1], [3, 10]]
expect(new Range([2, 1], [3, 10]).union(new Range([2, 5], [3, 1]))).toEqual [[2, 1], [3, 10]]
expect(new Range([2, 5], [3, 1]).union(new Range([2, 1], [3, 10]))).toEqual [[2, 1], [3, 10]]
describe ".translate(startPoint, endPoint)", ->
it "returns a range translates by the specified start and end points", ->
expect(new Range([1, 1], [2, 10]).translate([1])).toEqual [[2, 1], [3, 10]]
expect(new Range([1, 1], [2, 10]).translate([1,2], [3,4])).toEqual [[2, 3], [5, 14]]