Skip to content

Commit 950860e

Browse files
authored
Remove TestCaseContext (#1485)
## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent fcb09b4 commit 950860e

File tree

5 files changed

+40
-47
lines changed

5 files changed

+40
-47
lines changed

packages/cursorless-engine/src/core/commandRunner/CommandRunner.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export class CommandRunner {
7272

7373
const commandComplete = canonicalizeAndValidateCommand(command);
7474
const {
75-
spokenForm,
7675
action: { name: actionName, args: actionArgs },
7776
targets: partialTargetDescriptors,
7877
usePrePhraseSnapshot,
@@ -127,13 +126,10 @@ export class CommandRunner {
127126
: [];
128127

129128
if (this.testCaseRecorder.isActive()) {
130-
const context = {
131-
targets: targetDescriptors,
132-
storedTargets: this.storedTargets,
133-
hatTokenMap: readableHatMap,
134-
spokenForm,
135-
};
136-
await this.testCaseRecorder.preCommandHook(commandComplete, context);
129+
await this.testCaseRecorder.preCommandHook(
130+
readableHatMap,
131+
commandComplete,
132+
);
137133
}
138134

139135
// NB: We do this once test recording has started so that we can capture

packages/cursorless-engine/src/cursorlessEngine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function createCursorlessEngine(
3838

3939
const storedTargets = new StoredTargetMap();
4040

41-
const testCaseRecorder = new TestCaseRecorder(hatTokenMap);
41+
const testCaseRecorder = new TestCaseRecorder(hatTokenMap, storedTargets);
4242

4343
const languageDefinitions = new LanguageDefinitions(treeSitter);
4444

packages/cursorless-engine/src/testCaseRecorder/TestCase.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
extractTargetedMarks,
55
ExtraSnapshotField,
66
marksToPlainObject,
7+
PartialTargetDescriptor,
78
PlainSpyIDERecordedValues,
89
ReadOnlyHatMap,
910
serialize,
@@ -16,22 +17,15 @@ import {
1617
Token,
1718
} from "@cursorless/common";
1819
import { pick } from "lodash";
19-
import { StoredTargetMap } from "..";
2020
import { ide } from "../singletons/ide.singleton";
2121
import { cleanUpTestCaseCommand } from "../testUtil/cleanUpTestCaseCommand";
2222
import { extractTargetKeys } from "../testUtil/extractTargetKeys";
2323
import { takeSnapshot } from "../testUtil/takeSnapshot";
24-
import { TargetDescriptor } from "../typings/TargetDescriptor";
25-
26-
export type TestCaseContext = {
27-
storedTargets: StoredTargetMap;
28-
targets: TargetDescriptor[];
29-
hatTokenMap: ReadOnlyHatMap;
30-
};
24+
import { StoredTargetMap } from "..";
3125

3226
export class TestCase {
3327
private languageId: string;
34-
private fullTargets: TargetDescriptor[];
28+
private partialTargetDescriptors: PartialTargetDescriptor[];
3529
private initialState: TestCaseSnapshot | null = null;
3630
private finalState?: TestCaseSnapshot;
3731
thrownError?: ThrownError;
@@ -44,7 +38,8 @@ export class TestCase {
4438

4539
constructor(
4640
command: CommandLatest,
47-
private context: TestCaseContext,
41+
private hatTokenMap: ReadOnlyHatMap,
42+
private storedTargets: StoredTargetMap,
4843
private spyIde: SpyIDE,
4944
private isHatTokenMapTest: boolean = false,
5045
private isDecorationsTest: boolean = false,
@@ -55,34 +50,33 @@ export class TestCase {
5550
const activeEditor = ide().activeTextEditor!;
5651
this.command = cleanUpTestCaseCommand(command);
5752

58-
const { targets } = context;
59-
60-
this.targetKeys = targets.map(extractTargetKeys).flat();
53+
this.targetKeys = command.targets.map(extractTargetKeys).flat();
6154

6255
this.languageId = activeEditor.document.languageId;
63-
this.fullTargets = targets;
56+
this.partialTargetDescriptors = command.targets;
6457
this._awaitingFinalMarkInfo = isHatTokenMapTest;
6558
}
6659

6760
private getMarks() {
6861
let marks: Record<string, Token>;
6962

70-
const { hatTokenMap } = this.context;
71-
7263
if (this.isHatTokenMapTest) {
7364
// If we're doing a navigation map test, then we grab the entire
7465
// navigation map because we'll filter it later based on the marks
7566
// referenced in the expected follow up command
76-
marks = Object.fromEntries(hatTokenMap.getEntries());
67+
marks = Object.fromEntries(this.hatTokenMap.getEntries());
7768
} else {
78-
marks = extractTargetedMarks(this.targetKeys, hatTokenMap);
69+
marks = extractTargetedMarks(this.targetKeys, this.hatTokenMap);
7970
}
8071

8172
return marksToPlainObject(marks);
8273
}
8374

84-
private includesThatMark(target: TargetDescriptor, type: string): boolean {
85-
if (target.type === "primitive" && target.mark.type === type) {
75+
private includesThatMark(
76+
target: PartialTargetDescriptor,
77+
type: string,
78+
): boolean {
79+
if (target.type === "primitive" && target.mark?.type === type) {
8680
return true;
8781
} else if (target.type === "list") {
8882
return target.elements.some((target) =>
@@ -114,13 +108,13 @@ export class TestCase {
114108
thatMark:
115109
(!isInitialSnapshot && !this.captureFinalThatMark) ||
116110
(isInitialSnapshot &&
117-
!this.fullTargets.some((target) =>
111+
!this.partialTargetDescriptors.some((target) =>
118112
this.includesThatMark(target, "that"),
119113
)),
120114
sourceMark:
121115
(!isInitialSnapshot && !this.captureFinalThatMark) ||
122116
(isInitialSnapshot &&
123-
!this.fullTargets.some((target) =>
117+
!this.partialTargetDescriptors.some((target) =>
124118
this.includesThatMark(target, "source"),
125119
)),
126120
visibleRanges: !visibleRangeActions.includes(this.command.action.name),
@@ -154,7 +148,7 @@ export class TestCase {
154148
async recordInitialState() {
155149
const excludeFields = this.getExcludedFields(true);
156150
this.initialState = await takeSnapshot(
157-
this.context.storedTargets,
151+
this.storedTargets,
158152
excludeFields,
159153
this.extraSnapshotFields,
160154
ide().activeTextEditor!,
@@ -168,7 +162,7 @@ export class TestCase {
168162
const excludeFields = this.getExcludedFields(false);
169163
this.returnValue = returnValue;
170164
this.finalState = await takeSnapshot(
171-
this.context.storedTargets,
165+
this.storedTargets,
172166
excludeFields,
173167
this.extraSnapshotFields,
174168
ide().activeTextEditor!,
@@ -185,8 +179,10 @@ export class TestCase {
185179
raw == null ? undefined : spyIDERecordedValuesToPlainObject(raw);
186180
}
187181

188-
filterMarks(command: CommandLatest, context: TestCaseContext) {
189-
const marksToCheck = context.targets.map(extractTargetKeys).flat();
182+
filterMarks() {
183+
const marksToCheck = this.partialTargetDescriptors
184+
.map(extractTargetKeys)
185+
.flat();
190186
const keys = this.targetKeys.concat(marksToCheck);
191187

192188
this.initialState!.marks = pick(

packages/cursorless-engine/src/testCaseRecorder/TestCaseRecorder.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
HatTokenMap,
1010
IDE,
1111
marksToPlainObject,
12+
ReadOnlyHatMap,
1213
serialize,
1314
SerializedMarks,
1415
showError,
@@ -26,7 +27,8 @@ import { merge } from "lodash";
2627
import * as path from "path";
2728
import { ide, injectIde } from "../singletons/ide.singleton";
2829
import { takeSnapshot } from "../testUtil/takeSnapshot";
29-
import { TestCase, TestCaseContext } from "./TestCase";
30+
import { TestCase } from "./TestCase";
31+
import { StoredTargetMap } from "../core/StoredTargets";
3032

3133
const CALIBRATION_DISPLAY_DURATION_MS = 50;
3234

@@ -100,7 +102,10 @@ export class TestCaseRecorder {
100102
private spyIde: SpyIDE | undefined;
101103
private originalIde: IDE | undefined;
102104

103-
constructor(private hatTokenMap: HatTokenMap) {
105+
constructor(
106+
private hatTokenMap: HatTokenMap,
107+
private storedTargets: StoredTargetMap,
108+
) {
104109
const { runMode } = ide();
105110

106111
this.fixtureRoot =
@@ -284,7 +289,7 @@ export class TestCaseRecorder {
284289
this.paused = false;
285290
}
286291

287-
async preCommandHook(command: CommandLatest, context: TestCaseContext) {
292+
async preCommandHook(hatTokenMap: ReadOnlyHatMap, command: CommandLatest) {
288293
if (this.testCase != null) {
289294
// If testCase is not null and we are just before a command, this means
290295
// that this command is the follow up command indicating which marks we
@@ -293,7 +298,7 @@ export class TestCaseRecorder {
293298
this.testCase.awaitingFinalMarkInfo,
294299
() => "expected to be awaiting final mark info",
295300
);
296-
this.testCase.filterMarks(command, context);
301+
this.testCase.filterMarks();
297302
await this.finishTestCase();
298303
} else {
299304
// Otherwise, we are starting a new test case
@@ -303,7 +308,8 @@ export class TestCaseRecorder {
303308

304309
this.testCase = new TestCase(
305310
command,
306-
context,
311+
hatTokenMap,
312+
this.storedTargets,
307313
this.spyIde,
308314
this.isHatTokenMapTest,
309315
this.isDecorationsTest,

packages/cursorless-engine/src/testUtil/extractTargetKeys.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@ import {
33
PartialTargetDescriptor,
44
getKey,
55
} from "@cursorless/common";
6-
import {
7-
PrimitiveTargetDescriptor,
8-
TargetDescriptor,
9-
} from "../typings/TargetDescriptor";
6+
import { PrimitiveTargetDescriptor } from "../typings/TargetDescriptor";
107

11-
export function extractTargetKeys(
12-
target: TargetDescriptor | PartialTargetDescriptor,
13-
): string[] {
8+
export function extractTargetKeys(target: PartialTargetDescriptor): string[] {
149
switch (target.type) {
1510
case "primitive":
1611
return extractPrimitiveTargetKeys(target);

0 commit comments

Comments
 (0)