2017-01-18 00:17:54 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const compareLocations = require("../lib/compareLocations");
|
2018-02-25 18:46:17 +08:00
|
|
|
const createPosition = overides => {
|
|
|
|
return Object.assign(
|
|
|
|
{
|
|
|
|
line: 10,
|
|
|
|
column: 5
|
|
|
|
},
|
|
|
|
overides
|
|
|
|
);
|
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("string location comparison", () => {
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns -1 when the first string comes before the second string", () => {
|
|
|
|
expect(compareLocations("alpha", "beta")).toBe(-1);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns 1 when the first string comes after the second string", () => {
|
|
|
|
expect(compareLocations("beta", "alpha")).toBe(1);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns 0 when the first string is the same as the second string", () => {
|
|
|
|
expect(compareLocations("charlie", "charlie")).toBe(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", () => {
|
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("string and object location comparison", () => {
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns 1 when the first parameter is a string and the second parameter is an object", () => {
|
|
|
|
expect(compareLocations("alpha", createLocation())).toBe(1);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns -1 when the first parameter is an object and the second parameter is a string", () => {
|
|
|
|
expect(compareLocations(createLocation(), "alpha")).toBe(-1);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("unknown location type comparison", () => {
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns 0 when the first parameter is an object and the second parameter is a number", () => {
|
|
|
|
expect(compareLocations(createLocation(), 123)).toBe(0);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns undefined when the first parameter is a number and the second parameter is an object", () => {
|
|
|
|
expect(compareLocations(123, createLocation())).toBe(undefined);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns 0 when the first parameter is a string and the second parameter is a number", () => {
|
|
|
|
expect(compareLocations("alpha", 123)).toBe(0);
|
|
|
|
});
|
2017-01-18 00:17:54 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns undefined when the first parameter is a number and the second parameter is a string", () => {
|
|
|
|
expect(compareLocations(123, "alpha")).toBe(undefined);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2018-01-24 20:17:21 +08:00
|
|
|
it("returns undefined when both the first parameter and the second parameter is a number", () => {
|
|
|
|
expect(compareLocations(123, 456)).toBe(undefined);
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
});
|