-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
277 lines (253 loc) · 9.09 KB
/
index.js
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(function () {
function $(selector) {return document.querySelector(selector);}
function $$(selector) {return Array.from(document.querySelectorAll(selector));}
const PASS_COLOR = "rgb(92,190,93)";
const FAIL_COLOR = "rgb(209,70,78)";
const SLOW_TEST_THRESHOLD = 3000;
const beforeContainer = $("#beforewrapper");
const setupArea = addTextArea(beforeContainer);
const coverageWorker = new Worker('coverageworker.js');
let localStorageName = '';
let lastFocusedTest = undefined;
// Creates a div with a text area, a button, and error and reporting lines;
// returns the textarea. Supply a callback for the button, which is also
// fired on Shift+Delete.
function addTextArea(container, callback) {
let div = document.createElement("div");
div.className = "cell";
let textArea = document.createElement("textarea");
let errorDiv = document.createElement("div");
let reportDiv = document.createElement("div");
errorDiv.className = "testerror";
reportDiv.className = "testoutput";
div.appendChild(textArea);
if (callback) {
let closer = document.createElement("span");
closer.className = "deletebutton";
closer.innerHTML = "🗑";
closer.addEventListener('click', () => { callback(div); });
closer.style.display = 'none';
textArea.addEventListener('keydown', e => {
if (e.keyCode === 8 && e.shiftKey) {
callback(div);
e.preventDefault();
return false;
}
});
div.addEventListener('mouseenter', e => {
closer.style.display = 'inline';
});
div.addEventListener('mouseleave', e => {
closer.style.display = 'none';
});
div.appendChild(closer);
}
div.appendChild(errorDiv);
div.appendChild(reportDiv);
textArea.addEventListener('focus', e => { lastFocusedTest = textArea; });
container.appendChild(div);
return textArea;
}
// Adds a new text area for tests. Shift+Enter also creates a new one.
function addTest() {
let textArea = addTextArea($("#testwrapper"), div => deleteTest(div));
textArea.setAttribute('placeholder', 'Write a test, like eq(2+2, 4)');
textArea.addEventListener('input', () => {sizeBox(textArea); evalTest(textArea);});
textArea.addEventListener('input', debouncedCoverage);
textArea.focus();
textArea.worker = new Worker('testrunner.js');
textArea.worker.addEventListener('message', result => {
textArea.pendingCalls--;
textArea.lastReceipt = new Date();
let [success, output, errorMessage] = result.data;
textArea.style.borderLeftColor = success ? PASS_COLOR : FAIL_COLOR;
if (textArea.nextSibling.nextSibling) {
textArea.nextSibling.nextSibling.innerHTML = errorMessage;
if (textArea.nextSibling.nextSibling.nextSibling) {
textArea.nextSibling.nextSibling.nextSibling.innerHTML = output.join('<br>');
}
}
});
textArea.pendingCalls = 0;
return textArea;
}
// When deleting a text, make sure the focus goes to the test just before it.
function deleteTest(div) {
lastFocusedTest = div.previousSibling ? div.previousSibling.firstChild : undefined;
if (lastFocusedTest) {
lastFocusedTest.focus();
}
div.remove();
}
// We don't want a bunch of blank tests, so only add one if the last test is not empty
function addTestOrFocusLastTestIfEmpty() {
const lastTest = $('#testwrapper').lastChild;
if (lastTest && lastTest.firstChild && lastTest.firstChild.value.trim().length === 0) {
lastTest.firstChild.focus();
} else {
addTest();
}
}
function sizeBox(box) {
box.style.height = 'auto';
box.style.height = box.scrollHeight + 'px';
box.scrollTop = box.scrollHeight;
window.scrollTo(window.scrollLeft, (box.scrollTop + box.scrollHeight));
}
function evalTest(textArea) {
let test = textArea.value;
if (test.trim() === '') {
textArea.style.borderLeftColor = FAIL_COLOR;
return;
}
let before = beforeContainer.style.display === 'none' ? '' : setupArea.value;
textArea.pendingCalls++;
textArea.lastCall = new Date();
textArea.worker.postMessage(`"use strict";\n${editor.getValue()}\n;${before}\n;${test}`);
}
function runAllTests() {
$$("#testwrapper textarea").forEach(test => evalTest(test));
}
function sizeAllBoxes() {
sizeBox(setupArea);
$$("#testwrapper textarea").forEach(box => sizeBox(box));
}
function serialize() {
return {
setup: beforeContainer.style.display === 'none' ? null : setupArea.value,
tests: $$("#testwrapper textarea").map(area => area.value),
code: editor.getValue()
}
}
function load() {
let key = prompt('Enter a local storage key from which to load your work');
if (!key) return;
localStorageName = key;
let bundle = JSON.parse(localStorage.getItem('livetdd-' + key));
if (!(bundle && 'setup' in bundle && 'tests' in bundle && 'code' in bundle)) {
alert('Not a valid snapshot');
return;
}
editor.setValue(bundle.code, -1);
if (bundle.setup) {
setupArea.value = bundle.setup;
}
$$("#testwrapper div").forEach(div => div.remove());
bundle.tests.forEach(test => addTest().value = test);
sizeAllBoxes();
runAllTests();
}
function save() {
if (!localStorageName) {
saveAs();
return;
}
localStorage.setItem(`livetdd-${localStorageName}`, JSON.stringify(serialize()));
flash(`Saved to local storage at ${localStorageName}`);
}
function saveAs() {
let key = prompt('Enter a local storage key at which to save your work');
if (!key) return;
localStorageName = key;
save();
}
function exportAll() {
alert('Export is not yet implemented');
// TODO
}
function flash(message) {
$('#flash').innerHTML = message;
$('#flash').style.right = '0';
setTimeout(() => $('#flash').style.right = '-320px', 2000);
}
// TODO This belongs in a class. This file is getting out of hand.
let allMarkers = [];
function mark(start, end) {
let range = new Range(start.line-1, start.column, end.line-1, end.column);
allMarkers.push(editor.session.addMarker(range, 'uncovered', 'text'));
}
function clearAllMarkers() {
while (allMarkers.length > 0) editor.session.removeMarker(allMarkers.pop())
}
function runCoverage() {
clearAllMarkers();
coverageWorker.postMessage(serialize());
}
const debouncedCoverage = _.debounce(runCoverage, 500);
// The coverage worker return a pair [ok, message]. If ok, we highlight the
// uncovered regions of code. Right now we have only satement coverage and
// function coverage.
coverageWorker.addEventListener('message', message => {
if (!message.data) {
return;
}
let coverage = message.data[Object.keys(message.data)];
for (let key in coverage.s) {
if (coverage.s[key] === 0) {
let range = coverage.statementMap[key];
mark(range.start, range.end)
}
}
for (let key in coverage.f) {
if (coverage.f[key] === 0) {
let range = coverage.fnMap[key].loc;
mark(range.start, range.end)
}
}
});
function monitorTests() {
$$("#testwrapper textarea").forEach(test => {
let now = new Date();
if (!test.dead && test.pendingCalls > 0 && now-test.lastCall > SLOW_TEST_THRESHOLD) {
test.dead = true;
test.style.background = 'red';
test.worker.terminate();
test.nextSibling.nextSibling.innerHTML = 'Test is too slow, so I killed it';
}
});
setTimeout(monitorTests, 3000);
}
function initializeModal(trigger, modal) {
const dismiss = e => {
modal.style.display = 'none';
document.body.removeEventListener('keyup', dismiss);
};
trigger.addEventListener('click', () => {
modal.style.display = 'block';
document.body.addEventListener('keyup', dismiss);
});
modal.addEventListener('click', dismiss);
}
function addListeners() {
setupArea.setAttribute('placeholder', 'Add code to be run before every test here.');
setupArea.addEventListener('input', () => {sizeBox(setupArea); runAllTests();});
setupArea.value = 'let eq = chai.assert.equal;\nlet ok = chai.assert.ok;';
$('.addtest').addEventListener('click', addTestOrFocusLastTestIfEmpty);
initializeModal($("#helpbutton"), $(".modal"));
$("#loadbutton").addEventListener('click', load);
$("#savebutton").addEventListener('click', save);
$("#saveasbutton").addEventListener('click', saveAs);
$("#exportbutton").addEventListener('click', exportAll);
document.body.addEventListener('keydown', e => {
if (e.keyCode === 13 && e.shiftKey) {
addTestOrFocusLastTestIfEmpty();
e.preventDefault();
return false;
}
if (e.keyCode === 13 && e.ctrlKey) {
if (!editor.isFocused()) {
editor.focus();
} else if (lastFocusedTest) {
lastFocusedTest.focus();
}
e.preventDefault();
return false;
}
});
editor.getSession().on('change', runAllTests);
editor.getSession().on('change', debouncedCoverage);
}
addListeners();
addTest();
monitorTests();
}());