forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TensorBoard prompt and import tracking and add tests (#15073)
- Loading branch information
Showing
9 changed files
with
206 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as TypeMoq from 'typemoq'; | ||
import { IApplicationShell, ICommandManager } from '../../client/common/application/types'; | ||
import { IExperimentService, IPersistentStateFactory } from '../../client/common/types'; | ||
import { TensorBoardPrompt } from '../../client/tensorBoard/tensorBoardPrompt'; | ||
import { MockState } from '../interpreters/mocks'; | ||
|
||
export function createTensorBoardPromptWithMocks(): TensorBoardPrompt { | ||
const appShell = TypeMoq.Mock.ofType<IApplicationShell>(); | ||
const commandManager = TypeMoq.Mock.ofType<ICommandManager>(); | ||
const persistentStateFactory = TypeMoq.Mock.ofType<IPersistentStateFactory>(); | ||
const expService = TypeMoq.Mock.ofType<IExperimentService>(); | ||
const persistentState = new MockState(true); | ||
persistentStateFactory | ||
.setup((factory) => { | ||
factory.createWorkspacePersistentState(TypeMoq.It.isAny(), TypeMoq.It.isAny()); | ||
}) | ||
.returns(() => persistentState); | ||
return new TensorBoardPrompt( | ||
appShell.object, | ||
commandManager.object, | ||
persistentStateFactory.object, | ||
expService.object, | ||
); | ||
} |
47 changes: 20 additions & 27 deletions
47
src/test/tensorBoard/tensorBoardImportTracker.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,79 @@ | ||
import { assert } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
import { TensorBoardImportTracker } from '../../client/tensorBoard/tensorBoardImportTracker'; | ||
import { TensorBoardPrompt } from '../../client/tensorBoard/tensorBoardPrompt'; | ||
import { MockDocumentManager } from '../startPage/mockDocumentManager'; | ||
import { createTensorBoardPromptWithMocks } from './helpers'; | ||
|
||
suite('TensorBoard import tracker', () => { | ||
let documentManager: MockDocumentManager; | ||
let tensorBoardImportTracker: TensorBoardImportTracker; | ||
let onDidImportTensorBoardListener: sinon.SinonExpectation; | ||
let prompt: TensorBoardPrompt; | ||
let showNativeTensorBoardPrompt: sinon.SinonSpy; | ||
|
||
setup(() => { | ||
documentManager = new MockDocumentManager(); | ||
tensorBoardImportTracker = new TensorBoardImportTracker(documentManager, []); | ||
onDidImportTensorBoardListener = sinon.expectation.create('onDidImportTensorBoardListener'); | ||
tensorBoardImportTracker.onDidImportTensorBoard(onDidImportTensorBoardListener); | ||
prompt = createTensorBoardPromptWithMocks(); | ||
showNativeTensorBoardPrompt = sinon.spy(prompt, 'showNativeTensorBoardPrompt'); | ||
tensorBoardImportTracker = new TensorBoardImportTracker(documentManager, [], prompt); | ||
}); | ||
|
||
test('Simple tensorboard import in Python file', async () => { | ||
const document = documentManager.addDocument('import tensorboard', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.calledOnce); | ||
}); | ||
test('Simple tensorboard import in Python ipynb', async () => { | ||
const document = documentManager.addDocument('import tensorboard', 'foo.ipynb'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.calledOnce); | ||
}); | ||
test('`from x.y.tensorboard import z` import', async () => { | ||
const document = documentManager.addDocument('from torch.utils.tensorboard import SummaryWriter', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.calledOnce); | ||
}); | ||
test('`from x.y import tensorboard` import', async () => { | ||
const document = documentManager.addDocument('from torch.utils import tensorboard', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.calledOnce); | ||
}); | ||
test('`import x, y` import', async () => { | ||
const document = documentManager.addDocument('import tensorboard, tensorflow', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.calledOnce); | ||
}); | ||
test('`import pkg as _` import', async () => { | ||
const document = documentManager.addDocument('import tensorboard as tb', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.calledOnce); | ||
}); | ||
test('Fire on changed text editor', async () => { | ||
test('Show prompt on changed text editor', async () => { | ||
await tensorBoardImportTracker.activate(); | ||
const document = documentManager.addDocument('import tensorboard as tb', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
onDidImportTensorBoardListener.once().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.calledOnce); | ||
}); | ||
test('Do not fire event if no tensorboard import', async () => { | ||
test('Do not show prompt if no tensorboard import', async () => { | ||
const document = documentManager.addDocument('import tensorflow as tf\nfrom torch.utils import foo', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.never().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.notCalled); | ||
}); | ||
test('Do not fire event if language is not Python', async () => { | ||
test('Do not show prompt if language is not Python', async () => { | ||
const document = documentManager.addDocument( | ||
'import tensorflow as tf\nfrom torch.utils import foo', | ||
'foo.cpp', | ||
'cpp', | ||
); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.never().verify(); | ||
}); | ||
test('Ignore docstrings', async () => { | ||
const document = documentManager.addDocument( | ||
`""" | ||
import tensorboard | ||
"""`, | ||
'foo.py', | ||
); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.never().verify(); | ||
assert.ok(showNativeTensorBoardPrompt.notCalled); | ||
}); | ||
}); |
Oops, something went wrong.