aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/test/vscode_diagnostics.test.ts
diff options
context:
space:
mode:
authorRyan Cumming <[email protected]>2019-06-26 11:14:18 +0100
committerRyan Cumming <[email protected]>2019-06-26 11:31:36 +0100
commitf82ceca0bd8de2a2b0b51c96c5c1678351a7a20a (patch)
tree89e70d9965f973615dc0dfe978db002d82bf7e25 /editors/code/src/test/vscode_diagnostics.test.ts
parentafd18dbcb8147cb83de408b7da310ee187faf3df (diff)
Initial Visual Studio Code unit tests
As promised in #1439 this is an initial attempt at unit testing the VSCode extension. There are two separate parts to this: getting the test framework working and unit testing the code in #1439. The test framework nearly intact from the VSCode extension generator. The main thing missing was `test/index.ts` which acts as an entry point for Mocha. This was simply copied back in. I also needed to open the test VSCode instance inside a workspace as our file URI generation depends on a workspace being open. There are two ways to run the test framework: 1. Opening the extension's source in VSCode, pressing F5 and selecting the "Extensions Test" debug target. 2. Closing all copies of VSCode and running `npm test`. This is started from the command line but actually opens a temporary VSCode window to host the tests. This doesn't attempt to wire this up to CI. That requires running a headless X11 server which is a bit daunting. I'll assess the difficulty of that in a follow-up branch. This PR is at least helpful for local development without having to induce errors on a Rust project. For the actual tests this uses snapshots of `rustc` output from a real Rust project captured from the command line. Except for extracting the `message` object and reformatting they're copied verbatim into fixture JSON files. Only four different types of diagnostics are tested but they represent the main combinations of code actions and related information possible. They can be considered the happy path tests; as we encounter corner-cases we can introduce new tests fixtures.
Diffstat (limited to 'editors/code/src/test/vscode_diagnostics.test.ts')
-rw-r--r--editors/code/src/test/vscode_diagnostics.test.ts164
1 files changed, 164 insertions, 0 deletions
diff --git a/editors/code/src/test/vscode_diagnostics.test.ts b/editors/code/src/test/vscode_diagnostics.test.ts
new file mode 100644
index 000000000..ca4345626
--- /dev/null
+++ b/editors/code/src/test/vscode_diagnostics.test.ts
@@ -0,0 +1,164 @@
1import * as assert from 'assert';
2import * as vscode from 'vscode';
3
4import {
5 areCodeActionsEqual,
6 areDiagnosticsEqual
7} from '../utils/vscode_diagnostics';
8
9const uri = vscode.Uri.file('/file/1');
10
11const range1 = new vscode.Range(
12 new vscode.Position(1, 2),
13 new vscode.Position(3, 4)
14);
15
16const range2 = new vscode.Range(
17 new vscode.Position(5, 6),
18 new vscode.Position(7, 8)
19);
20
21describe('areDiagnosticsEqual', () => {
22 it('should treat identical diagnostics as equal', () => {
23 const diagnostic1 = new vscode.Diagnostic(
24 range1,
25 'Hello, world!',
26 vscode.DiagnosticSeverity.Error
27 );
28
29 const diagnostic2 = new vscode.Diagnostic(
30 range1,
31 'Hello, world!',
32 vscode.DiagnosticSeverity.Error
33 );
34
35 assert(areDiagnosticsEqual(diagnostic1, diagnostic2));
36 });
37
38 it('should treat diagnostics with different ranges as inequal', () => {
39 const diagnostic1 = new vscode.Diagnostic(
40 range1,
41 'Hello, world!',
42 vscode.DiagnosticSeverity.Error
43 );
44
45 const diagnostic2 = new vscode.Diagnostic(
46 range2,
47 'Hello, world!',
48 vscode.DiagnosticSeverity.Error
49 );
50
51 assert(!areDiagnosticsEqual(diagnostic1, diagnostic2));
52 });
53
54 it('should treat diagnostics with different messages as inequal', () => {
55 const diagnostic1 = new vscode.Diagnostic(
56 range1,
57 'Hello, world!',
58 vscode.DiagnosticSeverity.Error
59 );
60
61 const diagnostic2 = new vscode.Diagnostic(
62 range1,
63 'Goodbye!, world!',
64 vscode.DiagnosticSeverity.Error
65 );
66
67 assert(!areDiagnosticsEqual(diagnostic1, diagnostic2));
68 });
69
70 it('should treat diagnostics with different severities as inequal', () => {
71 const diagnostic1 = new vscode.Diagnostic(
72 range1,
73 'Hello, world!',
74 vscode.DiagnosticSeverity.Warning
75 );
76
77 const diagnostic2 = new vscode.Diagnostic(
78 range1,
79 'Hello, world!',
80 vscode.DiagnosticSeverity.Error
81 );
82
83 assert(!areDiagnosticsEqual(diagnostic1, diagnostic2));
84 });
85});
86
87describe('areCodeActionsEqual', () => {
88 it('should treat identical actions as equal', () => {
89 const codeAction1 = new vscode.CodeAction(
90 'Fix me!',
91 vscode.CodeActionKind.QuickFix
92 );
93
94 const codeAction2 = new vscode.CodeAction(
95 'Fix me!',
96 vscode.CodeActionKind.QuickFix
97 );
98
99 const edit = new vscode.WorkspaceEdit();
100 edit.replace(uri, range1, 'Replace with this');
101 codeAction1.edit = edit;
102 codeAction2.edit = edit;
103
104 assert(areCodeActionsEqual(codeAction1, codeAction2));
105 });
106
107 it('should treat actions with different types as inequal', () => {
108 const codeAction1 = new vscode.CodeAction(
109 'Fix me!',
110 vscode.CodeActionKind.Refactor
111 );
112
113 const codeAction2 = new vscode.CodeAction(
114 'Fix me!',
115 vscode.CodeActionKind.QuickFix
116 );
117
118 const edit = new vscode.WorkspaceEdit();
119 edit.replace(uri, range1, 'Replace with this');
120 codeAction1.edit = edit;
121 codeAction2.edit = edit;
122
123 assert(!areCodeActionsEqual(codeAction1, codeAction2));
124 });
125
126 it('should treat actions with different titles as inequal', () => {
127 const codeAction1 = new vscode.CodeAction(
128 'Fix me!',
129 vscode.CodeActionKind.Refactor
130 );
131
132 const codeAction2 = new vscode.CodeAction(
133 'Do something different!',
134 vscode.CodeActionKind.Refactor
135 );
136
137 const edit = new vscode.WorkspaceEdit();
138 edit.replace(uri, range1, 'Replace with this');
139 codeAction1.edit = edit;
140 codeAction2.edit = edit;
141
142 assert(!areCodeActionsEqual(codeAction1, codeAction2));
143 });
144
145 it('should treat actions with different edits as inequal', () => {
146 const codeAction1 = new vscode.CodeAction(
147 'Fix me!',
148 vscode.CodeActionKind.Refactor
149 );
150 const edit1 = new vscode.WorkspaceEdit();
151 edit1.replace(uri, range1, 'Replace with this');
152 codeAction1.edit = edit1;
153
154 const codeAction2 = new vscode.CodeAction(
155 'Fix me!',
156 vscode.CodeActionKind.Refactor
157 );
158 const edit2 = new vscode.WorkspaceEdit();
159 edit2.replace(uri, range1, 'Replace with this other thing');
160 codeAction2.edit = edit2;
161
162 assert(!areCodeActionsEqual(codeAction1, codeAction2));
163 });
164});