2017-01-18 00:17:54 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const compareLocations = require("../lib/compareLocations");
|
2018-03-08 04:56:06 +08:00
|
|
|
const createPosition = overrides => {
|
2018-02-25 18:46:17 +08:00
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
line: 10,
|
|
|
|
column: 5
|
|
|
|
},
|
2018-02-26 10:38:19 +08:00
|
|
|
overrides
|
2018-02-25 18:46:17 +08:00
|
|
|
);
|
2017-01-02 07:38:59 +08:00
|
|
|
};
|
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
const createLocation = (start, end, index) => {
|
2017-01-24 14:55:39 +08:00
|
|
|
return {
|
|
|
|
start: createPosition(start),
|
|
|
|
end: createPosition(end),
|
|
|
|
index: index || 3
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("compareLocations", () => {
|
|
|
|
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", () => {
|
2018-01-24 20:17:21 +08:00
|
|
|
expect(compareLocations(a, b)).toBe(-1);
|
2017-01-24 14:55:39 +08:00
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns 1 when the first location line number comes after the second location line number", () => {
|
|
|
|
expect(compareLocations(b, a)).toBe(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
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns -1 when the first location column number comes before the second location column number", () => {
|
|
|
|
expect(compareLocations(a, b)).toBe(-1);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns 1 when the first location column number comes after the second location column number", () => {
|
|
|
|
expect(compareLocations(b, a)).toBe(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
|
|
|
});
|
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns -1 when the first location index number comes before the second location index number", () => {
|
|
|
|
expect(compareLocations(a, b)).toBe(-1);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns 1 when the first location index number comes after the second location index number", () => {
|
|
|
|
expect(compareLocations(b, a)).toBe(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", () => {
|
2018-01-24 20:17:21 +08:00
|
|
|
expect(compareLocations(a, b)).toBe(0);
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("unknown location type comparison", () => {
|
2018-07-09 14:57:09 +08:00
|
|
|
it("returns 1 when the first parameter is an object and the second parameter is not", () => {
|
|
|
|
expect(compareLocations(createLocation(), 123)).toBe(1);
|
|
|
|
expect(compareLocations(createLocation(), "alpha")).toBe(1);
|
2018-01-24 20:17:21 +08:00
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-07-09 14:57:09 +08:00
|
|
|
it("returns -1 when the first parameter is not an object and the second parameter is", () => {
|
|
|
|
expect(compareLocations(123, createLocation())).toBe(-1);
|
|
|
|
expect(compareLocations("alpha", createLocation())).toBe(-1);
|
2018-01-24 20:17:21 +08:00
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-07-09 14:57:09 +08:00
|
|
|
it("returns 0 when both the first parameter and the second parameter is an object", () => {
|
|
|
|
expect(compareLocations(123, 456)).toBe(0);
|
2018-01-24 20:17:21 +08:00
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
});
|