11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4- import { anything , when } from 'ts-mockito' ;
4+ import { anything , verify , when } from 'ts-mockito' ;
55import * as TypeMoq from 'typemoq' ;
6- import { CancellationTokenSource , Disposable , EventEmitter , TextDocument , Uri } from 'vscode' ;
6+ import { CancellationTokenSource , CodeLens , Disposable , EventEmitter , TextDocument , Uri , Range } from 'vscode' ;
77
88import { IDebugService } from '../../platform/common/application/types' ;
99import { IConfigurationService , IWatchableJupyterSettings } from '../../platform/common/types' ;
1010import { DataScienceCodeLensProvider } from '../../interactive-window/editor-integration/codelensprovider' ;
1111import { IServiceContainer } from '../../platform/ioc/types' ;
12- import { ICodeWatcher , IDataScienceCodeLensProvider } from '../../interactive-window/editor-integration/types' ;
12+ import { ICodeWatcher } from '../../interactive-window/editor-integration/types' ;
1313import { IDebugLocationTracker } from '../../notebooks/debugger/debuggingTypes' ;
1414import { mockedVSCodeNamespaces } from '../../test/vscode-mock' ;
1515
1616// eslint-disable-next-line
1717suite ( 'DataScienceCodeLensProvider Unit Tests' , ( ) => {
1818 let serviceContainer : TypeMoq . IMock < IServiceContainer > ;
1919 let configurationService : TypeMoq . IMock < IConfigurationService > ;
20- let codeLensProvider : IDataScienceCodeLensProvider ;
2120 let pythonSettings : TypeMoq . IMock < IWatchableJupyterSettings > ;
2221 let debugService : TypeMoq . IMock < IDebugService > ;
2322 let debugLocationTracker : TypeMoq . IMock < IDebugLocationTracker > ;
@@ -37,16 +36,17 @@ suite('DataScienceCodeLensProvider Unit Tests', () => {
3736 configurationService . setup ( ( c ) => c . getSettings ( TypeMoq . It . isAny ( ) ) ) . returns ( ( ) => pythonSettings . object ) ;
3837 when ( mockedVSCodeNamespaces . commands . executeCommand ( anything ( ) , anything ( ) , anything ( ) ) ) . thenResolve ( ) ;
3938 debugService . setup ( ( d ) => d . activeDebugSession ) . returns ( ( ) => undefined ) ;
40- codeLensProvider = new DataScienceCodeLensProvider (
39+ } ) ;
40+
41+ function provideCodeLensesForOneDoc ( codeLenses : CodeLens [ ] = [ ] ) {
42+ const codeLensProvider = new DataScienceCodeLensProvider (
4143 serviceContainer . object ,
4244 debugLocationTracker . object ,
4345 configurationService . object ,
4446 disposables ,
4547 debugService . object
4648 ) ;
47- } ) ;
4849
49- test ( 'Initialize Code Lenses one document' , async ( ) => {
5050 // Create our document
5151 const document = TypeMoq . Mock . ofType < TextDocument > ( ) ;
5252 const uri = Uri . file ( 'test.py' ) ;
@@ -57,21 +57,35 @@ suite('DataScienceCodeLensProvider Unit Tests', () => {
5757 const targetCodeWatcher = TypeMoq . Mock . ofType < ICodeWatcher > ( ) ;
5858 targetCodeWatcher
5959 . setup ( ( tc ) => tc . getCodeLenses ( ) )
60- . returns ( ( ) => [ ] )
60+ . returns ( ( ) => codeLenses )
6161 . verifiable ( TypeMoq . Times . once ( ) ) ;
6262 serviceContainer
6363 . setup ( ( c ) => c . get ( TypeMoq . It . isValue ( ICodeWatcher ) ) )
6464 . returns ( ( ) => targetCodeWatcher . object )
6565 . verifiable ( TypeMoq . Times . once ( ) ) ;
6666 when ( mockedVSCodeNamespaces . workspace . textDocuments ) . thenReturn ( [ document . object ] ) ;
6767
68- await codeLensProvider . provideCodeLenses ( document . object , tokenSource . token ) ;
68+ codeLensProvider . provideCodeLenses ( document . object , tokenSource . token ) ;
69+
70+ return targetCodeWatcher ;
71+ }
72+
73+ test ( 'Initialize Code Lenses one document' , async ( ) => {
74+ const targetCodeWatcher = provideCodeLensesForOneDoc ( ) ;
6975
7076 targetCodeWatcher . verifyAll ( ) ;
7177 serviceContainer . verifyAll ( ) ;
7278 } ) ;
7379
7480 test ( 'Initialize Code Lenses same doc called' , async ( ) => {
81+ const codeLensProvider = new DataScienceCodeLensProvider (
82+ serviceContainer . object ,
83+ debugLocationTracker . object ,
84+ configurationService . object ,
85+ disposables ,
86+ debugService . object
87+ ) ;
88+
7589 // Create our document
7690 const document = TypeMoq . Mock . ofType < TextDocument > ( ) ;
7791 const uri = Uri . file ( 'test.py' ) ;
@@ -94,15 +108,23 @@ suite('DataScienceCodeLensProvider Unit Tests', () => {
94108 . verifiable ( TypeMoq . Times . once ( ) ) ;
95109 when ( mockedVSCodeNamespaces . workspace . textDocuments ) . thenReturn ( [ document . object ] ) ;
96110
97- await codeLensProvider . provideCodeLenses ( document . object , tokenSource . token ) ;
98- await codeLensProvider . provideCodeLenses ( document . object , tokenSource . token ) ;
111+ codeLensProvider . provideCodeLenses ( document . object , tokenSource . token ) ;
112+ codeLensProvider . provideCodeLenses ( document . object , tokenSource . token ) ;
99113
100114 // getCodeLenses should be called twice, but getting the code watcher only once due to same doc
101115 targetCodeWatcher . verifyAll ( ) ;
102116 serviceContainer . verifyAll ( ) ;
103117 } ) ;
104118
105119 test ( 'Initialize Code Lenses different documents' , async ( ) => {
120+ const codeLensProvider = new DataScienceCodeLensProvider (
121+ serviceContainer . object ,
122+ debugLocationTracker . object ,
123+ configurationService . object ,
124+ disposables ,
125+ debugService . object
126+ ) ;
127+
106128 // Create our document
107129 const uri1 = Uri . file ( 'test.py' ) ;
108130 const document1 = TypeMoq . Mock . ofType < TextDocument > ( ) ;
@@ -134,13 +156,40 @@ suite('DataScienceCodeLensProvider Unit Tests', () => {
134156
135157 when ( mockedVSCodeNamespaces . workspace . textDocuments ) . thenReturn ( [ document1 . object , document2 . object ] ) ;
136158
137- await codeLensProvider . provideCodeLenses ( document1 . object , tokenSource . token ) ;
138- await codeLensProvider . provideCodeLenses ( document1 . object , tokenSource . token ) ;
139- await codeLensProvider . provideCodeLenses ( document2 . object , tokenSource . token ) ;
159+ codeLensProvider . provideCodeLenses ( document1 . object , tokenSource . token ) ;
160+ codeLensProvider . provideCodeLenses ( document1 . object , tokenSource . token ) ;
161+ codeLensProvider . provideCodeLenses ( document2 . object , tokenSource . token ) ;
140162
141163 // service container get should be called three times as the names and versions don't match
142164 targetCodeWatcher1 . verifyAll ( ) ;
143165 targetCodeWatcher2 . verifyAll ( ) ;
144166 serviceContainer . verifyAll ( ) ;
145167 } ) ;
168+
169+ test ( 'Having code lenses will update context keys to true' , async ( ) => {
170+ pythonSettings . setup ( ( p ) => p . sendSelectionToInteractiveWindow ) . returns ( ( ) => true ) ;
171+
172+ provideCodeLensesForOneDoc ( [ new CodeLens ( { } as Range ) ] ) ;
173+
174+ verify ( mockedVSCodeNamespaces . commands . executeCommand ( 'setContext' , 'jupyter.ownsSelection' , true ) ) . atLeast ( 1 ) ;
175+ verify ( mockedVSCodeNamespaces . commands . executeCommand ( 'setContext' , 'jupyter.hascodecells' , true ) ) . atLeast ( 1 ) ;
176+ } ) ;
177+
178+ test ( 'Having no code lenses will set context keys to false' , async ( ) => {
179+ pythonSettings . setup ( ( p ) => p . sendSelectionToInteractiveWindow ) . returns ( ( ) => false ) ;
180+
181+ provideCodeLensesForOneDoc ( [ ] ) ;
182+
183+ verify ( mockedVSCodeNamespaces . commands . executeCommand ( 'setContext' , 'jupyter.ownsSelection' , false ) ) . atLeast ( 1 ) ;
184+ verify ( mockedVSCodeNamespaces . commands . executeCommand ( 'setContext' , 'jupyter.hascodecells' , false ) ) . atLeast ( 1 ) ;
185+ } ) ;
186+
187+ test ( 'Having no code lenses but ownership setting true will set context keys correctly' , async ( ) => {
188+ pythonSettings . setup ( ( p ) => p . sendSelectionToInteractiveWindow ) . returns ( ( ) => true ) ;
189+
190+ provideCodeLensesForOneDoc ( [ ] ) ;
191+
192+ verify ( mockedVSCodeNamespaces . commands . executeCommand ( 'setContext' , 'jupyter.ownsSelection' , true ) ) . atLeast ( 1 ) ;
193+ verify ( mockedVSCodeNamespaces . commands . executeCommand ( 'setContext' , 'jupyter.hascodecells' , false ) ) . atLeast ( 1 ) ;
194+ } ) ;
146195} ) ;
0 commit comments