-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathclojureEval.ts
More file actions
229 lines (193 loc) · 8.34 KB
/
clojureEval.ts
File metadata and controls
229 lines (193 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import * as vscode from 'vscode';
import { cljConnection } from './cljConnection';
import { cljParser } from './cljParser';
import { nreplClient } from './nreplClient';
import { TestListener } from './testRunner';
export function clojureEval(outputChannel: vscode.OutputChannel): void {
evaluate(outputChannel, false);
}
export function clojureEvalAndShowResult(outputChannel: vscode.OutputChannel): void {
evaluate(outputChannel, true);
}
type TestResults = {
summary: {
error: number
fail: number
ns: number
pass: number
test: number
var: number
}
'testing-ns': string
'gen-input': any[]
status?: string[]
results: {
[key: string]: { // Namespace
[key: string]: {
context: any
file?: string
index: number
line?: number
message?: string
ns: string
type: string
var: string
actual?: string
expected?: string
}[];
}
}
session: string
}
function runTests(outputChannel: vscode.OutputChannel, listener: TestListener, namespace?: string): void {
if (!cljConnection.isConnected()) {
vscode.window.showWarningMessage('You must be connected to an nREPL session to test a namespace.');
return;
}
const promise: Promise<TestResults[]> = nreplClient.runTests(namespace);
promise.then((responses) => {
console.log("Test result promise delivery");
responses.forEach(response => {
console.log(response);
console.log(response.results);
if (response.status && response.status.indexOf("unknown-op") != -1) {
outputChannel.appendLine("Failed to run tests: the cider.nrepl.middleware.test middleware in not loaded.");
return;
}
for (const ns in response.results) {
const namespace = response.results[ns];
outputChannel.appendLine("Results for " + ns)
for (const varName in namespace) {
// Each var being tested reports a list of statuses, one for each
// `is` assertion in the test. Here we just want to reduce this
// down to a single pass/fail.
const statuses = new Set(namespace[varName].map(r => r.type));
const passed = (statuses.size == 0) ||
((statuses.size == 1) && statuses.has('pass'));
listener.onTestResult(ns, varName, passed);
namespace[varName].forEach(r => {
if (r.type != 'pass') {
outputChannel.appendLine(r.type + " in (" + r.var + ") (" + r.file + ":" + r.line + ")");
if (typeof r.message === 'string') {
outputChannel.appendLine(r.message);
}
if (r.expected) {
outputChannel.append("expected: " + r.expected)
}
if (r.actual) {
outputChannel.append(" actual: " + r.actual)
}
}
});
}
}
if ('summary' in response) {
const failed = response.summary.fail + response.summary.error;
if (failed > 0) {
vscode.window.showErrorMessage(failed + " tests failed.")
} else {
vscode.window.showInformationMessage(response.summary.var + " tests passed")
}
}
});
}).catch((reason): void => {
const message: string = "" + reason;
outputChannel.append("Tests failed: ");
outputChannel.appendLine(message);
});
}
export function testNamespace(outputChannel: vscode.OutputChannel, listener: TestListener): void {
const editor = vscode.window.activeTextEditor;
if (editor) {
const text = editor.document.getText();
const ns = cljParser.getNamespace(text); // log ns and 'starting'
outputChannel.appendLine("Testing " + ns)
runTests(outputChannel, listener, ns);
} else {
// if having troubles with finding the namespace (though I'm not sure
// if it can actually happen), run all tests
runAllTests(outputChannel, listener);
}
}
export function runAllTests(outputChannel: vscode.OutputChannel, listener: TestListener): void {
outputChannel.appendLine("Testing all namespaces");
runTests(outputChannel, listener);
}
function evaluate(outputChannel: vscode.OutputChannel, showResults: boolean): void {
if (!cljConnection.isConnected()) {
vscode.window.showWarningMessage('You should connect to nREPL first to evaluate code.');
return;
}
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const selection = editor.selection;
let text = editor.document.getText();
if (!selection.isEmpty) {
const ns: string = cljParser.getNamespace(text);
text = `(ns ${ns})\n${editor.document.getText(selection)}`;
}
cljConnection.sessionForFilename(editor.document.fileName).then(session => {
let response;
if (!selection.isEmpty && session.type == 'ClojureScript') {
// Piggieback's evalFile() ignores the text sent as part of the request
// and just loads the whole file content from disk. So we use eval()
// here, which as a drawback will give us a random temporary filename in
// the stacktrace should an exception occur.
response = nreplClient.evaluate(text, session.id);
} else {
response = nreplClient.evaluateFile(text, editor.document.fileName, session.id);
}
response.then(respObjs => {
if (!!respObjs[0].ex) {
vscode.window.showErrorMessage("Compilation Error")
for (let error of respObjs.map((r) => r.err).filter((e) => e)) {
outputChannel.append(error);
}
return
}
return handleSuccess(outputChannel, showResults, respObjs);
})
});
}
function handleError(outputChannel: vscode.OutputChannel, selection: vscode.Selection, showResults: boolean, session: string): Promise<void> {
if (!showResults)
vscode.window.showErrorMessage('Compilation error');
return nreplClient.stacktrace(session)
.then(stacktraceObjs => {
const stacktraceObj = stacktraceObjs[0];
if (stacktraceObj.status && stacktraceObj.status.indexOf("unknown-op") != -1) {
outputChannel.appendLine("Failed to run get a stacktrace: the cider.nrepl.middleware.stacktrace middleware in not loaded.");
return;
}
let errLine = stacktraceObj.line !== undefined ? stacktraceObj.line - 1 : 0;
let errChar = stacktraceObj.column !== undefined ? stacktraceObj.column - 1 : 0;
if (!selection.isEmpty) {
errLine += selection.start.line;
errChar += selection.start.character;
}
outputChannel.appendLine(`${stacktraceObj.class} ${stacktraceObj.message}`);
outputChannel.appendLine(` at ${stacktraceObj.file}:${errLine}:${errChar}`);
stacktraceObj.stacktrace.forEach((trace: any) => {
if (trace.flags.indexOf('tooling') > -1)
outputChannel.appendLine(` ${trace.class}.${trace.method} (${trace.file}:${trace.line})`);
});
outputChannel.show();
nreplClient.close(session);
});
}
function handleSuccess(outputChannel: vscode.OutputChannel, showResults: boolean, respObjs: any[]): void {
if (!showResults) {
vscode.window.showInformationMessage('Successfully compiled');
} else {
respObjs.forEach(respObj => {
if (respObj.out)
outputChannel.append(respObj.out);
if (respObj.err)
outputChannel.append(respObj.err);
if (respObj.value)
outputChannel.appendLine(`=> ${respObj.value}`);
outputChannel.show();
});
}
nreplClient.close(respObjs[0].session);
}