Skip to content

Commit 09a743f

Browse files
committed
Store "that mark" in test case fixture
1 parent 0471c7f commit 09a743f

File tree

3 files changed

+47
-27
lines changed

3 files changed

+47
-27
lines changed

src/TestCase.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import * as yaml from "js-yaml";
22
import * as vscode from "vscode";
33
import { Position, Range, Selection } from "vscode";
44
import NavigationMap from "./NavigationMap";
5-
import { ActionType, PartialTarget, PrimitiveTarget, Target } from "./Types";
5+
import {
6+
ActionType,
7+
PartialTarget,
8+
PrimitiveTarget,
9+
SelectionWithEditor,
10+
Target,
11+
} from "./Types";
612

713
export type SerializedPosition = {
814
line: number;
@@ -37,13 +43,19 @@ export function serializePosition(position: Position): SerializedPosition {
3743
return { line: position.line, character: position.character };
3844
}
3945

40-
type Command = {
46+
type TestCaseCommand = {
4147
actionName: ActionType;
4248
partialTargets: PartialTarget[];
4349
extraArgs: any[];
4450
};
4551

46-
type Snapshot = {
52+
type TestCaseContext = {
53+
thatMark: SelectionWithEditor[];
54+
targets: Target[];
55+
navigationMap: NavigationMap;
56+
};
57+
58+
type TestCaseSnapshot = {
4759
document: string;
4860
clipboard: string;
4961
visibleRanges: SerializedRange[];
@@ -53,32 +65,32 @@ type Snapshot = {
5365
type DecorationRanges = { [coloredSymbol: string]: SerializedRange };
5466

5567
export type TestCaseFixture = {
56-
command: Command;
68+
command: TestCaseCommand;
5769
targets: Target[];
5870
languageId: string;
59-
decorations: DecorationRanges;
60-
initialState: Snapshot;
61-
finalState: Snapshot;
71+
marks: DecorationRanges;
72+
thatMark: SerializedSelection[];
73+
initialState: TestCaseSnapshot;
74+
finalState: TestCaseSnapshot;
6275
};
6376

6477
export default class TestCase {
65-
command: Command;
78+
command: TestCaseCommand;
6679
languageId: string;
6780
targets: Target[];
68-
decorations: DecorationRanges;
69-
initialState: Snapshot | null = null;
70-
finalState: Snapshot | null = null;
71-
72-
constructor(
73-
command: Command,
74-
targets: Target[],
75-
navigationMap: NavigationMap
76-
) {
81+
marks: DecorationRanges;
82+
thatMark: SerializedSelection[];
83+
initialState: TestCaseSnapshot | null = null;
84+
finalState: TestCaseSnapshot | null = null;
85+
86+
constructor(command: TestCaseCommand, context: TestCaseContext) {
7787
const activeEditor = vscode.window.activeTextEditor!;
88+
const { thatMark, navigationMap, targets } = context;
7889

7990
this.command = command;
8091
this.languageId = activeEditor.document.languageId;
81-
this.decorations = this.extractTargetedDecorations(targets, navigationMap);
92+
this.marks = this.extractTargetedDecorations(targets, navigationMap);
93+
this.thatMark = thatMark.map((mark) => serializeSelection(mark.selection));
8294
this.targets = targets;
8395
}
8496

@@ -123,7 +135,7 @@ export default class TestCase {
123135
return targetedDecorations;
124136
}
125137

126-
static async getSnapshot(): Promise<Snapshot> {
138+
static async getSnapshot(): Promise<TestCaseSnapshot> {
127139
const activeEditor = vscode.window.activeTextEditor!;
128140
return {
129141
document: activeEditor.document.getText(),
@@ -159,7 +171,8 @@ export default class TestCase {
159171
command: this.command,
160172
languageId: this.languageId,
161173
targets: this.targets,
162-
decorations: this.decorations,
174+
marks: this.marks,
175+
thatMark: this.thatMark,
163176
initialState: this.initialState,
164177
finalState: this.finalState,
165178
};

src/extension.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ export async function activate(context: vscode.ExtensionContext) {
134134
// console.log(`targets:`);
135135
// console.log(JSON.stringify(targets, null, 3));
136136

137-
let testCase: TestCase | null = null;
138-
if (recordTestCase) {
139-
const command = { actionName, partialTargets, extraArgs };
140-
testCase = new TestCase(command, targets, graph.navigationMap!);
141-
await testCase.saveSnapshot();
142-
}
143-
144137
const processedTargetsContext: ProcessedTargetsContext = {
145138
currentSelections:
146139
vscode.window.activeTextEditor?.selections.map((selection) => ({
@@ -155,6 +148,18 @@ export async function activate(context: vscode.ExtensionContext) {
155148

156149
const selections = processTargets(processedTargetsContext, targets);
157150

151+
let testCase: TestCase | null = null;
152+
if (recordTestCase) {
153+
const command = { actionName, partialTargets, extraArgs };
154+
const context = {
155+
thatMark,
156+
targets,
157+
navigationMap: graph.navigationMap!,
158+
};
159+
testCase = new TestCase(command, context);
160+
await testCase.saveSnapshot();
161+
}
162+
158163
const { returnValue, thatMark: newThatMark } = await action.run(
159164
selections,
160165
...extraArgs

src/test/suite/recorded.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ suite("recorded test cases", async function () {
5252
editor.selections =
5353
fixture.initialState.selections.map(deserializeSelection);
5454

55+
// TODO Restore `thatMark` from fixture
56+
5557
// TODO (Many) unsuccessful mocking attempts
5658
// TypeError: Cannot assign to read only property 'readText' of object '#<Object>'
5759
// sinon.replace(

0 commit comments

Comments
 (0)