Path: blob/main/src/vs/workbench/contrib/notebook/test/browser/notebookBrowser.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';7import { ICellViewModel } from '../../browser/notebookBrowser.js';8import { CellKind } from '../../common/notebookCommon.js';9import { ICellRange } from '../../common/notebookRange.js';1011/**12* Return a set of ranges for the cells matching the given predicate13*/14function getRanges(cells: ICellViewModel[], included: (cell: ICellViewModel) => boolean): ICellRange[] {15const ranges: ICellRange[] = [];16let currentRange: ICellRange | undefined;1718cells.forEach((cell, idx) => {19if (included(cell)) {20if (!currentRange) {21currentRange = { start: idx, end: idx + 1 };22ranges.push(currentRange);23} else {24currentRange.end = idx + 1;25}26} else {27currentRange = undefined;28}29});3031return ranges;32}333435suite('notebookBrowser', () => {36ensureNoDisposablesAreLeakedInTestSuite();3738suite('getRanges', function () {39const predicate = (cell: ICellViewModel) => cell.cellKind === CellKind.Code;4041test('all code', function () {42const cells = [43{ cellKind: CellKind.Code },44{ cellKind: CellKind.Code },45];46assert.deepStrictEqual(getRanges(cells as ICellViewModel[], predicate), [{ start: 0, end: 2 }]);47});4849test('none code', function () {50const cells = [51{ cellKind: CellKind.Markup },52{ cellKind: CellKind.Markup },53];54assert.deepStrictEqual(getRanges(cells as ICellViewModel[], predicate), []);55});5657test('start code', function () {58const cells = [59{ cellKind: CellKind.Code },60{ cellKind: CellKind.Markup },61];62assert.deepStrictEqual(getRanges(cells as ICellViewModel[], predicate), [{ start: 0, end: 1 }]);63});6465test('random', function () {66const cells = [67{ cellKind: CellKind.Code },68{ cellKind: CellKind.Code },69{ cellKind: CellKind.Markup },70{ cellKind: CellKind.Code },71{ cellKind: CellKind.Markup },72{ cellKind: CellKind.Markup },73{ cellKind: CellKind.Code },74];75assert.deepStrictEqual(getRanges(cells as ICellViewModel[], predicate), [{ start: 0, end: 2 }, { start: 3, end: 4 }, { start: 6, end: 7 }]);76});77});78});798081