-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BaseScopeHandler.test.ts
187 lines (162 loc) · 4.6 KB
/
BaseScopeHandler.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { TextEditor, Position, Direction, Range } from "@cursorless/common";
import { BaseScopeHandler } from "./BaseScopeHandler";
import { TargetScope } from "./scope.types";
import {
CustomScopeType,
ScopeIteratorRequirements,
} from "./scopeHandler.types";
import { compareTargetScopes } from "./compareTargetScopes";
import assert from "assert";
class TestScopeHandler extends BaseScopeHandler {
public scopeType = undefined;
public get iterationScopeType(): CustomScopeType {
throw new Error("Method not implemented.");
}
constructor(
private scopes: TargetScope[],
protected isHierarchical: boolean,
) {
super();
}
protected *generateScopeCandidates(
_editor: TextEditor,
_position: Position,
_direction: Direction,
_hints: ScopeIteratorRequirements,
): Iterable<TargetScope> {
yield* this.scopes;
}
}
interface DummyScope {
/** Start offset of scope's domain */
start: number;
/** End offset of scope's domain */
end: number;
/** Indicates whether we expect this scope to be yielded */
shouldYield: boolean;
}
type Hints = Omit<Partial<ScopeIteratorRequirements>, "distalPosition"> & {
distalPosition?: number;
};
interface TestCase {
name: string;
isHierarchical: boolean;
hints: Hints;
position: number;
direction: Direction;
scopes: DummyScope[];
documentEnd?: number;
}
/** These are the actual test cases. Everything else is scaffolding */
const testCases: TestCase[] = [
{
name: "should yield all scopes in simple case",
isHierarchical: false,
hints: {},
position: 0,
direction: "forward",
scopes: [{ start: 0, end: 2, shouldYield: true }],
},
{
name: "should handle skipAncestorScopes with required containment",
isHierarchical: true,
hints: {
containment: "required",
skipAncestorScopes: true,
},
position: 1,
direction: "forward",
scopes: [
{ start: 2, end: 3, shouldYield: false },
{ start: 0, end: 4, shouldYield: true },
],
},
{
name: "should handle skipAncestorScopes with required containment in between scopes",
isHierarchical: true,
hints: {
containment: "required",
skipAncestorScopes: true,
allowAdjacentScopes: true,
},
position: 2,
direction: "forward",
scopes: [
{ start: 1, end: 2, shouldYield: true },
// Skip next because we skip ancestor scopes
{ start: 0, end: 2, shouldYield: false },
{ start: 2, end: 3, shouldYield: true },
],
},
{
name: "should handle skipAncestorScopes with optional containment",
isHierarchical: true,
hints: {
skipAncestorScopes: true,
distalPosition: 3,
},
position: 2,
direction: "forward",
scopes: [
{ start: 2, end: 3, shouldYield: true },
{ start: 0, end: 3, shouldYield: false },
],
},
];
suite("BaseScopeHandler", () => {
testCases.forEach((testCase) => {
test(testCase.name, () => {
const editor = {
document: { range: toRange(0, testCase.documentEnd ?? Infinity) },
} as TextEditor;
const position = toPosition(testCase.position);
const inputScopes = testCase.scopes.map((scope) => ({
editor,
domain: toRange(scope.start, scope.end),
getTargets: () => undefined as any,
}));
assert.deepStrictEqual(
[...inputScopes]
.sort((a, b) =>
compareTargetScopes(testCase.direction, position, a, b),
)
.map(fromScope),
inputScopes.map(fromScope),
"input scopes must be sorted",
);
const handler = new TestScopeHandler(
inputScopes,
testCase.isHierarchical,
);
const hints = {
...testCase.hints,
distalPosition:
testCase.hints.distalPosition == null
? undefined
: toPosition(testCase.hints.distalPosition),
};
const actualScopes = Array.from(
handler.generateScopes(editor, position, testCase.direction, hints),
).map(fromScope);
const expectedScopes = testCase.scopes
.filter((scope) => scope.shouldYield)
.map(({ start, end }) => ({ start, end }));
assert.deepStrictEqual(actualScopes, expectedScopes);
});
});
});
function fromScope(scope: TargetScope) {
return {
start: fromPosition(scope.domain.start),
end: fromPosition(scope.domain.end),
};
}
function fromPosition(position: Position): number {
return position.character;
}
function toPosition(offset: number): Position {
return new Position(0, offset);
}
function toRange(start: number, end: number): Range {
return new Range(0, start, 0, end);
}