Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookBrowser.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 { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
8
import { ICellViewModel } from '../../browser/notebookBrowser.js';
9
import { CellKind } from '../../common/notebookCommon.js';
10
import { ICellRange } from '../../common/notebookRange.js';
11
12
/**
13
* Return a set of ranges for the cells matching the given predicate
14
*/
15
function getRanges(cells: ICellViewModel[], included: (cell: ICellViewModel) => boolean): ICellRange[] {
16
const ranges: ICellRange[] = [];
17
let currentRange: ICellRange | undefined;
18
19
cells.forEach((cell, idx) => {
20
if (included(cell)) {
21
if (!currentRange) {
22
currentRange = { start: idx, end: idx + 1 };
23
ranges.push(currentRange);
24
} else {
25
currentRange.end = idx + 1;
26
}
27
} else {
28
currentRange = undefined;
29
}
30
});
31
32
return ranges;
33
}
34
35
36
suite('notebookBrowser', () => {
37
ensureNoDisposablesAreLeakedInTestSuite();
38
39
suite('getRanges', function () {
40
const predicate = (cell: ICellViewModel) => cell.cellKind === CellKind.Code;
41
42
test('all code', function () {
43
const cells = [
44
{ cellKind: CellKind.Code },
45
{ cellKind: CellKind.Code },
46
];
47
assert.deepStrictEqual(getRanges(cells as ICellViewModel[], predicate), [{ start: 0, end: 2 }]);
48
});
49
50
test('none code', function () {
51
const cells = [
52
{ cellKind: CellKind.Markup },
53
{ cellKind: CellKind.Markup },
54
];
55
assert.deepStrictEqual(getRanges(cells as ICellViewModel[], predicate), []);
56
});
57
58
test('start code', function () {
59
const cells = [
60
{ cellKind: CellKind.Code },
61
{ cellKind: CellKind.Markup },
62
];
63
assert.deepStrictEqual(getRanges(cells as ICellViewModel[], predicate), [{ start: 0, end: 1 }]);
64
});
65
66
test('random', function () {
67
const cells = [
68
{ cellKind: CellKind.Code },
69
{ cellKind: CellKind.Code },
70
{ cellKind: CellKind.Markup },
71
{ cellKind: CellKind.Code },
72
{ cellKind: CellKind.Markup },
73
{ cellKind: CellKind.Markup },
74
{ cellKind: CellKind.Code },
75
];
76
assert.deepStrictEqual(getRanges(cells as ICellViewModel[], predicate), [{ start: 0, end: 2 }, { start: 3, end: 4 }, { start: 6, end: 7 }]);
77
});
78
});
79
});
80
81