Skip to content

Commit 4dc0dd0

Browse files
committed
Simplify tests; add visualize test
1 parent d5c3142 commit 4dc0dd0

File tree

4 files changed

+136
-150
lines changed

4 files changed

+136
-150
lines changed

package.json

Lines changed: 107 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,108 @@
11
{
2-
"name": "vscode-syntax-tree",
3-
"displayName": "Syntax Tree",
4-
"description": "VSCode support for the syntax_tree gem",
5-
"icon": "doc/logo.png",
6-
"version": "0.2.1",
7-
"publisher": "ruby-syntax-tree",
8-
"repository": {
9-
"type": "git",
10-
"url": "https://github.com/ruby-syntax-tree/vscode-syntax-tree.git"
11-
},
12-
"license": "MIT",
13-
"bugs": {
14-
"url": "https://github.com/ruby-syntax-tree/vscode-syntax-tree/issues"
15-
},
16-
"packageManager": "[email protected]",
17-
"engines": {
18-
"vscode": "^1.66.0"
19-
},
20-
"activationEvents": [
21-
"onLanguage:ruby",
22-
"workspaceContains:Gemfile.lock",
23-
"onCommand:syntaxTree.start",
24-
"onCommand:syntaxTree.stop",
25-
"onCommand:syntaxTree.restart",
26-
"onCommand:syntaxTree.showOutputChannel",
27-
"onCommand:syntaxTree.visualize"
28-
],
29-
"main": "./out/extension",
30-
"contributes": {
31-
"commands": [
32-
{
33-
"command": "syntaxTree.start",
34-
"title": "Syntax Tree: Start"
35-
},
36-
{
37-
"command": "syntaxTree.stop",
38-
"title": "Syntax Tree: Stop"
39-
},
40-
{
41-
"command": "syntaxTree.restart",
42-
"title": "Syntax Tree: Restart"
43-
},
44-
{
45-
"command": "syntaxTree.clearOutputChannel",
46-
"title": "Syntax Tree: Clear Output Channel"
47-
},
48-
{
49-
"command": "syntaxTree.showOutputChannel",
50-
"title": "Syntax Tree: Show Output Channel"
51-
},
52-
{
53-
"command": "syntaxTree.visualize",
54-
"title": "Syntax Tree: Visualize"
55-
}
56-
],
57-
"configuration": {
58-
"type": "object",
59-
"title": "Syntax Tree",
60-
"properties": {
61-
"syntaxTree.singleQuotes": {
62-
"default": false,
63-
"markdownDescription": "Uses single-quoted strings when possible.",
64-
"type": "boolean"
65-
},
66-
"syntaxTree.trailingComma": {
67-
"default": false,
68-
"markdownDescription": "Adds a trailing comma to multi-line array literals, hash literals, and method parameters.",
69-
"type": "boolean"
70-
},
71-
"syntaxTree.additionalPlugins": {
72-
"default": [],
73-
"markdownDescription": "Registers [extra behaviors](https://github.com/ruby-syntax-tree/syntax_tree#plugins) with the language server.",
74-
"items": {
75-
"type": "string"
76-
},
77-
"type": "array"
78-
}
79-
}
80-
},
81-
"colors": []
82-
},
83-
"scripts": {
84-
"clean": "rm -Rf out",
85-
"compile": "tsc -p ./",
86-
"package": "vsce package --yarn --githubBranch main",
87-
"publish": "vsce publish --yarn --githubBranch main",
88-
"vscode:prepublish": "yarn compile",
89-
"test": "node ./out/test/runTest.js",
90-
"watch": "tsc --watch -p ./"
91-
},
92-
"dependencies": {
93-
"vscode-languageclient": "8.0.2-next.5"
94-
},
95-
"devDependencies": {
96-
"@types/glob": "^7.1.1",
97-
"@types/mocha": "^9.1.1",
98-
"@types/node": "^18.0.0",
99-
"@types/vscode": "^1.68.0",
100-
"@vscode/test-electron": "^1.6.1",
101-
"glob": "^7.1.4",
102-
"mocha": "^9.1.1",
103-
"typescript": "^4.7.4",
104-
"vsce": "^2.9.2"
105-
}
106-
}
2+
"name": "vscode-syntax-tree",
3+
"displayName": "Syntax Tree",
4+
"description": "VSCode support for the syntax_tree gem",
5+
"icon": "doc/logo.png",
6+
"version": "0.2.1",
7+
"publisher": "ruby-syntax-tree",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/ruby-syntax-tree/vscode-syntax-tree.git"
11+
},
12+
"license": "MIT",
13+
"bugs": {
14+
"url": "https://github.com/ruby-syntax-tree/vscode-syntax-tree/issues"
15+
},
16+
"packageManager": "[email protected]",
17+
"engines": {
18+
"vscode": "^1.66.0"
19+
},
20+
"activationEvents": [
21+
"onLanguage:ruby",
22+
"workspaceContains:Gemfile.lock",
23+
"onCommand:syntaxTree.start",
24+
"onCommand:syntaxTree.stop",
25+
"onCommand:syntaxTree.restart",
26+
"onCommand:syntaxTree.showOutputChannel",
27+
"onCommand:syntaxTree.visualize"
28+
],
29+
"main": "./out/extension",
30+
"contributes": {
31+
"commands": [
32+
{
33+
"command": "syntaxTree.start",
34+
"title": "Syntax Tree: Start"
35+
},
36+
{
37+
"command": "syntaxTree.stop",
38+
"title": "Syntax Tree: Stop"
39+
},
40+
{
41+
"command": "syntaxTree.restart",
42+
"title": "Syntax Tree: Restart"
43+
},
44+
{
45+
"command": "syntaxTree.showOutputChannel",
46+
"title": "Syntax Tree: Show Output Channel"
47+
},
48+
{
49+
"command": "syntaxTree.visualize",
50+
"title": "Syntax Tree: Visualize"
51+
}
52+
],
53+
"configuration": {
54+
"type": "object",
55+
"title": "Syntax Tree",
56+
"properties": {
57+
"syntaxTree.singleQuotes": {
58+
"default": false,
59+
"markdownDescription": "Uses single-quoted strings when possible.",
60+
"type": "boolean"
61+
},
62+
"syntaxTree.trailingComma": {
63+
"default": false,
64+
"markdownDescription": "Adds a trailing comma to multi-line array literals, hash literals, and method parameters.",
65+
"type": "boolean"
66+
},
67+
"syntaxTree.additionalPlugins": {
68+
"default": [],
69+
"markdownDescription": "Registers [extra behaviors](https://github.com/ruby-syntax-tree/syntax_tree#plugins) with the language server.",
70+
"items": {
71+
"type": "string"
72+
},
73+
"type": "array"
74+
}
75+
}
76+
},
77+
"colors": []
78+
},
79+
"scripts": {
80+
"clean": "rm -Rf out",
81+
"compile": "tsc -p ./",
82+
"package": "vsce package --yarn --githubBranch main",
83+
"publish": "vsce publish --yarn --githubBranch main",
84+
"vscode:prepublish": "yarn compile",
85+
"test": "node ./out/test/runTest.js",
86+
"watch": "tsc --watch -p ./"
87+
},
88+
"dependencies": {
89+
"vscode-languageclient": "8.0.2-next.5"
90+
},
91+
"devDependencies": {
92+
"@types/glob": "^7.1.1",
93+
"@types/mocha": "^9.1.1",
94+
"@types/node": "^18.0.0",
95+
"@types/vscode": "^1.68.0",
96+
"@vscode/test-electron": "^1.6.1",
97+
"glob": "^7.1.4",
98+
"mocha": "^9.1.1",
99+
"typescript": "^4.7.4",
100+
"vsce": "^2.9.2"
101+
},
102+
"__metadata": {
103+
"id": "b46118f9-0f6f-4320-9e2e-75c96492b4cb",
104+
"publisherDisplayName": "ruby-syntax-tree",
105+
"publisherId": "63942dce-de09-44d8-b863-4a1dbd5508c6",
106+
"isPreReleaseVersion": false
107+
}
108+
}

src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import Visualize from "./Visualize";
1010
const promiseExec = promisify(exec);
1111

1212
// This object will get initialized once the language client is ready. It will
13-
// get set back to null when the extension is deactivated.
14-
let languageClient: LanguageClient | null = null;
13+
// get set back to null when the extension is deactivated. It is exported for
14+
// easier testing.
15+
export let languageClient: LanguageClient | null = null;
1516

1617
// This is the expected top-level export that is called by VSCode when the
1718
// extension is activated.
@@ -39,7 +40,6 @@ export async function activate(context: ExtensionContext) {
3940
commands.registerCommand("syntaxTree.stop", stopLanguageServer),
4041
commands.registerCommand("syntaxTree.restart", restartLanguageServer),
4142
commands.registerCommand("syntaxTree.visualize", () => visualizer?.visualize()),
42-
commands.registerCommand("syntaxTree.clearOutputChannel", () => outputChannel.clear()),
4343
commands.registerCommand("syntaxTree.showOutputChannel", () => outputChannel.show()),
4444
workspace.onDidChangeConfiguration(event => {
4545
if (event.affectsConfiguration("syntaxTree")) {

src/test/suite/automation.ts

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@ import * as assert from 'assert';
22
import * as path from 'path';
33
import { TextEncoder } from 'util';
44

5-
import { Range, Uri, commands, window, workspace } from 'vscode';
5+
import { Uri, commands, window, workspace } from 'vscode';
66

77
import { WORKSPACE_DIR } from './setup';
88

99
export async function reset() {
1010
await commands.executeCommand('workbench.action.closeAllEditors');
11-
await clearOutputChannel();
12-
13-
// Ensure output panel is closed
14-
await commands.executeCommand('workbench.panel.output.focus');
15-
await commands.executeCommand('workbench.action.output.toggleOutput');
16-
17-
// Ensure something neutral is focused
18-
await commands.executeCommand('workbench.explorer.fileView.focus');
1911
}
2012

21-
export async function createEditor(content: string, filename = 'test.rb') {
13+
export async function createEditor(content: string) {
14+
const filename = `${Math.random().toString().slice(2)}.rb`;
2215
const uri = Uri.file(`${WORKSPACE_DIR}${path.sep}${filename}`);
2316
await workspace.fs.writeFile(uri, new TextEncoder().encode(content));
2417
await window.showTextDocument(uri);
@@ -27,6 +20,10 @@ export async function createEditor(content: string, filename = 'test.rb') {
2720
return window.activeTextEditor;
2821
}
2922

23+
export function findNewestEditor() {
24+
return window.visibleTextEditors[window.visibleTextEditors.length - 1];
25+
}
26+
3027
export function formatDocument() {
3128
return commands.executeCommand('editor.action.formatDocument', 'ruby-syntax-tree.vscode-syntax-tree');
3229
}
@@ -35,30 +32,14 @@ export function restart() {
3532
return commands.executeCommand('syntaxTree.restart');
3633
}
3734

38-
export function clearOutputChannel() {
39-
return commands.executeCommand('syntaxTree.showOutputChannel');
40-
}
41-
42-
export function showOutputChannel(timeout = 15000) {
43-
// TODO: make use of await commands.executeCommand('workbench.panel.output.focus'); ??
44-
return new Promise<string>((c, e) => {
45-
const failSauce = new Error(`No active editor change within ${timeout}ms`)
46-
const timer = setTimeout(() => e(failSauce), timeout);
47-
const disposable = window.onDidChangeActiveTextEditor(e => {
48-
if (e) {
49-
clearTimeout(timer);
50-
disposable.dispose();
51-
c(e.document.getText())
52-
}
53-
})
54-
commands.executeCommand('syntaxTree.showOutputChannel');
55-
})
56-
}
57-
5835
export function start() {
5936
return commands.executeCommand('syntaxTree.start');
6037
}
6138

6239
export function stop() {
6340
return commands.executeCommand('syntaxTree.stop');
6441
}
42+
43+
export function visualize() {
44+
return commands.executeCommand('syntaxTree.visualize');
45+
}

src/test/suite/index.test.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as assert from 'assert';
22
import { before, beforeEach } from 'mocha';
3+
import { State } from 'vscode-languageclient';
34

45
import * as auto from './automation';
5-
6-
// can directly import extension if needed
7-
// import * as myExtension from '../../extension';
6+
import * as extension from '../../extension';
87

98
const UNFORMATTED = `class Foo; def bar; puts 'baz'; end; end`;
109

@@ -21,33 +20,37 @@ suite('Syntax Tree', () => {
2120
suite('lifecycle commands', () => {
2221
test('start', async () => {
2322
await auto.start();
24-
const text = await auto.showOutputChannel();
25-
assert.match(text, /^Starting language server/);
23+
assert.notEqual(extension.languageClient, null)
24+
assert.equal(extension.languageClient?.state, State.Running)
2625
});
2726

2827
test('stop', async () => {
2928
await auto.start();
3029
await auto.stop();
31-
const text = await auto.showOutputChannel();
32-
assert.match(text, /^Stopping language server/m);
30+
assert.equal(extension.languageClient, null)
3331
})
3432

3533
test('restart', async () => {
3634
await auto.restart();
37-
const text = await auto.showOutputChannel();
38-
assert.match(text, /^Restarting language server/m);
39-
assert.match(text, /^Stopping language server/m);
40-
assert.match(text, /^Starting language server/);
35+
assert.notEqual(extension.languageClient, null)
36+
assert.equal(extension.languageClient?.state, State.Running)
4137
})
4238
});
4339

4440
suite('functional commands', () => {
4541
before(auto.start);
4642

47-
test('Format Document', async () => {
43+
test('format', async () => {
4844
const editor = await auto.createEditor(UNFORMATTED);
4945
await auto.formatDocument();
5046
assert.equal(editor.document.getText(), FORMATTED);
5147
});
48+
49+
test('visualize', async () => {
50+
await auto.createEditor(UNFORMATTED);
51+
await auto.visualize();
52+
const editor = auto.findNewestEditor();
53+
assert.match(editor.document.getText(), /^\(program/);
54+
})
5255
})
5356
});

0 commit comments

Comments
 (0)