|
| 1 | +var should = require("should"); |
| 2 | +var compareLocations = require("../lib/compareLocations"); |
| 3 | +var createLocation = function(overides) { |
| 4 | + return Object.assign({ |
| 5 | + line: 10, |
| 6 | + column: 5, |
| 7 | + index: 3 |
| 8 | + }, overides); |
| 9 | +}; |
| 10 | + |
| 11 | +describe('compareLocations', function() { |
| 12 | + describe('string location comparison', function() { |
| 13 | + it('returns -1 when the first string comes before the second string', function() { |
| 14 | + compareLocations('alpha', 'beta').should.be.exactly(-1); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns 1 when the first string comes after the second string', function() { |
| 18 | + compareLocations('beta', 'alpha').should.be.exactly(1); |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns 0 when the first string is the same as the second string', function() { |
| 22 | + compareLocations('charlie', 'charlie').should.be.exactly(0); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('object location comparison', function() { |
| 27 | + var a, b; |
| 28 | + |
| 29 | + describe('location line number', function() { |
| 30 | + beforeEach(function() { |
| 31 | + a = createLocation({ |
| 32 | + line: 10 |
| 33 | + }); |
| 34 | + b = createLocation({ |
| 35 | + line: 20 |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns -1 when the first location line number comes before the second location line number', function() { |
| 40 | + compareLocations(a, b).should.be.exactly(-1); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns 1 when the first location line number comes after the second location line number', function() { |
| 44 | + compareLocations(b, a).should.be.exactly(1); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('location column number', function() { |
| 49 | + beforeEach(function() { |
| 50 | + a = createLocation({ |
| 51 | + column: 10 |
| 52 | + }); |
| 53 | + b = createLocation({ |
| 54 | + column: 20 |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns -1 when the first location column number comes before the second location column number', function() { |
| 59 | + compareLocations(a, b).should.be.exactly(-1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns 1 when the first location column number comes after the second location column number', function() { |
| 63 | + compareLocations(b, a).should.be.exactly(1); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('location index number', function() { |
| 68 | + beforeEach(function() { |
| 69 | + a = createLocation({ |
| 70 | + index: 10 |
| 71 | + }); |
| 72 | + b = createLocation({ |
| 73 | + index: 20 |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('returns -1 when the first location index number comes before the second location index number', function() { |
| 78 | + compareLocations(a, b).should.be.exactly(-1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns 1 when the first location index number comes after the second location index number', function() { |
| 82 | + compareLocations(b, a).should.be.exactly(1); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('same location', function() { |
| 87 | + beforeEach(function() { |
| 88 | + a = createLocation(); |
| 89 | + b = createLocation(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns 0', function() { |
| 93 | + compareLocations(a, b).should.be.exactly(0); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('start location set', function() { |
| 98 | + beforeEach(function() { |
| 99 | + a = { |
| 100 | + start: createLocation({ |
| 101 | + line: 10 |
| 102 | + }) |
| 103 | + }; |
| 104 | + b = { |
| 105 | + start: createLocation({ |
| 106 | + line: 20 |
| 107 | + }) |
| 108 | + }; |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns -1 when the first location line number comes before the second location line number', function() { |
| 112 | + compareLocations(a, b).should.be.exactly(-1); |
| 113 | + }); |
| 114 | + |
| 115 | + it('returns 1 when the first location line number comes after the second location line number', function() { |
| 116 | + compareLocations(b, a).should.be.exactly(1); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('string and object location comparison', function() { |
| 122 | + it('returns 1 when the first parameter is a string and the second parameter is an object', function() { |
| 123 | + compareLocations('alpha', createLocation()).should.be.exactly(1); |
| 124 | + }); |
| 125 | + |
| 126 | + it('returns -1 when the first parameter is an object and the second parameter is a string', function() { |
| 127 | + compareLocations(createLocation(), 'alpha').should.be.exactly(-1); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('unknown location type comparison', function() { |
| 132 | + it('returns 0 when the first parameter is an object and the second parameter is a number', function() { |
| 133 | + compareLocations(createLocation(), 123).should.be.exactly(0); |
| 134 | + }); |
| 135 | + |
| 136 | + it('returns undefined when the first parameter is a number and the second parameter is an object', function() { |
| 137 | + should(compareLocations(123, createLocation())).be.undefined(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('returns 0 when the first parameter is a string and the second parameter is a number', function() { |
| 141 | + compareLocations('alpha', 123).should.be.exactly(0); |
| 142 | + }); |
| 143 | + |
| 144 | + it('returns undefined when the first parameter is a number and the second parameter is a string', function() { |
| 145 | + should(compareLocations(123, 'alpha')).be.undefined(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('returns undefined when both the first parameter and the second parameter is a number', function() { |
| 149 | + should(compareLocations(123, 456)).be.undefined(); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments