2017-01-18 00:17:54 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const should = require("should");
|
|
|
|
const compareLocations = require("../lib/compareLocations");
|
2017-01-24 14:55:39 +08:00
|
|
|
const createPosition = function(overides) {
|
2017-01-02 07:38:59 +08:00
|
|
|
return Object.assign({
|
|
|
|
line: 10,
|
2017-01-24 14:55:39 +08:00
|
|
|
column: 5
|
2017-01-02 07:38:59 +08:00
|
|
|
}, overides);
|
|
|
|
};
|
|
|
|
|
2017-01-24 14:55:39 +08:00
|
|
|
const createLocation = function(start, end, index) {
|
|
|
|
return {
|
|
|
|
start: createPosition(start),
|
|
|
|
end: createPosition(end),
|
|
|
|
index: index || 3
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("compareLocations", () => {
|
|
|
|
describe("string location comparison", () => {
|
|
|
|
it("returns -1 when the first string comes before the second string", () =>
|
|
|
|
compareLocations("alpha", "beta").should.be.exactly(-1));
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns 1 when the first string comes after the second string", () =>
|
|
|
|
compareLocations("beta", "alpha").should.be.exactly(1));
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns 0 when the first string is the same as the second string", () =>
|
|
|
|
compareLocations("charlie", "charlie").should.be.exactly(0));
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("object location comparison", () => {
|
|
|
|
let a, b;
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("location line number", () => {
|
|
|
|
beforeEach(() => {
|
2017-01-02 07:38:59 +08:00
|
|
|
a = createLocation({
|
|
|
|
line: 10
|
|
|
|
});
|
|
|
|
b = createLocation({
|
|
|
|
line: 20
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-24 14:55:39 +08:00
|
|
|
it("returns -1 when the first location line number comes before the second location line number", () => {
|
2017-11-15 21:08:11 +08:00
|
|
|
return compareLocations(a, b).should.be.exactly(-1);
|
2017-01-24 14:55:39 +08:00
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns 1 when the first location line number comes after the second location line number", () =>
|
|
|
|
compareLocations(b, a).should.be.exactly(1));
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("location column number", () => {
|
|
|
|
beforeEach(() => {
|
2017-01-02 07:38:59 +08:00
|
|
|
a = createLocation({
|
|
|
|
column: 10
|
|
|
|
});
|
|
|
|
b = createLocation({
|
|
|
|
column: 20
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns -1 when the first location column number comes before the second location column number", () =>
|
|
|
|
compareLocations(a, b).should.be.exactly(-1));
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns 1 when the first location column number comes after the second location column number", () =>
|
|
|
|
compareLocations(b, a).should.be.exactly(1));
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("location index number", () => {
|
|
|
|
beforeEach(() => {
|
2017-01-24 14:55:39 +08:00
|
|
|
a = createLocation(null, null, 10);
|
|
|
|
b = createLocation(null, null, 20);
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns -1 when the first location index number comes before the second location index number", () =>
|
|
|
|
compareLocations(a, b).should.be.exactly(-1));
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns 1 when the first location index number comes after the second location index number", () =>
|
|
|
|
compareLocations(b, a).should.be.exactly(1));
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("same location", () => {
|
|
|
|
beforeEach(() => {
|
2017-01-02 07:38:59 +08:00
|
|
|
a = createLocation();
|
|
|
|
b = createLocation();
|
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns 0", () => {
|
2017-01-02 07:38:59 +08:00
|
|
|
compareLocations(a, b).should.be.exactly(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("string and object location comparison", () => {
|
|
|
|
it("returns 1 when the first parameter is a string and the second parameter is an object", () =>
|
|
|
|
compareLocations("alpha", createLocation()).should.be.exactly(1));
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns -1 when the first parameter is an object and the second parameter is a string", () =>
|
|
|
|
compareLocations(createLocation(), "alpha").should.be.exactly(-1));
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("unknown location type comparison", () => {
|
|
|
|
it("returns 0 when the first parameter is an object and the second parameter is a number", () =>
|
|
|
|
compareLocations(createLocation(), 123).should.be.exactly(0));
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns undefined when the first parameter is a number and the second parameter is an object", () =>
|
|
|
|
should(compareLocations(123, createLocation())).be.undefined());
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns 0 when the first parameter is a string and the second parameter is a number", () =>
|
|
|
|
compareLocations("alpha", 123).should.be.exactly(0));
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
it("returns undefined when the first parameter is a number and the second parameter is a string", () =>
|
|
|
|
should(compareLocations(123, "alpha")).be.undefined());
|
|
|
|
|
|
|
|
it("returns undefined when both the first parameter and the second parameter is a number", () =>
|
|
|
|
should(compareLocations(123, 456)).be.undefined());
|
2017-01-02 07:38:59 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
});
|