Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add real snippet insertion test #82

Merged
merged 5 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to the Keyboard Macro Bata extension will be documented in t
- Internal
- Added `record` option to the `kb-macro.wrap` command. [#76](https://github.com/tshino/vscode-kb-macro/pull/76)
- Fixed: gen_keymap_wrapper.js end with exit code 0 even when it fails [#77](https://github.com/tshino/vscode-kb-macro/pull/77)
- Added real snippets insertion tests instead of tests based on simulating cursor movements and edits. [#82](https://github.com/tshino/vscode-kb-macro/pull/82)

### [0.11.3] - 2022-03-06
- Update
Expand Down
5 changes: 4 additions & 1 deletion test/suite/commands_to_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ const CommandsToTest = {
MoveLinesUp: { command: "editor.action.moveLinesUpAction" },
CommentLine: { command: "editor.action.commentLine" },
AddCommentLine: { command: "editor.action.addCommentLine" },
RemoveCommentLine: { command: "editor.action.removeCommentLine" }
RemoveCommentLine: { command: "editor.action.removeCommentLine" },

NextSnippetPlaceholder: { command: "jumpToNextSnippetPlaceholder", record: "side-effect" },
PrevSnippetPlaceholder: { command: "jumpToPrevSnippetPlaceholder", record: "side-effect" }
};

module.exports = { CommandsToTest };
133 changes: 133 additions & 0 deletions test/suite/playback_snippet.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
'use strict';
const assert = require('assert');
const vscode = require('vscode');
const util = require('../../src/util.js');
const { TestUtil } = require('./test_util.js');
const { CommandsToTest } = require('./commands_to_test.js');
const { keyboardMacro, awaitController } = require('../../src/extension.js');

describe('Recording and Playback: Real Snippet Insertion', () => {
let textEditor;
const Cmd = CommandsToTest;

const setSelections = async function(array) {
await awaitController.waitFor('selection', 1).catch(() => {});
const newSelections = TestUtil.arrayToSelections(array);
if (!util.isEqualSelections(textEditor.selections, newSelections)) {
const timeout = 1000;
textEditor.selections = newSelections;
await awaitController.waitFor('selection', timeout).catch(
() => { console.log('Warning: timeout in setSelections!'); }
);
}
};
const getSelections = function() {
return TestUtil.selectionsToArray(textEditor.selections);
};
const record = async function(sequence) {
keyboardMacro.startRecording();
for (let i = 0; i < sequence.length; i++) {
const { command, args } = sequence[i];
if (command === '$wrap') {
await keyboardMacro.wrapSync(args);
} else {
await vscode.commands.executeCommand(command, args);
}
}
keyboardMacro.finishRecording();
};

before(async () => {
vscode.window.showInformationMessage('Started test for Recording and Playback of real Snippet insertion.');
textEditor = await TestUtil.setupTextEditor({ content: '', language: 'javascript' });
});

describe('JavaScript snippets', () => {
beforeEach(async () => {
await TestUtil.resetDocument(textEditor, (
'\n' +
'\n' +
'\n' +
'\n' +
'\n' +
''
));
});
it('forof', async () => {
const seq = [
{ command: "$wrap", args: {
command: "editor.action.insertSnippet",
args: {
snippet:
"for (const ${1:element} of ${2:array}) {\n" +
"\t$0\n" +
"}"
},
record: "side-effect"
} },
{ command: "default:type", args: { text: "iter" } },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "default:type", args: { text: "obj" } },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "$wrap", args: Cmd.CursorDown },
{ command: "$wrap", args: Cmd.Enter }
];

await setSelections([[0, 0]]);
await record(seq);
assert.strictEqual(textEditor.document.lineAt(0).text, 'for (const iter of obj) {');
assert.strictEqual(textEditor.document.lineAt(2).text, '}');
assert.deepStrictEqual(getSelections(), [[3, 0]]);

await keyboardMacro.playback();
assert.strictEqual(textEditor.document.lineAt(3).text, 'for (const iter of obj) {');
assert.strictEqual(textEditor.document.lineAt(5).text, '}');
assert.deepStrictEqual(getSelections(), [[6, 0]]);
});
it('forin', async () => {
const seq = [
{ command: "$wrap", args: {
command: "editor.action.insertSnippet",
args: {
snippet:
"for (const ${1:key} in ${2:object}) {\n" +
"\tif (Object.hasOwnProperty.call(${2}, ${1})) {\n" +
"\t\tconst ${3:element} = ${2}[${1}];\n" +
"\t\t$0\n" +
"\t}\n" +
"}"
},
record: "side-effect"
} },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "default:type", args: { text: "obj" } },
{ command: "$wrap", args: Cmd.PrevSnippetPlaceholder },
{ command: "default:type", args: { text: "prop" } },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "default:type", args: { text: "val" } },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "$wrap", args: Cmd.CursorDown },
{ command: "$wrap", args: Cmd.CursorDown },
{ command: "$wrap", args: Cmd.Enter }
];

await setSelections([[0, 0]]);
await record(seq);
assert.strictEqual(textEditor.document.lineAt(0).text, 'for (const prop in obj) {');
assert.strictEqual(textEditor.document.lineAt(1).text, ' if (Object.hasOwnProperty.call(obj, prop)) {');
assert.strictEqual(textEditor.document.lineAt(2).text, ' const val = obj[prop];');
assert.strictEqual(textEditor.document.lineAt(4).text, ' }');
assert.strictEqual(textEditor.document.lineAt(5).text, '}');
assert.deepStrictEqual(getSelections(), [[6, 0]]);

await keyboardMacro.playback();
assert.strictEqual(textEditor.document.lineAt(6).text, 'for (const prop in obj) {');
assert.strictEqual(textEditor.document.lineAt(7).text, ' if (Object.hasOwnProperty.call(obj, prop)) {');
assert.strictEqual(textEditor.document.lineAt(8).text, ' const val = obj[prop];');
assert.strictEqual(textEditor.document.lineAt(10).text, ' }');
assert.strictEqual(textEditor.document.lineAt(11).text, '}');
assert.deepStrictEqual(getSelections(), [[12, 0]]);
});
});
});