2017-01-18 00:17:54 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-12-20 15:51:54 +08:00
|
|
|
const { compareLocations } = require("../lib/util/comparators");
|
2025-07-02 20:10:54 +08:00
|
|
|
|
2025-07-17 00:13:14 +08:00
|
|
|
const createPosition = (overrides) => ({
|
2024-07-31 11:31:11 +08:00
|
|
|
line: 10,
|
|
|
|
column: 5,
|
|
|
|
...overrides
|
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
|
2024-07-31 11:31:11 +08:00
|
|
|
const createLocation = (start, end, index) => ({
|
|
|
|
start: createPosition(start),
|
|
|
|
end: createPosition(end),
|
|
|
|
index: index || 3
|
|
|
|
});
|
2017-01-24 14:55:39 +08:00
|
|
|
|
2017-01-18 00:17:54 +08:00
|
|
|
describe("compareLocations", () => {
|
|
|
|
describe("object location comparison", () => {
|
2024-07-31 09:56:53 +08:00
|
|
|
let a;
|
|
|
|
let 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-15 16:09:21 +08:00
|
|
|
it("returns 0 when both the first parameter and the second parameter are not objects", () => {
|
2018-07-09 14:57:09 +08:00
|
|
|
expect(compareLocations(123, 456)).toBe(0);
|
2018-01-24 20:17:21 +08:00
|
|
|
});
|
2017-01-02 07:38:59 +08:00
|
|
|
});
|
|
|
|
});
|