Skip to content

Commit

Permalink
Fixes; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Nov 27, 2021
1 parent 454f4be commit fd37e49
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/core/HatAllocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class HatAllocator {

this.addDecorationsDebounced = this.addDecorationsDebounced.bind(this);
this.toggleDecorations = this.toggleDecorations.bind(this);
this.clearEditorDecorations = this.clearEditorDecorations.bind(this);

this.disposalFunctions.push(
graph.decorations.registerDecorationChangeListener(
Expand Down
1 change: 0 additions & 1 deletion src/core/HatTokenMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HatStyleName } from "./constants";
import { Graph } from "../typings/Types";
import { Signal } from "../util/getExtensionApi";
import { IndividualHatMap, ReadOnlyHatMap } from "./IndividualHatMap";
import { HatAllocator } from "./HatAllocator";

Expand Down
6 changes: 1 addition & 5 deletions src/test/mockPrePhraseGetVersion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as sinon from "sinon";
import { Signal } from "../util/getExtensionApi";
import { Graph } from "../typings/Types";

export function mockPrePhraseGetVersion(
Expand All @@ -8,12 +7,9 @@ export function mockPrePhraseGetVersion(
) {
sinon.replaceGetter(graph, "commandServerApi", () => ({
signals: {
getNamedSignal(name: string) {
return {} as Signal;
},
prePhrase: {
getVersion,
} as Signal,
},
},
}));
}
18 changes: 18 additions & 0 deletions src/test/openNewEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as vscode from "vscode";
import { getParseTreeApi } from "../util/getExtensionApi";

export async function openNewEditor(
content: string,
language: string = "plaintext"
) {
await vscode.commands.executeCommand("workbench.action.closeAllEditors");

const document = await vscode.workspace.openTextDocument({
language,
content,
});

await (await getParseTreeApi()).loadLanguage(language);

return await vscode.window.showTextDocument(document);
}
10 changes: 2 additions & 8 deletions src/test/suite/prePhraseSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as sinon from "sinon";
import { getCursorlessApi } from "../../util/getExtensionApi";
import { selectionToPlainObject } from "../../testUtil/toPlainObject";
import { mockPrePhraseGetVersion } from "../mockPrePhraseGetVersion";
import { openNewEditor } from "../openNewEditor";

/**
* The selections we expect when the pre-phrase snapshot is used
Expand Down Expand Up @@ -39,16 +40,9 @@ async function runTest(
multiplePhrases: boolean,
expectedSelections: vscode.Selection[]
) {
const initialContent = "Hello world testing whatever";

const graph = (await getCursorlessApi()).graph!;

await vscode.commands.executeCommand("workbench.action.closeAllEditors");
const document = await vscode.workspace.openTextDocument({
language: "plaintext",
content: initialContent,
});
const editor = await vscode.window.showTextDocument(document);
const editor = await openNewEditor("Hello world testing whatever");

editor.selections = [new vscode.Selection(0, 0, 0, 0)];

Expand Down
20 changes: 6 additions & 14 deletions src/test/suite/recorded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ import {
SerializedMarks,
} from "../../testUtil/toPlainObject";
import { walkFilesSync } from "../../testUtil/walkSync";
import {
getCursorlessApi,
getParseTreeApi,
Signal,
} from "../../util/getExtensionApi";
import { getCursorlessApi } from "../../util/getExtensionApi";
import { enableDebugLog } from "../../util/debug";
import { extractTargetedMarks } from "../../testUtil/extractTargetedMarks";
import { ReadOnlyHatMap } from "../../core/IndividualHatMap";
import { doTargetsUsePrePhraseSnapshot } from "../../util/doTargetsUsePrePhraseSnapshot";
import { mockPrePhraseGetVersion } from "../mockPrePhraseGetVersion";
import { openNewEditor } from "../openNewEditor";

function createPosition(position: PositionPlainObject) {
return new vscode.Position(position.line, position.character);
Expand Down Expand Up @@ -61,24 +58,19 @@ async function runTest(file: string) {
const excludeFields: string[] = [];

const cursorlessApi = await getCursorlessApi();
const parseTreeApi = await getParseTreeApi();
const graph = cursorlessApi.graph!;

await vscode.commands.executeCommand("workbench.action.closeAllEditors");
const document = await vscode.workspace.openTextDocument({
language: fixture.languageId,
content: fixture.initialState.documentContents,
});
const editor = await vscode.window.showTextDocument(document);
const editor = await openNewEditor(
fixture.initialState.documentContents,
fixture.languageId
);

if (!fixture.initialState.documentContents.includes("\n")) {
await editor.edit((editBuilder) => {
editBuilder.setEndOfLine(vscode.EndOfLine.LF);
});
}

await parseTreeApi.loadLanguage(document.languageId);

editor.selections = fixture.initialState.selections.map(createSelection);

if (fixture.initialState.thatMark) {
Expand Down
36 changes: 36 additions & 0 deletions src/test/suite/toggleDecorations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as assert from "assert";
import * as vscode from "vscode";
import * as sinon from "sinon";
import { getCursorlessApi } from "../../util/getExtensionApi";
import { openNewEditor } from "../openNewEditor";

suite("toggle decorations", async function () {
this.timeout("100s");
this.retries(3);

teardown(() => {
sinon.restore();
});

test("toggle decorations", () => runTest());
});

async function runTest() {
const { hatTokenMap } = (await getCursorlessApi()).graph!;

await openNewEditor("Hello world testing whatever");

// Check that hats appear by default
await hatTokenMap.addDecorations();
assert((await hatTokenMap.getReadableMap(false)).getEntries().length !== 0);

// Check that hats disappear when turned off
await vscode.commands.executeCommand("cursorless.toggleDecorations");
await hatTokenMap.addDecorations();
assert((await hatTokenMap.getReadableMap(false)).getEntries().length === 0);

// Check that hats reappear when turned back on
await vscode.commands.executeCommand("cursorless.toggleDecorations");
await hatTokenMap.addDecorations();
assert((await hatTokenMap.getReadableMap(false)).getEntries().length !== 0);
}
5 changes: 2 additions & 3 deletions src/util/getExtensionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ export interface ParseTreeApi {
loadLanguage: (languageId: string) => Promise<boolean>;
}

export interface Signal {
export interface InboundSignal {
getVersion(): Promise<string | null>;
}

export interface CommandServerApi {
signals: {
getNamedSignal: (name: string) => Signal;
prePhrase: Signal;
prePhrase: InboundSignal;
};
}

Expand Down

0 comments on commit fd37e49

Please sign in to comment.