Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Oct 12, 2023
1 parent f1daa55 commit 357e57d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,38 @@ import { assert } from "chai";
import { sleepWithBackoff } from "../../endToEndTestSetup";
import { isEqual } from "lodash";

async function sleepAndCheck<T>(
export async function assertCalledWithScopeInfo<T extends ScopeTypeInfo>(
fake: sinon.SinonSpy<[scopeInfos: T[]], void>,
check: () => void,
...expectedScopeInfos: T[]
) {
await sleepWithBackoff(25);
sinon.assert.called(fake);

check();

fake.resetHistory();
}

export function assertCalled<T extends ScopeTypeInfo>(
fake: sinon.SinonSpy<[scopeInfos: T[]], void>,
expectedScopeInfos: T[],
expectedNotToHaveScopeTypes: ScopeType[],
) {
return sleepAndCheck(fake, () => {
assertCalledWith(expectedScopeInfos, fake);
assertCalledWithout(expectedNotToHaveScopeTypes, fake);
});
}

export function assertCalledWithScopeInfo<T extends ScopeTypeInfo>(
fake: sinon.SinonSpy<[scopeInfos: T[]], void>,
...expectedScopeInfos: T[]
) {
return sleepAndCheck(fake, () => assertCalledWith(expectedScopeInfos, fake));
}

export async function assertCalledWithoutScopeType<T extends ScopeTypeInfo>(
fake: sinon.SinonSpy<[scopeInfos: T[]], void>,
...scopeTypes: ScopeType[]
) {
return sleepAndCheck(fake, () => assertCalledWithout(scopeTypes, fake));
}

function assertCalledWith<T extends ScopeTypeInfo>(
expectedScopeInfos: T[],
fake: sinon.SinonSpy<[scopeInfos: T[]], void>,
) {
for (const expectedScopeInfo of expectedScopeInfos) {
const actualScopeInfo = fake.lastCall.args[0].find((scopeInfo) =>
isEqual(scopeInfo.scopeType, expectedScopeInfo.scopeType),
);
assert.isDefined(actualScopeInfo);
assert.deepEqual(actualScopeInfo, expectedScopeInfo);
}

fake.resetHistory();
}

function assertCalledWithout<T extends ScopeTypeInfo>(
scopeTypes: ScopeType[],
export async function assertCalledWithoutScopeInfo<T extends ScopeTypeInfo>(
fake: sinon.SinonSpy<[scopeInfos: T[]], void>,
...scopeTypes: ScopeType[]
) {
await sleepWithBackoff(25);
sinon.assert.called(fake);

for (const scopeType of scopeTypes) {
assert.isUndefined(
fake.lastCall.args[0].find((scopeInfo) =>
isEqual(scopeInfo.scopeType, scopeType),
),
);
}

fake.resetHistory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import * as sinon from "sinon";
import {
assertCalledWithScopeInfo,
assertCalledWithoutScopeType as assertCalledWithoutScope,
assertCalledWithoutScopeInfo,
} from "./assertCalledWithScopeInfo";
import { stat, unlink, writeFile } from "fs/promises";
import { sleepWithBackoff } from "../../endToEndTestSetup";
Expand All @@ -30,7 +30,7 @@ export async function runCustomRegexScopeInfoTest() {
const disposable = scopeProvider.onDidChangeScopeSupport(fake);

try {
await assertCalledWithoutScope(fake, scopeType);
await assertCalledWithoutScopeInfo(fake, scopeType);

await writeFile(
spokenFormsJsonPath,
Expand All @@ -44,7 +44,7 @@ export async function runCustomRegexScopeInfoTest() {

await unlink(spokenFormsJsonPath);
await sleepWithBackoff(50);
await assertCalledWithoutScope(fake, scopeType);
await assertCalledWithoutScopeInfo(fake, scopeType);
} finally {
disposable.dispose();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getCursorlessApi } from "@cursorless/vscode-common";
import { LATEST_VERSION, ScopeTypeInfo, sleep } from "@cursorless/common";
import * as sinon from "sinon";
import {
assertCalled,
assertCalledWithScopeInfo,
} from "./assertCalledWithScopeInfo";
import { assertCalledWithScopeInfo } from "./assertCalledWithScopeInfo";
import { stat, unlink, writeFile } from "fs/promises";
import { sleepWithBackoff } from "../../endToEndTestSetup";

Expand All @@ -19,17 +16,14 @@ export async function runCustomSpokenFormScopeInfoTest() {
const disposable = scopeProvider.onDidChangeScopeInfo(fake);

try {
await assertCalled(
await assertCalledWithScopeInfo(
fake,
[
roundStandard,
namedFunctionStandard,
lambdaStandard,
statementStandard,
squareStandard,
subjectStandard,
],
[],
roundStandard,
namedFunctionStandard,
lambdaStandard,
statementStandard,
squareStandard,
subjectStandard,
);

await writeFile(
Expand All @@ -49,17 +43,14 @@ export async function runCustomSpokenFormScopeInfoTest() {

await unlink(spokenFormsJsonPath);
await sleepWithBackoff(50);
await assertCalled(
await assertCalledWithScopeInfo(
fake,
[
roundStandard,
namedFunctionStandard,
lambdaStandard,
statementStandard,
squareStandard,
subjectStandard,
],
[],
roundStandard,
namedFunctionStandard,
lambdaStandard,
statementStandard,
squareStandard,
subjectStandard,
);
} finally {
disposable.dispose();
Expand Down

0 comments on commit 357e57d

Please sign in to comment.