-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
173 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 22 additions & 7 deletions
29
packages/cursorless-vscode-e2e/src/suite/scopeProvider/assertCalledWithScopeInfo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
import { ScopeSupportInfo, ScopeSupportLevels } from "@cursorless/common"; | ||
import { ScopeType, ScopeTypeInfo } from "@cursorless/common"; | ||
import Sinon = require("sinon"); | ||
import { assert } from "chai"; | ||
import { sleepWithBackoff } from "../../endToEndTestSetup"; | ||
import { isEqual } from "lodash"; | ||
|
||
export function assertCalledWithScopeInfo( | ||
fake: Sinon.SinonSpy<[scopeInfos: ScopeSupportLevels], void>, | ||
expectedScopeInfo: ScopeSupportInfo, | ||
export async function assertCalledWithScopeInfo<T extends ScopeTypeInfo>( | ||
fake: Sinon.SinonSpy<[scopeInfos: T[]], void>, | ||
expectedScopeInfo: T, | ||
) { | ||
await sleepWithBackoff(25); | ||
Sinon.assert.called(fake); | ||
const actualScopeInfo = fake.lastCall.args[0].find( | ||
(scopeInfo) => | ||
scopeInfo.scopeType.type === expectedScopeInfo.scopeType.type, | ||
const actualScopeInfo = fake.lastCall.args[0].find((scopeInfo) => | ||
isEqual(scopeInfo.scopeType, expectedScopeInfo.scopeType), | ||
); | ||
assert.isDefined(actualScopeInfo); | ||
assert.deepEqual(actualScopeInfo, expectedScopeInfo); | ||
fake.resetHistory(); | ||
} | ||
|
||
export async function assertCalledWithoutScopeInfo<T extends ScopeTypeInfo>( | ||
fake: Sinon.SinonSpy<[scopeInfos: T[]], void>, | ||
scopeType: ScopeType, | ||
) { | ||
await sleepWithBackoff(25); | ||
Sinon.assert.called(fake); | ||
const actualScopeInfo = fake.lastCall.args[0].find((scopeInfo) => | ||
isEqual(scopeInfo.scopeType, scopeType), | ||
); | ||
assert.isUndefined(actualScopeInfo); | ||
fake.resetHistory(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/cursorless-vscode-e2e/src/suite/scopeProvider/runCustomRegexScopeInfoTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { getCursorlessApi, openNewEditor } from "@cursorless/vscode-common"; | ||
import { | ||
LATEST_VERSION, | ||
ScopeSupport, | ||
ScopeSupportInfo, | ||
ScopeSupportLevels, | ||
ScopeType, | ||
} from "@cursorless/common"; | ||
import Sinon = require("sinon"); | ||
import { | ||
assertCalledWithScopeInfo, | ||
assertCalledWithoutScopeInfo as assertCalledWithoutScope, | ||
} from "./assertCalledWithScopeInfo"; | ||
import { stat, unlink, writeFile } from "fs/promises"; | ||
import { sleepWithBackoff } from "../../endToEndTestSetup"; | ||
|
||
/** | ||
* Tests that the scope provider correctly reports the scope support for a | ||
* simple named function. | ||
*/ | ||
export async function runCustomRegexScopeInfoTest() { | ||
const { scopeProvider, spokenFormsJsonPath } = (await getCursorlessApi()) | ||
.testHelpers!; | ||
const fake = Sinon.fake<[scopeInfos: ScopeSupportLevels], void>(); | ||
|
||
const disposable = scopeProvider.onDidChangeScopeSupport(fake); | ||
|
||
try { | ||
await assertCalledWithoutScope(fake, scopeType); | ||
|
||
await writeFile( | ||
spokenFormsJsonPath, | ||
JSON.stringify(spokenFormJsonContents), | ||
); | ||
await sleepWithBackoff(50); | ||
await assertCalledWithScopeInfo(fake, unsupported); | ||
|
||
await openNewEditor(contents); | ||
await assertCalledWithScopeInfo(fake, present); | ||
|
||
await unlink(spokenFormsJsonPath); | ||
await sleepWithBackoff(50); | ||
await assertCalledWithoutScope(fake, scopeType); | ||
} finally { | ||
disposable.dispose(); | ||
|
||
// Delete spokenFormsJsonPath if it exists | ||
try { | ||
await stat(spokenFormsJsonPath); | ||
await unlink(spokenFormsJsonPath); | ||
} catch (e) { | ||
// Do nothing | ||
} | ||
} | ||
} | ||
|
||
const contents = ` | ||
hello world | ||
`; | ||
|
||
const regex = "[a-zA-Z]+"; | ||
|
||
const spokenFormJsonContents = { | ||
version: LATEST_VERSION, | ||
entries: [ | ||
{ | ||
type: "customRegex", | ||
id: regex, | ||
spokenForms: ["spaghetti"], | ||
}, | ||
], | ||
}; | ||
|
||
const scopeType: ScopeType = { | ||
type: "customRegex", | ||
regex, | ||
}; | ||
|
||
function getExpectedScope(scopeSupport: ScopeSupport): ScopeSupportInfo { | ||
return { | ||
humanReadableName: "Regex `[a-zA-Z]+`", | ||
isLanguageSpecific: false, | ||
iterationScopeSupport: | ||
scopeSupport === ScopeSupport.unsupported | ||
? ScopeSupport.unsupported | ||
: ScopeSupport.supportedAndPresentInEditor, | ||
scopeType, | ||
spokenForm: { | ||
alternatives: [], | ||
preferred: "spaghetti", | ||
type: "success", | ||
}, | ||
support: scopeSupport, | ||
}; | ||
} | ||
|
||
const unsupported = getExpectedScope(ScopeSupport.unsupported); | ||
const present = getExpectedScope(ScopeSupport.supportedAndPresentInEditor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters