Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amunger committed Oct 3, 2024
1 parent 3559536 commit 435876b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/client/repl/variables/variablesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class VariablesProvider implements NotebookVariableProvider {
const notebook = this.getNotebookDocument();
if (notebook) {
this.executionCount += 1;
if (isEnabled()) {
if (isEnabled(notebook.uri)) {
this._onDidChangeVariables.fire(notebook);
}
}
Expand All @@ -52,7 +52,12 @@ export class VariablesProvider implements NotebookVariableProvider {
token: CancellationToken,
): AsyncIterable<VariablesResult> {
const notebookDocument = this.getNotebookDocument();
if (!isEnabled() || token.isCancellationRequested || !notebookDocument || notebookDocument !== notebook) {
if (
!isEnabled(notebook.uri) ||
token.isCancellationRequested ||
!notebookDocument ||
notebookDocument !== notebook
) {
return;
}

Expand Down
63 changes: 61 additions & 2 deletions src/test/repl/variableProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@
// Licensed under the MIT License.

import { assert } from 'chai';
import { NotebookDocument, CancellationTokenSource, VariablesResult, Variable, EventEmitter } from 'vscode';
import sinon from 'sinon';
import {
NotebookDocument,
CancellationTokenSource,
VariablesResult,
Variable,
EventEmitter,
ConfigurationScope,
WorkspaceConfiguration,
} from 'vscode';
import * as TypeMoq from 'typemoq';
import { IVariableDescription } from '../../client/repl/variables/types';
import { VariablesProvider } from '../../client/repl/variables/variablesProvider';
import { VariableRequester } from '../../client/repl/variables/variableRequester';
import * as workspaceApis from '../../client/common/vscodeApis/workspaceApis';

suite('ReplVariablesProvider', () => {
suite.only('ReplVariablesProvider', () => {
let provider: VariablesProvider;
let varRequester: TypeMoq.IMock<VariableRequester>;
let notebook: TypeMoq.IMock<NotebookDocument>;
let getConfigurationStub: sinon.SinonStub;
let configMock: TypeMoq.IMock<WorkspaceConfiguration>;
let enabled: boolean;
const executionEventEmitter = new EventEmitter<void>();
const cancellationToken = new CancellationTokenSource().token;

Expand Down Expand Up @@ -68,9 +81,23 @@ suite('ReplVariablesProvider', () => {
}

setup(() => {
enabled = true;
varRequester = TypeMoq.Mock.ofType<VariableRequester>();
notebook = TypeMoq.Mock.ofType<NotebookDocument>();
provider = new VariablesProvider(varRequester.object, () => notebook.object, executionEventEmitter.event);
configMock = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
configMock.setup((c) => c.get<boolean>('REPL.provideVariables')).returns(() => enabled);
getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration');
getConfigurationStub.callsFake((section?: string, _scope?: ConfigurationScope | null) => {
if (section === 'python') {
return configMock.object;
}
return undefined;
});
});

teardown(() => {
sinon.restore();
});

test('provideVariables without parent should yield variables', async () => {
Expand All @@ -84,6 +111,38 @@ suite('ReplVariablesProvider', () => {
assert.equal(results[0].variable.expression, 'myObject');
});

test('No variables are returned when variable provider is disabled', async () => {
enabled = false;
setVariablesForParent(undefined, [objectVariable]);

const results = await provideVariables(undefined);

assert.isEmpty(results);
});

test('No change event from provider when disabled', async () => {
enabled = false;
let eventFired = false;
provider.onDidChangeVariables(() => {
eventFired = true;
});

executionEventEmitter.fire();

assert.isFalse(eventFired, 'event should not have fired');
});

test('Variables change event from provider should fire when execution happens', async () => {
let eventFired = false;
provider.onDidChangeVariables(() => {
eventFired = true;
});

executionEventEmitter.fire();

assert.isTrue(eventFired, 'event should have fired');
});

test('provideVariables with a parent should call get children correctly', async () => {
const listVariableItems = [0, 1, 2].map(createListItem);
setVariablesForParent(undefined, [objectVariable]);
Expand Down

0 comments on commit 435876b

Please sign in to comment.