@@ -11,129 +11,74 @@ import { suite } from 'mocha';
1111import * as path from 'path' ;
1212import * as sinon from 'sinon' ;
1313import * as vscode from 'vscode' ;
14- import * as api from 'vscode-cpptools' ;
15- import * as apit from 'vscode-cpptools/out/testApi' ;
1614import * as extension from '../../../../src/LanguageServer/extension' ;
1715import * as testHelpers from '../../../common/testHelpers' ;
1816
1917suite ( "Inactive region folding in a multi-root workspace" , function ( ) : void {
20- let testHook : apit . CppToolsTestHook ;
2118 let firstWorkspaceFolder : vscode . WorkspaceFolder ;
2219 let workspaceFolder : vscode . WorkspaceFolder ;
2320
2421 suiteSetup ( async function ( ) : Promise < void > {
2522 await testHelpers . activateCppExtension ( ) ;
26- const cpptools : apit . CppToolsTestApi = await apit . getCppToolsTestApi ( api . Version . latest )
27- ?? assert . fail ( "Could not get CppToolsTestApi" ) ;
28- testHook = cpptools . getTestHook ( ) ;
2923 firstWorkspaceFolder = vscode . workspace . workspaceFolders ?. [ 0 ]
3024 ?? assert . fail ( "First workspace folder is unavailable" ) ;
3125 workspaceFolder = vscode . workspace . workspaceFolders ?. [ 1 ]
3226 ?? assert . fail ( "Second workspace folder is unavailable" ) ;
3327 } ) ;
3428
35- suiteTeardown ( function ( ) : void {
36- testHook . dispose ( ) ;
37- } ) ;
38-
3929 test ( "routes manual folding operations by URI during a workspace client switch" , async ( ) => {
40- const configuration : vscode . WorkspaceConfiguration = vscode . workspace . getConfiguration ( "C_Cpp" , workspaceFolder . uri ) ;
41- const previousCodeFoldingValue : string | undefined = configuration . inspect < string > ( "codeFolding" ) ?. globalValue ;
42- await configuration . update ( "codeFolding" , "enabled" , vscode . ConfigurationTarget . Global ) ;
30+ const sandbox : sinon . SinonSandbox = sinon . createSandbox ( ) ;
4331 let releaseEditorChange : ( ( ) => void ) | undefined ;
44- let editorChangeStub : sinon . SinonStub | undefined ;
4532 let editorChangePromise : Promise < void > | undefined ;
4633 let editor : vscode . TextEditor | undefined ;
4734 let firstEditor : vscode . TextEditor | undefined ;
4835 try {
49- editor = await openFileAndWaitForIntelliSense ( ) ;
50- await vscode . commands . executeCommand ( "editor.unfoldAll" ) ;
51-
5236 const firstDocument : vscode . TextDocument = await vscode . workspace . openTextDocument (
5337 path . join ( firstWorkspaceFolder . uri . fsPath , "test.cpp" ) ) ;
5438 firstEditor = await vscode . window . showTextDocument ( firstDocument ) ;
5539 await extension . clients . didChangeActiveEditor ( firstEditor ) ;
5640 const firstClient = extension . clients . ActiveClient ;
57- const owner = extension . clients . getClientFor ( editor . document . uri ) ;
41+ const document : vscode . TextDocument = await vscode . workspace . openTextDocument (
42+ path . join ( workspaceFolder . uri . fsPath , "code_folding.cpp" ) ) ;
43+ const owner = extension . clients . getClientFor ( document . uri ) ;
5844 assert . notStrictEqual ( firstClient , owner ) ;
5945
46+ const ownerFoldStub : sinon . SinonStub = sandbox . stub ( owner , "foldAllInactiveRegions" ) . resolves ( ) ;
47+ const ownerUnfoldStub : sinon . SinonStub = sandbox . stub ( owner , "unfoldAllInactiveRegions" ) . resolves ( ) ;
48+ const firstFoldStub : sinon . SinonStub = sandbox . stub ( firstClient , "foldAllInactiveRegions" ) . resolves ( ) ;
49+ const firstUnfoldStub : sinon . SinonStub = sandbox . stub ( firstClient , "unfoldAllInactiveRegions" ) . resolves ( ) ;
50+
6051 const pendingEditorChange : Promise < void > = new Promise ( resolve => releaseEditorChange = resolve ) ;
61- editorChangeStub = sinon . stub ( owner , "didChangeActiveEditor" ) . returns ( pendingEditorChange ) ;
62- const activeEditor : vscode . TextEditor = await vscode . window . showTextDocument ( editor . document , editor . viewColumn , false ) ;
63- editorChangePromise = extension . clients . didChangeActiveEditor ( activeEditor ) ;
52+ // Hold the client switch open; only command dispatch is under test.
53+ sandbox . stub ( owner , "didChangeActiveEditor" ) . returns ( pendingEditorChange ) ;
54+ editor = await vscode . window . showTextDocument ( document ) ;
55+ editorChangePromise = extension . clients . didChangeActiveEditor ( editor ) ;
6456 assert . strictEqual ( extension . clients . ActiveClient , firstClient ) ;
6557
6658 await vscode . commands . executeCommand ( "C_Cpp.FoldAllInactiveRegions" ) ;
67- await assertInactiveBranchIsFolded ( editor ) ;
59+ sinon . assert . calledOnce ( ownerFoldStub ) ;
60+ sinon . assert . notCalled ( firstFoldStub ) ;
61+ assert . strictEqual ( extension . clients . ActiveClient , firstClient ) ;
6862
6963 await vscode . commands . executeCommand ( "C_Cpp.UnfoldAllInactiveRegions" ) ;
70- await assertInactiveBranchIsUnfolded ( editor ) ;
64+ sinon . assert . calledOnce ( ownerUnfoldStub ) ;
65+ sinon . assert . notCalled ( firstUnfoldStub ) ;
66+ assert . strictEqual ( extension . clients . ActiveClient , firstClient ) ;
7167 } finally {
7268 releaseEditorChange ?.( ) ;
7369 await editorChangePromise ;
74- editorChangeStub ? .restore ( ) ;
70+ sandbox . restore ( ) ;
7571 if ( editor ) {
7672 await closeEditor ( editor ) ;
7773 }
7874 if ( firstEditor ) {
7975 await closeEditor ( firstEditor ) ;
8076 }
81- await testHelpers . delay ( 150 ) ;
82- await configuration . update ( "codeFolding" , previousCodeFoldingValue , vscode . ConfigurationTarget . Global ) ;
8377 }
8478 } ) ;
8579
8680 async function closeEditor ( editor : vscode . TextEditor ) : Promise < void > {
8781 await vscode . window . showTextDocument ( editor . document , editor . viewColumn , false ) ;
8882 await vscode . commands . executeCommand ( "workbench.action.closeActiveEditor" ) ;
8983 }
90-
91- async function assertInactiveBranchIsFolded ( editor : vscode . TextEditor ) : Promise < void > {
92- let line : number = 24 ;
93- for ( let i : number = 0 ; i < 20 ; ++ i ) {
94- const activeEditor : vscode . TextEditor = await vscode . window . showTextDocument ( editor . document , editor . viewColumn , false ) ;
95- activeEditor . selection = new vscode . Selection ( 24 , 0 , 24 , 0 ) ;
96- await vscode . commands . executeCommand ( "cursorMove" , { to : "down" , by : "wrappedLine" , value : 1 } ) ;
97- line = activeEditor . selection . active . line ;
98- if ( line === 28 ) {
99- return ;
100- }
101- await testHelpers . delay ( 50 ) ;
102- }
103-
104- assert . strictEqual ( line , 28 ) ;
105- }
106-
107- async function assertInactiveBranchIsUnfolded ( editor : vscode . TextEditor ) : Promise < void > {
108- const activeEditor : vscode . TextEditor = await vscode . window . showTextDocument ( editor . document , editor . viewColumn , false ) ;
109- activeEditor . selection = new vscode . Selection ( 24 , 0 , 24 , 0 ) ;
110- await vscode . commands . executeCommand ( "cursorMove" , { to : "down" , by : "wrappedLine" , value : 1 } ) ;
111- assert . strictEqual ( activeEditor . selection . active . line , 25 ) ;
112- }
113-
114- function waitForIntelliSenseReady ( fileName : string ) : Promise < void > {
115- return new Promise < void > ( ( resolve , reject ) => {
116- const timeout : NodeJS . Timeout = setTimeout ( ( ) => {
117- listener . dispose ( ) ;
118- reject ( new Error ( `Timed out waiting for IntelliSense for ${ fileName } ` ) ) ;
119- } , testHelpers . defaultTimeout ) ;
120- const listener : vscode . Disposable = testHook . IntelliSenseStatusChanged ( result => {
121- if ( result . filename === fileName && result . status === apit . Status . IntelliSenseReady ) {
122- clearTimeout ( timeout ) ;
123- listener . dispose ( ) ;
124- resolve ( ) ;
125- }
126- } ) ;
127- } ) ;
128- }
129-
130- async function openFileAndWaitForIntelliSense ( ) : Promise < vscode . TextEditor > {
131- const fileName : string = "code_folding.cpp" ;
132- const filePath : string = path . join ( workspaceFolder . uri . fsPath , fileName ) ;
133- const ready : Promise < void > = waitForIntelliSenseReady ( fileName ) ;
134- const document : vscode . TextDocument = await vscode . workspace . openTextDocument ( filePath ) ;
135- const editor : vscode . TextEditor = await vscode . window . showTextDocument ( document ) ;
136- await ready ;
137- return editor ;
138- }
13984} ) ;
0 commit comments