Skip to content

Commit

Permalink
Store "that mark" in test case fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
brxck committed Jul 15, 2021
1 parent 0471c7f commit 09a743f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
53 changes: 33 additions & 20 deletions src/TestCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import * as yaml from "js-yaml";
import * as vscode from "vscode";
import { Position, Range, Selection } from "vscode";
import NavigationMap from "./NavigationMap";
import { ActionType, PartialTarget, PrimitiveTarget, Target } from "./Types";
import {
ActionType,
PartialTarget,
PrimitiveTarget,
SelectionWithEditor,
Target,
} from "./Types";

export type SerializedPosition = {
line: number;
Expand Down Expand Up @@ -37,13 +43,19 @@ export function serializePosition(position: Position): SerializedPosition {
return { line: position.line, character: position.character };
}

type Command = {
type TestCaseCommand = {
actionName: ActionType;
partialTargets: PartialTarget[];
extraArgs: any[];
};

type Snapshot = {
type TestCaseContext = {
thatMark: SelectionWithEditor[];
targets: Target[];
navigationMap: NavigationMap;
};

type TestCaseSnapshot = {
document: string;
clipboard: string;
visibleRanges: SerializedRange[];
Expand All @@ -53,32 +65,32 @@ type Snapshot = {
type DecorationRanges = { [coloredSymbol: string]: SerializedRange };

export type TestCaseFixture = {
command: Command;
command: TestCaseCommand;
targets: Target[];
languageId: string;
decorations: DecorationRanges;
initialState: Snapshot;
finalState: Snapshot;
marks: DecorationRanges;
thatMark: SerializedSelection[];
initialState: TestCaseSnapshot;
finalState: TestCaseSnapshot;
};

export default class TestCase {
command: Command;
command: TestCaseCommand;
languageId: string;
targets: Target[];
decorations: DecorationRanges;
initialState: Snapshot | null = null;
finalState: Snapshot | null = null;

constructor(
command: Command,
targets: Target[],
navigationMap: NavigationMap
) {
marks: DecorationRanges;
thatMark: SerializedSelection[];
initialState: TestCaseSnapshot | null = null;
finalState: TestCaseSnapshot | null = null;

constructor(command: TestCaseCommand, context: TestCaseContext) {
const activeEditor = vscode.window.activeTextEditor!;
const { thatMark, navigationMap, targets } = context;

this.command = command;
this.languageId = activeEditor.document.languageId;
this.decorations = this.extractTargetedDecorations(targets, navigationMap);
this.marks = this.extractTargetedDecorations(targets, navigationMap);
this.thatMark = thatMark.map((mark) => serializeSelection(mark.selection));
this.targets = targets;
}

Expand Down Expand Up @@ -123,7 +135,7 @@ export default class TestCase {
return targetedDecorations;
}

static async getSnapshot(): Promise<Snapshot> {
static async getSnapshot(): Promise<TestCaseSnapshot> {
const activeEditor = vscode.window.activeTextEditor!;
return {
document: activeEditor.document.getText(),
Expand Down Expand Up @@ -159,7 +171,8 @@ export default class TestCase {
command: this.command,
languageId: this.languageId,
targets: this.targets,
decorations: this.decorations,
marks: this.marks,
thatMark: this.thatMark,
initialState: this.initialState,
finalState: this.finalState,
};
Expand Down
19 changes: 12 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ export async function activate(context: vscode.ExtensionContext) {
// console.log(`targets:`);
// console.log(JSON.stringify(targets, null, 3));

let testCase: TestCase | null = null;
if (recordTestCase) {
const command = { actionName, partialTargets, extraArgs };
testCase = new TestCase(command, targets, graph.navigationMap!);
await testCase.saveSnapshot();
}

const processedTargetsContext: ProcessedTargetsContext = {
currentSelections:
vscode.window.activeTextEditor?.selections.map((selection) => ({
Expand All @@ -155,6 +148,18 @@ export async function activate(context: vscode.ExtensionContext) {

const selections = processTargets(processedTargetsContext, targets);

let testCase: TestCase | null = null;
if (recordTestCase) {
const command = { actionName, partialTargets, extraArgs };
const context = {
thatMark,
targets,
navigationMap: graph.navigationMap!,
};
testCase = new TestCase(command, context);
await testCase.saveSnapshot();
}

const { returnValue, thatMark: newThatMark } = await action.run(
selections,
...extraArgs
Expand Down
2 changes: 2 additions & 0 deletions src/test/suite/recorded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ suite("recorded test cases", async function () {
editor.selections =
fixture.initialState.selections.map(deserializeSelection);

// TODO Restore `thatMark` from fixture

// TODO (Many) unsuccessful mocking attempts
// TypeError: Cannot assign to read only property 'readText' of object '#<Object>'
// sinon.replace(
Expand Down

0 comments on commit 09a743f

Please sign in to comment.