Skip to content

Commit

Permalink
some integration tests for notebook editing, #105283
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Sep 10, 2020
1 parent 6e02276 commit 7fdda46
Showing 1 changed file with 132 additions and 4 deletions.
136 changes: 132 additions & 4 deletions extensions/vscode-notebook-tests/src/notebook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ suite('Notebook API tests', () => {
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');

await vscode.notebook.activeNotebookEditor!.edit(editBuilder => {
editBuilder.replaceOutput(0, [{ outputKind: vscode.CellOutputKind.Rich, data: { foo: 'bar' } }]);
editBuilder.replaceCellOutput(0, [{ outputKind: vscode.CellOutputKind.Rich, data: { foo: 'bar' } }]);
});

const document = vscode.notebook.activeNotebookEditor?.document!;
Expand All @@ -442,7 +442,7 @@ suite('Notebook API tests', () => {

const outputChangeEvent = getEventOncePromise<vscode.NotebookCellOutputsChangeEvent>(vscode.notebook.onDidChangeCellOutputs);
await vscode.notebook.activeNotebookEditor!.edit(editBuilder => {
editBuilder.replaceOutput(0, [{ outputKind: vscode.CellOutputKind.Rich, data: { foo: 'bar' } }]);
editBuilder.replaceCellOutput(0, [{ outputKind: vscode.CellOutputKind.Rich, data: { foo: 'bar' } }]);
});

const value = await outputChangeEvent;
Expand All @@ -462,7 +462,7 @@ suite('Notebook API tests', () => {
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');

await vscode.notebook.activeNotebookEditor!.edit(editBuilder => {
editBuilder.replaceMetadata(0, { inputCollapsed: true, executionOrder: 17 });
editBuilder.replaceCellMetadata(0, { inputCollapsed: true, executionOrder: 17 });
});

const document = vscode.notebook.activeNotebookEditor?.document!;
Expand All @@ -483,7 +483,7 @@ suite('Notebook API tests', () => {
const event = getEventOncePromise<vscode.NotebookCellMetadataChangeEvent>(vscode.notebook.onDidChangeCellMetadata);

await vscode.notebook.activeNotebookEditor!.edit(editBuilder => {
editBuilder.replaceMetadata(0, { inputCollapsed: true, executionOrder: 17 });
editBuilder.replaceCellMetadata(0, { inputCollapsed: true, executionOrder: 17 });
});

const data = await event;
Expand All @@ -495,6 +495,134 @@ suite('Notebook API tests', () => {
await saveFileAndCloseAll(resource);
});

test('workspace edit API (replaceCells)', async function () {

assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');

const { document } = vscode.notebook.activeNotebookEditor!;
assert.strictEqual(document.cells.length, 1);

// inserting two new cells
{
const edit = new vscode.WorkspaceEdit();
edit.replaceCells(document.uri, 0, 0, [{
cellKind: vscode.CellKind.Markdown,
language: 'markdown',
metadata: undefined,
outputs: [],
source: 'new_markdown'
}, {
cellKind: vscode.CellKind.Code,
language: 'fooLang',
metadata: undefined,
outputs: [],
source: 'new_code'
}]);

const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, true);
}

assert.strictEqual(document.cells.length, 3);
assert.strictEqual(document.cells[0].document.getText(), 'new_markdown');
assert.strictEqual(document.cells[1].document.getText(), 'new_code');

// deleting cell 1 and 3
{
const edit = new vscode.WorkspaceEdit();
edit.replaceCells(document.uri, 0, 1, []);
edit.replaceCells(document.uri, 2, 3, []);
const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, true);
}

assert.strictEqual(document.cells.length, 1);
assert.strictEqual(document.cells[0].document.getText(), 'new_code');

// replacing all cells
{
const edit = new vscode.WorkspaceEdit();
edit.replaceCells(document.uri, 0, 1, [{
cellKind: vscode.CellKind.Markdown,
language: 'markdown',
metadata: undefined,
outputs: [],
source: 'new2_markdown'
}, {
cellKind: vscode.CellKind.Code,
language: 'fooLang',
metadata: undefined,
outputs: [],
source: 'new2_code'
}]);
const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, true);
}
assert.strictEqual(document.cells.length, 2);
assert.strictEqual(document.cells[0].document.getText(), 'new2_markdown');
assert.strictEqual(document.cells[1].document.getText(), 'new2_code');

// remove all cells
{
const edit = new vscode.WorkspaceEdit();
edit.replaceCells(document.uri, 0, document.cells.length, []);
const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, true);
}
assert.strictEqual(document.cells.length, 0);

await saveFileAndCloseAll(resource);
});

test('workspace edit API (replaceCells, event)', async function () {

assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');

const { document } = vscode.notebook.activeNotebookEditor!;
assert.strictEqual(document.cells.length, 1);

const edit = new vscode.WorkspaceEdit();
edit.replaceCells(document.uri, 0, 0, [{
cellKind: vscode.CellKind.Markdown,
language: 'markdown',
metadata: undefined,
outputs: [],
source: 'new_markdown'
}, {
cellKind: vscode.CellKind.Code,
language: 'fooLang',
metadata: undefined,
outputs: [],
source: 'new_code'
}]);

const event = getEventOncePromise<vscode.NotebookCellsChangeEvent>(vscode.notebook.onDidChangeNotebookCells);

const success = await vscode.workspace.applyEdit(edit);
assert.strictEqual(success, true);

const data = await event;

// check document
assert.strictEqual(document.cells.length, 3);
assert.strictEqual(document.cells[0].document.getText(), 'new_markdown');
assert.strictEqual(document.cells[1].document.getText(), 'new_code');

// check event data
assert.strictEqual(data.document === document, true);
assert.strictEqual(data.changes.length, 1);
assert.strictEqual(data.changes[0].deletedCount, 0);
assert.strictEqual(data.changes[0].deletedItems.length, 0);
assert.strictEqual(data.changes[0].items.length, 2);
assert.strictEqual(data.changes[0].items[0], document.cells[0]);
assert.strictEqual(data.changes[0].items[1], document.cells[1]);
await saveFileAndCloseAll(resource);
});

test('initialzation should not emit cell change events.', async function () {
assertInitalState();
const resource = await createRandomFile('', undefined, 'first', '.vsctestnb');
Expand Down

0 comments on commit 7fdda46

Please sign in to comment.