Skip to content

Commit cf5dfe1

Browse files
committed
unit test for applying metadata and output, #105283
1 parent 88664e2 commit cf5dfe1

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
252252
break;
253253
case CellEditType.Output:
254254
//TODO@joh,@rebornix no event, no undo stop (?)
255+
this.assertIndex(edit.index);
255256
const cell = this.cells[edit.index];
256257
this.spliceNotebookCellOutputs(cell.handle, [[0, cell.outputs.length, edit.outputs]]);
257258
break;
258259
case CellEditType.Metadata:
260+
this.assertIndex(edit.index);
259261
this.changeCellMetadata(this.cells[edit.index].handle, edit.metadata);
260262
break;
261263
}

src/vs/workbench/contrib/notebook/test/notebookTextModel.test.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as assert from 'assert';
7-
import { CellKind, CellEditType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
7+
import { CellKind, CellEditType, CellOutputKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
88
import { withTestNotebook, TestCell, setupInstantiationService } from 'vs/workbench/contrib/notebook/test/testNotebookEditor';
99
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
1010
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
@@ -139,4 +139,89 @@ suite('NotebookTextModel', () => {
139139
}
140140
);
141141
});
142+
143+
test('output', function () {
144+
withTestNotebook(
145+
instantiationService,
146+
blukEditService,
147+
undoRedoService,
148+
[
149+
['var a = 1;', 'javascript', CellKind.Code, [], { editable: true }],
150+
],
151+
(editor, viewModel, textModel) => {
152+
153+
// invalid index 1
154+
assert.throws(() => {
155+
textModel.applyEdit(textModel.versionId, [{
156+
index: Number.MAX_VALUE,
157+
editType: CellEditType.Output,
158+
outputs: []
159+
}], true);
160+
});
161+
162+
// invalid index 2
163+
assert.throws(() => {
164+
textModel.applyEdit(textModel.versionId, [{
165+
index: -1,
166+
editType: CellEditType.Output,
167+
outputs: []
168+
}], true);
169+
});
170+
171+
textModel.applyEdit(textModel.versionId, [{
172+
index: 0,
173+
editType: CellEditType.Output,
174+
outputs: [{
175+
outputKind: CellOutputKind.Rich,
176+
outputId: 'someId',
177+
data: { 'text/markdown': '_Hello_' }
178+
}]
179+
}], true);
180+
181+
assert.equal(textModel.cells.length, 1);
182+
assert.equal(textModel.cells[0].outputs.length, 1);
183+
assert.equal(textModel.cells[0].outputs[0].outputKind, CellOutputKind.Rich);
184+
}
185+
);
186+
});
187+
188+
test('metadata', function () {
189+
withTestNotebook(
190+
instantiationService,
191+
blukEditService,
192+
undoRedoService,
193+
[
194+
['var a = 1;', 'javascript', CellKind.Code, [], { editable: true }],
195+
],
196+
(editor, viewModel, textModel) => {
197+
198+
// invalid index 1
199+
assert.throws(() => {
200+
textModel.applyEdit(textModel.versionId, [{
201+
index: Number.MAX_VALUE,
202+
editType: CellEditType.Metadata,
203+
metadata: { editable: false }
204+
}], true);
205+
});
206+
207+
// invalid index 2
208+
assert.throws(() => {
209+
textModel.applyEdit(textModel.versionId, [{
210+
index: -1,
211+
editType: CellEditType.Metadata,
212+
metadata: { editable: false }
213+
}], true);
214+
});
215+
216+
textModel.applyEdit(textModel.versionId, [{
217+
index: 0,
218+
editType: CellEditType.Metadata,
219+
metadata: { editable: false },
220+
}], true);
221+
222+
assert.equal(textModel.cells.length, 1);
223+
assert.equal(textModel.cells[0].metadata?.editable, false);
224+
}
225+
);
226+
});
142227
});

src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,15 @@ export function withTestNotebook(instantiationService: TestInstantiationService,
381381
const viewType = 'notebook';
382382
const editor = new TestNotebookEditor();
383383
const notebook = new NotebookTextModel(0, viewType, false, URI.parse('test'), undoRedoService, textModelService);
384-
notebook.cells = cells.map((cell, index) => {
385-
return new NotebookCellTextModel(notebook.uri, index, cell[0], cell[1], cell[2], cell[3], cell[4], textModelService);
386-
});
384+
notebook.initialize(cells.map(cell => {
385+
return {
386+
source: cell[0],
387+
language: cell[1],
388+
cellKind: cell[2],
389+
outputs: cell[3],
390+
metadata: cell[4]
391+
};
392+
}));
387393
const model = new NotebookEditorTestModel(notebook);
388394
const eventDispatcher = new NotebookEventDispatcher();
389395
const viewModel = new NotebookViewModel(viewType, model.notebook, eventDispatcher, null, instantiationService, blukEditService, undoRedoService);

0 commit comments

Comments
 (0)