Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonykim1 committed Nov 18, 2024
1 parent 140771b commit 418c1a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/client/repl/nativeRepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { VariablesProvider } from './variables/variablesProvider';
import { VariableRequester } from './variables/variableRequester';
import { getTabNameForUri } from './replUtils';

const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri';
export const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri';
let nativeRepl: NativeRepl | undefined;
export class NativeRepl implements Disposable {
// Adding ! since it will get initialized in create method, not the constructor.
Expand Down
35 changes: 29 additions & 6 deletions src/test/repl/nativeRepl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,39 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as TypeMoq from 'typemoq';
import * as sinon from 'sinon';
import { Disposable } from 'vscode';
import { Disposable, ExtensionContext } from 'vscode';
import { expect } from 'chai';

import { IInterpreterService } from '../../client/interpreter/contracts';
import { PythonEnvironment } from '../../client/pythonEnvironments/info';
import { getNativeRepl, NativeRepl } from '../../client/repl/nativeRepl';
import { getNativeRepl, NATIVE_REPL_URI_MEMENTO, NativeRepl } from '../../client/repl/nativeRepl';
import { IExtensionContext } from '../../client/common/types';
import * as replUtils from '../../client/repl/replUtils';

suite('REPL - Native REPL', () => {
let interpreterService: TypeMoq.IMock<IInterpreterService>;
let extensionContext: TypeMoq.IMock<IExtensionContext>;
let disposable: TypeMoq.IMock<Disposable>;
let disposableArray: Disposable[] = [];

let setReplDirectoryStub: sinon.SinonStub;
let setReplControllerSpy: sinon.SinonSpy;

let memento: TypeMoq.IMock<ExtensionContext['globalState']>;
let getTabNameForUriStub: sinon.SinonStub;
setup(() => {
interpreterService = TypeMoq.Mock.ofType<IInterpreterService>();
interpreterService
.setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny()))
.returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment));
disposable = TypeMoq.Mock.ofType<Disposable>();
disposableArray = [disposable.object];

memento = TypeMoq.Mock.ofType<ExtensionContext['globalState']>();
setReplDirectoryStub = sinon.stub(NativeRepl.prototype as any, 'setReplDirectory').resolves(); // Stubbing private method
// Use a spy instead of a stub for setReplController
getTabNameForUriStub = sinon.stub(replUtils, 'getTabNameForUri').returns('tabName');
setReplControllerSpy = sinon.spy(NativeRepl.prototype, 'setReplController');
extensionContext = TypeMoq.Mock.ofType<IExtensionContext>();
extensionContext.setup((c) => c.globalState).returns(() => memento.object);
memento.setup((m) => m.get(NATIVE_REPL_URI_MEMENTO)).returns(() => undefined);
});

teardown(() => {
Expand All @@ -39,9 +43,10 @@ suite('REPL - Native REPL', () => {
d.dispose();
}
});

disposableArray = [];
sinon.restore();
extensionContext?.reset();
memento?.reset();
});

test('getNativeRepl should call create constructor', async () => {
Expand All @@ -55,6 +60,24 @@ suite('REPL - Native REPL', () => {
expect(createMethodStub.calledOnce).to.be.true;
});

test('sendToNativeRepl with undefined URI should not try to reload', async () => {
memento.setup((m) => m.get(NATIVE_REPL_URI_MEMENTO)).returns(() => undefined);

interpreterService
.setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny()))
.returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment));
const interpreter = await interpreterService.object.getActiveInterpreter();
const nativeRepl = await getNativeRepl(
interpreter as PythonEnvironment,
disposableArray,
extensionContext.object,
);

nativeRepl.sendToNativeRepl(undefined, false);

expect(getTabNameForUriStub.notCalled).to.be.true;
});

test('create should call setReplDirectory, setReplController', async () => {
const interpreter = await interpreterService.object.getActiveInterpreter();
interpreterService
Expand Down

0 comments on commit 418c1a3

Please sign in to comment.