Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookCommon.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { DisposableStore } from '../../../../../base/common/lifecycle.js';
8
import { Mimes } from '../../../../../base/common/mime.js';
9
import { URI } from '../../../../../base/common/uri.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
11
import { ILanguageService } from '../../../../../editor/common/languages/language.js';
12
import { TestInstantiationService } from '../../../../../platform/instantiation/test/common/instantiationServiceMock.js';
13
import { CellKind, CellUri, diff, MimeTypeDisplayOrder, NotebookWorkingCopyTypeIdentifier } from '../../common/notebookCommon.js';
14
import { cellIndexesToRanges, cellRangesToIndexes, reduceCellRanges } from '../../common/notebookRange.js';
15
import { setupInstantiationService, TestCell } from './testNotebookEditor.js';
16
17
suite('NotebookCommon', () => {
18
ensureNoDisposablesAreLeakedInTestSuite();
19
20
let disposables: DisposableStore;
21
let instantiationService: TestInstantiationService;
22
let languageService: ILanguageService;
23
24
setup(() => {
25
disposables = new DisposableStore();
26
instantiationService = setupInstantiationService(disposables);
27
languageService = instantiationService.get(ILanguageService);
28
});
29
30
test('sortMimeTypes default orders', function () {
31
assert.deepStrictEqual(new MimeTypeDisplayOrder().sort(
32
[
33
'application/json',
34
'application/javascript',
35
'text/html',
36
'image/svg+xml',
37
Mimes.latex,
38
Mimes.markdown,
39
'image/png',
40
'image/jpeg',
41
Mimes.text
42
]),
43
[
44
'application/json',
45
'application/javascript',
46
'text/html',
47
'image/svg+xml',
48
Mimes.latex,
49
Mimes.markdown,
50
'image/png',
51
'image/jpeg',
52
Mimes.text
53
]
54
);
55
56
assert.deepStrictEqual(new MimeTypeDisplayOrder().sort(
57
[
58
'application/json',
59
Mimes.latex,
60
Mimes.markdown,
61
'application/javascript',
62
'text/html',
63
Mimes.text,
64
'image/png',
65
'image/jpeg',
66
'image/svg+xml'
67
]),
68
[
69
'application/json',
70
'application/javascript',
71
'text/html',
72
'image/svg+xml',
73
Mimes.latex,
74
Mimes.markdown,
75
'image/png',
76
'image/jpeg',
77
Mimes.text
78
]
79
);
80
81
assert.deepStrictEqual(new MimeTypeDisplayOrder().sort(
82
[
83
Mimes.markdown,
84
'application/json',
85
Mimes.text,
86
'image/jpeg',
87
'application/javascript',
88
'text/html',
89
'image/png',
90
'image/svg+xml'
91
]),
92
[
93
'application/json',
94
'application/javascript',
95
'text/html',
96
'image/svg+xml',
97
Mimes.markdown,
98
'image/png',
99
'image/jpeg',
100
Mimes.text
101
]
102
);
103
104
disposables.dispose();
105
});
106
107
108
109
test('sortMimeTypes user orders', function () {
110
assert.deepStrictEqual(
111
new MimeTypeDisplayOrder([
112
'image/png',
113
Mimes.text,
114
Mimes.markdown,
115
'text/html',
116
'application/json'
117
]).sort(
118
[
119
'application/json',
120
'application/javascript',
121
'text/html',
122
'image/svg+xml',
123
Mimes.markdown,
124
'image/png',
125
'image/jpeg',
126
Mimes.text
127
]
128
),
129
[
130
'image/png',
131
Mimes.text,
132
Mimes.markdown,
133
'text/html',
134
'application/json',
135
'application/javascript',
136
'image/svg+xml',
137
'image/jpeg',
138
]
139
);
140
141
assert.deepStrictEqual(
142
new MimeTypeDisplayOrder([
143
'application/json',
144
'text/html',
145
'text/html',
146
Mimes.markdown,
147
'application/json'
148
]).sort([
149
Mimes.markdown,
150
'application/json',
151
Mimes.text,
152
'application/javascript',
153
'text/html',
154
'image/svg+xml',
155
'image/jpeg',
156
'image/png'
157
]),
158
[
159
'application/json',
160
'text/html',
161
Mimes.markdown,
162
'application/javascript',
163
'image/svg+xml',
164
'image/png',
165
'image/jpeg',
166
Mimes.text
167
]
168
);
169
170
disposables.dispose();
171
});
172
173
test('prioritizes mimetypes', () => {
174
const m = new MimeTypeDisplayOrder([
175
Mimes.markdown,
176
'text/html',
177
'application/json'
178
]);
179
assert.deepStrictEqual(m.toArray(), [Mimes.markdown, 'text/html', 'application/json']);
180
181
// no-op if already in the right order
182
m.prioritize('text/html', ['application/json']);
183
assert.deepStrictEqual(m.toArray(), [Mimes.markdown, 'text/html', 'application/json']);
184
185
// sorts to highest priority
186
m.prioritize('text/html', ['application/json', Mimes.markdown]);
187
assert.deepStrictEqual(m.toArray(), ['text/html', Mimes.markdown, 'application/json']);
188
189
// adds in new type
190
m.prioritize('text/plain', ['application/json', Mimes.markdown]);
191
assert.deepStrictEqual(m.toArray(), ['text/plain', 'text/html', Mimes.markdown, 'application/json']);
192
193
// moves multiple, preserves order
194
m.prioritize(Mimes.markdown, ['text/plain', 'application/json', Mimes.markdown]);
195
assert.deepStrictEqual(m.toArray(), ['text/html', Mimes.markdown, 'text/plain', 'application/json']);
196
197
// deletes multiple
198
m.prioritize('text/plain', ['text/plain', 'text/html', Mimes.markdown]);
199
assert.deepStrictEqual(m.toArray(), ['text/plain', 'text/html', Mimes.markdown, 'application/json']);
200
201
// handles multiple mimetypes, unknown mimetype
202
const m2 = new MimeTypeDisplayOrder(['a', 'b']);
203
m2.prioritize('b', ['a', 'b', 'a', 'q']);
204
assert.deepStrictEqual(m2.toArray(), ['b', 'a']);
205
206
disposables.dispose();
207
});
208
209
test('sortMimeTypes glob', function () {
210
assert.deepStrictEqual(
211
new MimeTypeDisplayOrder([
212
'application/vnd-vega*',
213
Mimes.markdown,
214
'text/html',
215
'application/json'
216
]).sort(
217
[
218
'application/json',
219
'application/javascript',
220
'text/html',
221
'application/vnd-plot.json',
222
'application/vnd-vega.json'
223
]
224
),
225
[
226
'application/vnd-vega.json',
227
'text/html',
228
'application/json',
229
'application/vnd-plot.json',
230
'application/javascript',
231
],
232
'glob *'
233
);
234
235
disposables.dispose();
236
});
237
238
test('diff cells', function () {
239
const cells: TestCell[] = [];
240
241
for (let i = 0; i < 5; i++) {
242
cells.push(
243
disposables.add(new TestCell('notebook', i, `var a = ${i};`, 'javascript', CellKind.Code, [], languageService))
244
);
245
}
246
247
assert.deepStrictEqual(diff<TestCell>(cells, [], (cell) => {
248
return cells.indexOf(cell) > -1;
249
}), [
250
{
251
start: 0,
252
deleteCount: 5,
253
toInsert: []
254
}
255
]
256
);
257
258
assert.deepStrictEqual(diff<TestCell>([], cells, (cell) => {
259
return false;
260
}), [
261
{
262
start: 0,
263
deleteCount: 0,
264
toInsert: cells
265
}
266
]
267
);
268
269
const cellA = disposables.add(new TestCell('notebook', 6, 'var a = 6;', 'javascript', CellKind.Code, [], languageService));
270
const cellB = disposables.add(new TestCell('notebook', 7, 'var a = 7;', 'javascript', CellKind.Code, [], languageService));
271
272
const modifiedCells = [
273
cells[0],
274
cells[1],
275
cellA,
276
cells[3],
277
cellB,
278
cells[4]
279
];
280
281
const splices = diff<TestCell>(cells, modifiedCells, (cell) => {
282
return cells.indexOf(cell) > -1;
283
});
284
285
assert.deepStrictEqual(splices,
286
[
287
{
288
start: 2,
289
deleteCount: 1,
290
toInsert: [cellA]
291
},
292
{
293
start: 4,
294
deleteCount: 0,
295
toInsert: [cellB]
296
}
297
]
298
);
299
300
disposables.dispose();
301
});
302
303
});
304
305
306
suite('CellUri', function () {
307
308
ensureNoDisposablesAreLeakedInTestSuite();
309
310
test('parse, generate (file-scheme)', function () {
311
312
const nb = URI.parse('file:///bar/følder/file.nb');
313
const id = 17;
314
315
const data = CellUri.generate(nb, id);
316
const actual = CellUri.parse(data);
317
assert.ok(Boolean(actual));
318
assert.strictEqual(actual?.handle, id);
319
assert.strictEqual(actual?.notebook.toString(), nb.toString());
320
});
321
322
test('parse, generate (foo-scheme)', function () {
323
324
const nb = URI.parse('foo:///bar/følder/file.nb');
325
const id = 17;
326
327
const data = CellUri.generate(nb, id);
328
const actual = CellUri.parse(data);
329
assert.ok(Boolean(actual));
330
assert.strictEqual(actual?.handle, id);
331
assert.strictEqual(actual?.notebook.toString(), nb.toString());
332
});
333
334
test('stable order', function () {
335
336
const nb = URI.parse('foo:///bar/følder/file.nb');
337
const handles = [1, 2, 9, 10, 88, 100, 666666, 7777777];
338
339
const uris = handles.map(h => CellUri.generate(nb, h)).sort();
340
341
const strUris = uris.map(String).sort();
342
const parsedUris = strUris.map(s => URI.parse(s));
343
344
const actual = parsedUris.map(u => CellUri.parse(u)?.handle);
345
346
assert.deepStrictEqual(actual, handles);
347
});
348
});
349
350
351
suite('CellRange', function () {
352
353
ensureNoDisposablesAreLeakedInTestSuite();
354
355
test('Cell range to index', function () {
356
assert.deepStrictEqual(cellRangesToIndexes([]), []);
357
assert.deepStrictEqual(cellRangesToIndexes([{ start: 0, end: 0 }]), []);
358
assert.deepStrictEqual(cellRangesToIndexes([{ start: 0, end: 1 }]), [0]);
359
assert.deepStrictEqual(cellRangesToIndexes([{ start: 0, end: 2 }]), [0, 1]);
360
assert.deepStrictEqual(cellRangesToIndexes([{ start: 0, end: 2 }, { start: 2, end: 3 }]), [0, 1, 2]);
361
assert.deepStrictEqual(cellRangesToIndexes([{ start: 0, end: 2 }, { start: 3, end: 4 }]), [0, 1, 3]);
362
});
363
364
test('Cell index to range', function () {
365
assert.deepStrictEqual(cellIndexesToRanges([]), []);
366
assert.deepStrictEqual(cellIndexesToRanges([0]), [{ start: 0, end: 1 }]);
367
assert.deepStrictEqual(cellIndexesToRanges([0, 1]), [{ start: 0, end: 2 }]);
368
assert.deepStrictEqual(cellIndexesToRanges([0, 1, 2]), [{ start: 0, end: 3 }]);
369
assert.deepStrictEqual(cellIndexesToRanges([0, 1, 3]), [{ start: 0, end: 2 }, { start: 3, end: 4 }]);
370
371
assert.deepStrictEqual(cellIndexesToRanges([1, 0]), [{ start: 0, end: 2 }]);
372
assert.deepStrictEqual(cellIndexesToRanges([1, 2, 0]), [{ start: 0, end: 3 }]);
373
assert.deepStrictEqual(cellIndexesToRanges([3, 1, 0]), [{ start: 0, end: 2 }, { start: 3, end: 4 }]);
374
375
assert.deepStrictEqual(cellIndexesToRanges([9, 10]), [{ start: 9, end: 11 }]);
376
assert.deepStrictEqual(cellIndexesToRanges([10, 9]), [{ start: 9, end: 11 }]);
377
});
378
379
test('Reduce ranges', function () {
380
assert.deepStrictEqual(reduceCellRanges([{ start: 0, end: 1 }, { start: 1, end: 2 }]), [{ start: 0, end: 2 }]);
381
assert.deepStrictEqual(reduceCellRanges([{ start: 0, end: 2 }, { start: 1, end: 3 }]), [{ start: 0, end: 3 }]);
382
assert.deepStrictEqual(reduceCellRanges([{ start: 1, end: 3 }, { start: 0, end: 2 }]), [{ start: 0, end: 3 }]);
383
assert.deepStrictEqual(reduceCellRanges([{ start: 0, end: 2 }, { start: 4, end: 5 }]), [{ start: 0, end: 2 }, { start: 4, end: 5 }]);
384
385
assert.deepStrictEqual(reduceCellRanges([
386
{ start: 0, end: 1 },
387
{ start: 1, end: 2 },
388
{ start: 4, end: 6 }
389
]), [
390
{ start: 0, end: 2 },
391
{ start: 4, end: 6 }
392
]);
393
394
assert.deepStrictEqual(reduceCellRanges([
395
{ start: 0, end: 1 },
396
{ start: 1, end: 3 },
397
{ start: 3, end: 4 }
398
]), [
399
{ start: 0, end: 4 }
400
]);
401
});
402
403
test('Reduce ranges 2, empty ranges', function () {
404
assert.deepStrictEqual(reduceCellRanges([{ start: 0, end: 0 }, { start: 0, end: 0 }]), [{ start: 0, end: 0 }]);
405
assert.deepStrictEqual(reduceCellRanges([{ start: 0, end: 0 }, { start: 1, end: 2 }]), [{ start: 1, end: 2 }]);
406
assert.deepStrictEqual(reduceCellRanges([{ start: 2, end: 2 }]), [{ start: 2, end: 2 }]);
407
});
408
});
409
410
suite('NotebookWorkingCopyTypeIdentifier', function () {
411
ensureNoDisposablesAreLeakedInTestSuite();
412
413
test('supports notebook type only', function () {
414
const viewType = 'testViewType';
415
const type = NotebookWorkingCopyTypeIdentifier.create(viewType);
416
assert.deepEqual(NotebookWorkingCopyTypeIdentifier.parse(type), { notebookType: viewType, viewType });
417
assert.strictEqual(NotebookWorkingCopyTypeIdentifier.parse('something'), undefined);
418
});
419
420
test('supports different viewtype', function () {
421
const notebookType = { notebookType: 'testNotebookType', viewType: 'testViewType' };
422
const type = NotebookWorkingCopyTypeIdentifier.create(notebookType.notebookType, notebookType.viewType);
423
assert.deepEqual(NotebookWorkingCopyTypeIdentifier.parse(type), notebookType);
424
assert.strictEqual(NotebookWorkingCopyTypeIdentifier.parse('something'), undefined);
425
});
426
});
427
428