webpack/test/compareLocations.unittest.js

114 lines
3.0 KiB
JavaScript
Raw Normal View History

"use strict";
const compareLocations = require("../lib/compareLocations");
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) => {
return {
start: createPosition(start),
end: createPosition(end),
index: index || 3
};
};
describe("compareLocations", () => {
describe("object location comparison", () => {
let a, b;
2017-01-02 07:38:59 +08:00
describe("location line number", () => {
beforeEach(() => {
2017-01-02 07:38:59 +08:00
a = createLocation({
line: 10
});
b = createLocation({
line: 20
});
});
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-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
});
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
});
describe("location index number", () => {
beforeEach(() => {
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
});
describe("same location", () => {
beforeEach(() => {
2017-01-02 07:38:59 +08:00
a = createLocation();
b = createLocation();
});
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
});
});
});
describe("unknown location type comparison", () => {
it("returns 0 when the first parameter is an object and the second parameter is not", () => {
2018-01-24 20:17:21 +08:00
expect(compareLocations(createLocation(), 123)).toBe(0);
expect(compareLocations(createLocation(), "alpha")).toBe(0);
2018-01-24 20:17:21 +08:00
});
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
it("returns undefined when the first parameter is a string and the second parameter is a number", () => {
expect(compareLocations("alpha", 123)).toBe(undefined);
2018-01-24 20:17:21 +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
});
});