/
index-spec.js
451 lines (403 loc) · 14.9 KB
/
index-spec.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"use babel";
import { CompositeDisposable } from "atom";
import { isObservableMap, isObservableProp, isComputedProp } from "mobx";
import store, { Store } from "./../../lib/store";
import KernelTransport from "./../../lib/kernel-transport";
import Kernel from "./../../lib/kernel";
import MarkerStore from "./../../lib/store/markers";
const commutable = require("@nteract/commutable");
describe("Store initialize", () => {
it("should correctly initialize store", () => {
expect(store.subscriptions instanceof CompositeDisposable).toBeTruthy();
expect(store.markers instanceof MarkerStore).toBeTruthy();
expect(store.runningKernels).toEqual([]);
expect(isObservableMap(store.startingKernels)).toBeTruthy();
expect(isObservableMap(store.kernelMapping)).toBeTruthy();
expect(isObservableProp(store, "editor")).toBeTruthy();
expect(isObservableProp(store, "grammar")).toBeTruthy();
expect(isComputedProp(store, "kernel")).toBeTruthy();
expect(isComputedProp(store, "notebook")).toBeTruthy();
});
});
describe("Store", () => {
beforeEach(() => {
store.subscriptions = new CompositeDisposable();
store.startingKernels = store.kernelMapping = new Map();
store.runningKernels = [];
store.editor = null;
store.grammar = null;
});
describe("setGrammar", () => {
it("should set grammar", () => {
const editor = atom.workspace.buildTextEditor();
const grammar = editor.getGrammar();
expect(grammar.name).toBe("Null Grammar");
expect(store.editor).toBeNull();
store.setGrammar(editor);
expect(store.grammar).toEqual(grammar);
});
it("should set grammar to null if editor is null", () => {
store.setGrammar(null);
expect(store.grammar).toBeNull();
});
it("should set grammar to null if editor is undefined", () => {
store.setGrammar(undefined);
expect(store.grammar).toBeNull();
});
it("should set non multi language grammar", () => {
const grammar = { scopeName: "source.python", name: "Python" };
const editor = { getGrammar: () => grammar };
store.setGrammar(editor);
expect(store.grammar).toEqual(grammar);
expect(store.grammar.name.toLowerCase()).toBe("python");
});
it("should set multi language grammar inside code block", () => {
const editor = {
getGrammar: () => {
return { scopeName: "source.gfm", name: "GitHub Markdown" };
},
getCursorBufferPosition: () => {},
scopeDescriptorForBufferPosition: () => {
return {
getScopesArray: () => [
"source.gfm",
"markup.code.python.gfm",
"source.embedded.python"
]
};
}
};
const pythonGrammar = { scopeName: "source.python", name: "Python" };
spyOn(atom.grammars, "grammarForScopeName").and.returnValue(
pythonGrammar
);
store.setGrammar(editor);
expect(store.grammar).toEqual(pythonGrammar);
expect(store.grammar.name.toLowerCase()).toBe("python");
});
it("should set multi language grammar outside code block", () => {
const grammar = { scopeName: "source.gfm", name: "GitHub Markdown" };
const editor = {
getGrammar: () => grammar,
getCursorBufferPosition: () => {},
scopeDescriptorForBufferPosition: () => {
return {
getScopesArray: () => ["source.gfm", "markup.code.python.gfm"]
};
}
};
store.setGrammar(editor);
expect(store.grammar).toEqual(grammar);
expect(store.grammar.name.toLowerCase()).toBe("github markdown");
});
});
describe("startKernel", () => {
it("should add display name to startingKernels", () => {
const display_name = "null grammar";
expect(store.startingKernels.get(display_name)).toBeFalsy();
store.startKernel(display_name);
expect(store.startingKernels.get(display_name)).toBeTruthy();
});
});
describe("newKernel", () => {
it("should store kernel", () => {
const editor = { getGrammar: () => ({ scopeName: "source.python" }) };
const kernel = { kernelSpec: { display_name: "Python 3" } };
spyOn(store.startingKernels, "delete");
store.newKernel(kernel, "foo.py", editor);
expect(store.kernelMapping.size).toBe(1);
expect(store.kernelMapping.get("foo.py")).toEqual(kernel);
expect(store.runningKernels).toEqual([kernel]);
expect(store.startingKernels.delete).toHaveBeenCalledWith("Python 3");
});
it("should store kernel for multilanguage file", () => {
const editor = { getGrammar: () => ({ scopeName: "source.gfm" }) };
const kernel = { kernelSpec: { display_name: "Python 3" } };
const kernel2 = { kernelSpec: { display_name: "Javascript" } };
spyOn(store.startingKernels, "delete");
store.newKernel(kernel, "foo.md", editor, { name: "python" });
expect(store.kernelMapping.size).toBe(1);
expect(store.kernelMapping.get("foo.md").toJS()).toEqual(
new Map([["python", kernel]])
);
expect(store.runningKernels).toEqual([kernel]);
expect(store.startingKernels.delete).toHaveBeenCalledWith("Python 3");
store.newKernel(kernel2, "foo.md", editor, { name: "javascript" });
expect(store.kernelMapping.size).toBe(1);
expect(store.kernelMapping.get("foo.md").toJS()).toEqual(
new Map([["python", kernel], ["javascript", kernel2]])
);
expect(store.runningKernels).toEqual([kernel, kernel2]);
expect(store.startingKernels.delete).toHaveBeenCalledWith("Javascript");
});
});
describe("deleteKernel", () => {
it("should delete kernel", () => {
const kernel1 = new Kernel(
new KernelTransport(
{
display_name: "Python 3",
language: "python"
},
{ name: "python" }
)
);
const kernel2 = new Kernel(
new KernelTransport(
{
display_name: "Python 3",
language: "python"
},
{ name: "python" }
)
);
const kernel3 = new Kernel(
new KernelTransport(
{
display_name: "JS",
language: "Javascript"
},
{ name: "javascript" }
)
);
store.runningKernels = [kernel1, kernel2, kernel3];
store.kernelMapping = new Map([
["foo.py", kernel1],
["bar.py", kernel1],
["baz.py", kernel2],
["foo.md", new Map([["python", kernel1], ["javascript", kernel3]])]
]);
const kernelFiles = new Set(["foo.py", "bar.py", "foo.md"]);
expect(new Set(store.getFilesForKernel(kernel1))).toEqual(kernelFiles);
expect(store.kernelMapping.size).toBe(4);
store.deleteKernel(kernel1);
expect(store.kernelMapping.size).toBe(2);
expect(store.kernelMapping.get("baz.py")).toEqual(kernel2);
expect(store.kernelMapping.get("foo.md").toJS()).toEqual(
new Map([["javascript", kernel3]])
);
expect(store.runningKernels).toEqual([kernel2, kernel3]);
});
});
describe("getFilesForKernel", () => {
it("should return files related to kernel", () => {
const kernel1 = new Kernel(
new KernelTransport(
{
display_name: "Python 3",
language: "python"
},
{ name: "python" }
)
);
const kernel2 = new Kernel(
new KernelTransport(
{
display_name: "Python 3",
language: "python"
},
{ name: "python" }
)
);
const kernel3 = new Kernel(
new KernelTransport(
{
display_name: "JS",
language: "Javascript"
},
{ name: "javascript" }
)
);
store.kernelMapping = new Map([
["foo.py", kernel1],
["bar.py", kernel1],
["baz.py", kernel2],
["foo.md", new Map([["python", kernel1], ["javascript", kernel2]])]
]);
expect(store.getFilesForKernel(kernel1)).toEqual([
"foo.py",
"bar.py",
"foo.md"
]);
});
});
describe("updateEditor", () => {
it("should update editor", () => {
spyOn(store, "setGrammar").and.callThrough();
expect(store.editor).toBeNull();
const editor = atom.workspace.buildTextEditor();
store.updateEditor(editor);
expect(store.editor).toBe(editor);
expect(store.setGrammar).toHaveBeenCalledWith(editor);
expect(store.grammar).toEqual(editor.getGrammar());
expect(store.grammar.name.toLowerCase()).toBe("null grammar");
});
});
describe("dispose", () => {
it("should dispose kernels and subscriptions", () => {
spyOn(store.subscriptions, "dispose");
spyOn(store.markers, "clear");
const kernel1 = jasmine.createSpyObj("kernel1", ["destroy"]);
const kernel2 = jasmine.createSpyObj("kernel2", ["destroy"]);
store.runningKernels = [kernel1, kernel2];
store.dispose();
expect(store.runningKernels.length).toEqual(0);
expect(store.kernelMapping.size).toBe(0);
expect(kernel1.destroy).toHaveBeenCalled();
expect(kernel2.destroy).toHaveBeenCalled();
expect(store.subscriptions.dispose).toHaveBeenCalled();
expect(store.markers.clear).toHaveBeenCalled();
});
});
describe("get filePath", () => {
it("should return null if no editor", () => {
expect(store.filePath).toBeNull();
});
it("should return file path", () => {
store.editor = { getPath: () => "foo.py" };
expect(store.filePath).toBe("foo.py");
});
});
describe("get notebook", () => {
// runAsync is borrowed and modified from link below.
// https://github.com/jasmine/jasmine/issues/923#issuecomment-169634461
// This is duplicated from code-manager-spec.js
function waitAsync(fn) {
return done => {
fn().then(done, function rejected(e) {
fail(e);
done();
});
};
}
let editor;
beforeEach(
waitAsync(async () => {
editor = atom.workspace.buildTextEditor();
await atom.packages.activatePackage("language-python");
editor.setGrammar(atom.grammars.grammarForScopeName("source.python"));
})
);
it("should return null if no editor", () => {
expect(store.notebook).toBeNull();
});
it("should return an empty notebook for empty file", () => {
store.updateEditor(editor);
// Build a notebook with one code cell.
const nb = commutable.emptyNotebook;
expect(store.notebook).toEqual(commutable.toJS(nb));
});
it("should return a fully-fledged notebook when the file isn't empty", () => {
const source1 = 'print("Hola World! I <3 ZMQ!")\n';
const source2 = "2 + 2\n";
editor.setText(`# %%\n${source1}# %%\n${source2}`);
store.updateEditor(editor);
const codeCell1 = commutable.emptyCodeCell.set("source", source1);
const codeCell2 = commutable.emptyCodeCell.set("source", source2);
// The outputted notebook will have three cells because currently a cell
// is always created before the first `# %%`
let nb = commutable.appendCellToNotebook(
commutable.emptyNotebook,
codeCell1
);
nb = commutable.appendCellToNotebook(nb, codeCell2);
expect(store.notebook).toEqual(commutable.toJS(nb));
});
it("should export markdown to markdown cells", () => {
const source1 = 'print("Hola World! I <3 ZMQ!")\n';
const source2 = "2 + 2\n";
editor.setText(`# %%\n${source1}# %% markdown\n${source2}`);
store.updateEditor(editor);
const codeCell = commutable.emptyCodeCell.set("source", source1);
const markdownCell = commutable.emptyMarkdownCell.set("source", source2);
// The outputted notebook will have three cells because currently a cell
// is always created before the first `# %%`
let nb = commutable.appendCellToNotebook(
commutable.emptyNotebook,
codeCell
);
nb = commutable.appendCellToNotebook(nb, markdownCell);
expect(store.notebook).toEqual(commutable.toJS(nb));
});
});
describe("get kernel", () => {
it("should return null if no editor", () => {
expect(store.kernel).toBeNull();
});
it("should return null if editor isn't saved", () => {
store.editor = { getPath: () => {} };
expect(store.kernel).toBeNull();
});
it("should return kernel", () => {
store.editor = { getPath: () => "foo.py" };
const kernel = new Kernel(
new KernelTransport({
display_name: "Python 3",
language: "python"
})
);
store.kernelMapping = new Map([["foo.py", kernel]]);
expect(store.kernel).toEqual(kernel);
});
it("should return null if no kernel for file", () => {
store.editor = { getPath: () => "foo.py" };
const kernel = new Kernel(
new KernelTransport({
display_name: "Python 3",
language: "python"
})
);
store.kernelMapping = new Map([["bar.py", kernel]]);
expect(store.kernel).toBeUndefined();
});
it("should return the correct kernel for multilanguage files", () => {
store.editor = { getPath: () => "foo.md" };
const kernel = new Kernel(
new KernelTransport({
display_name: "Python 3",
language: "python"
})
);
store.kernelMapping = new Map([
["foo.md", new Map([["python", kernel]])]
]);
store.grammar = { name: "python" };
expect(store.kernel).toEqual(kernel);
});
});
describe("global mode", () => {
let mockStore, kernelSpec, grammar, editor1, editor2, kernel1;
beforeEach(() => {
atom.config.set("Hydrogen.globalMode", true);
mockStore = new Store();
kernelSpec = { language: "python", display_name: "Python 3" };
grammar = {
scopeName: "source.python",
name: "Python"
};
editor1 = atom.workspace.buildTextEditor();
editor2 = atom.workspace.buildTextEditor();
spyOn(editor1, "getPath").and.returnValue("foo.py");
spyOn(editor2, "getPath").and.returnValue("bar.py");
spyOn(editor1, "getGrammar").and.returnValue(grammar);
spyOn(editor2, "getGrammar").and.returnValue(grammar);
kernel1 = new Kernel(new KernelTransport(kernelSpec, grammar));
});
afterEach(() => {
atom.config.set("Hydrogen.globalMode", false);
});
it("should use the same kernel if two files have the same grammar", () => {
mockStore.updateEditor(editor1);
mockStore.newKernel(kernel1, editor1.getPath(), editor1);
expect(mockStore.kernel).toEqual(kernel1);
mockStore.updateEditor(editor2);
expect(mockStore.kernel).toEqual(kernel1);
// store should still keep track of all filePaths
expect(mockStore.filePaths).toEqual(["foo.py", "bar.py"]);
expect(mockStore.getFilesForKernel(mockStore.kernel)).toEqual([
"foo.py",
"bar.py"
]);
});
});
});