Skip to content

Commit 5fd68e6

Browse files
Added tests
1 parent c5070f2 commit 5fd68e6

File tree

3 files changed

+112
-2
lines changed

3 files changed

+112
-2
lines changed

packages/common/src/types/Position.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ export class Position {
2121
* @return A position with the given line and character values.
2222
*/
2323
static fromConcise(concise: string): Position {
24-
const [line, character] = concise.split(":").map((s) => parseInt(s, 10));
24+
const parts = concise.split(":");
25+
if (parts.length !== 2) {
26+
throw new Error(
27+
`Invalid concise position format: "${concise}". Expected "line:character" format.`,
28+
);
29+
}
30+
const [line, character] = parts.map((s) => parseInt(s, 10));
2531
return new Position(line, character);
2632
}
2733

packages/common/src/types/Range.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ export class Range {
2020
* @return A range with the given start and end positions.
2121
*/
2222
static fromConcise(concise: string): Range {
23-
const [start, end] = concise.split("-").map((s) => Position.fromConcise(s));
23+
const parts = concise.split("-");
24+
if (parts.length !== 2) {
25+
throw new Error(
26+
`Invalid concise range format: "${concise}". Expected "start-end" format.`,
27+
);
28+
}
29+
const [start, end] = parts.map((s) => Position.fromConcise(s));
2430
return new Range(start, end);
2531
}
2632

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import * as assert from "assert";
2+
import { calculateHighlights } from "./calculateHighlights";
3+
import type { Fixture, Scope } from "./types";
4+
5+
interface Test {
6+
name: string;
7+
scopes: Scope[];
8+
expected: string[];
9+
}
10+
11+
const tests: Test[] = [
12+
{
13+
name: "Distant targets",
14+
scopes: [
15+
{
16+
targets: [{ content: "0:3-0:5" }, { content: "0:7-0:9" }],
17+
},
18+
],
19+
expected: ["0:3-0:5", "0:7-0:9"],
20+
},
21+
{
22+
name: "Adjacent targets",
23+
scopes: [
24+
{
25+
targets: [{ content: "0:3-0:5" }, { content: "0:5-0:9" }],
26+
},
27+
],
28+
expected: ["0:3-0:5", "0:5-0:9"],
29+
},
30+
{
31+
name: "Overlapping targets",
32+
scopes: [
33+
{
34+
targets: [{ content: "0:3-0:5" }, { content: "0:4-0:9" }],
35+
},
36+
],
37+
expected: ["0:3-0:5", "0:4-0:5", "0:5-0:9"],
38+
},
39+
{
40+
name: "Domain == target",
41+
scopes: [
42+
{
43+
domain: "0:3-0:5",
44+
targets: [{ content: "0:3-0:5" }],
45+
},
46+
],
47+
expected: ["0:3-0:5"],
48+
},
49+
{
50+
name: "Domain contains target",
51+
scopes: [
52+
{
53+
domain: "0:3-0:6",
54+
targets: [{ content: "0:3-0:5" }],
55+
},
56+
],
57+
expected: ["0:3-0:5", "0:3-0:6"],
58+
},
59+
{
60+
name: "Target contains domain",
61+
scopes: [
62+
{
63+
domain: "0:3-0:5",
64+
targets: [{ content: "0:3-0:6" }],
65+
},
66+
],
67+
expected: ["0:3-0:6", "0:3-0:5"],
68+
},
69+
{
70+
name: "Domain overlaps target",
71+
scopes: [
72+
{
73+
domain: "0:3-0:5",
74+
targets: [{ content: "0:4-0:6" }],
75+
},
76+
],
77+
expected: ["0:4-0:6"],
78+
},
79+
];
80+
81+
suite("calculate highlights", () => {
82+
tests.forEach((t) => {
83+
const fixture: Fixture = {
84+
name: t.name,
85+
scopes: t.scopes,
86+
facet: "line",
87+
languageId: "plaintext",
88+
code: "",
89+
};
90+
test(fixture.name, () => {
91+
const highlights = calculateHighlights(fixture, "content");
92+
assert.equal(highlights.length, t.expected.length);
93+
for (let i = 0; i < highlights.length; i++) {
94+
assert.equal(highlights[i].range.concise(), t.expected[i]);
95+
}
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)