@@ -26,6 +26,30 @@ const CONNECTION = {
2626 driverOptions : { }
2727} ;
2828
29+ const mockPlayground = 'use(\'dbName\');\n a\n\n\n\ndb.find();' ;
30+
31+ const activeTestEditorWithSelectionMock = {
32+ document : {
33+ languageId : 'mongodb' ,
34+ uri : {
35+ path : 'test'
36+ } as vscode . Uri ,
37+ getText : ( range : vscode . Range ) => mockPlayground . split ( '\n' ) [ range . start . line ] . substr ( range . start . character , range . end . character ) ,
38+ lineAt : ( lineNumber : number ) => ( {
39+ text : mockPlayground . split ( '\n' ) [ lineNumber ]
40+ } ) ,
41+ lineCount : mockPlayground . split ( '\n' ) . length
42+ } ,
43+ selections : [
44+ new vscode . Selection (
45+ new vscode . Position ( 0 , 5 ) ,
46+ new vscode . Position ( 0 , 11 )
47+ )
48+ ] ,
49+ edit : ( ) => null
50+ } as unknown as vscode . TextEditor ;
51+
52+
2953suite ( 'Playground Controller Test Suite' , function ( ) {
3054 this . timeout ( 5000 ) ;
3155
@@ -443,43 +467,120 @@ suite('Playground Controller Test Suite', function () {
443467 } ) ;
444468 } ) ;
445469
446- test ( 'do not show code lens if a part of a line is selected' , ( ) => {
447- const activeTestEditorWithSelectionMock : unknown = {
448- document : {
449- languageId : 'mongodb' ,
450- uri : {
451- path : 'test'
452- } ,
453- getText : ( ) => 'dbName' ,
454- lineAt : sinon . fake . returns ( { text : "use('dbName');" } )
455- } ,
456- selections : [
457- new vscode . Selection (
458- new vscode . Position ( 0 , 5 ) ,
459- new vscode . Position ( 0 , 11 )
460- )
461- ]
462- } ;
463-
464- testPlaygroundController . _activeTextEditor = activeTestEditorWithSelectionMock as vscode . TextEditor ;
470+ test ( 'do not show code lens if a part of a line with content is selected' , ( ) => {
471+ testPlaygroundController . _selectedText = 'db' ;
472+ testPlaygroundController . _activeTextEditor = activeTestEditorWithSelectionMock ;
465473
466474 testPlaygroundController . _showCodeLensForSelection (
467- new vscode . Range ( 0 , 5 , 0 , 11 )
475+ [ new vscode . Selection (
476+ new vscode . Position ( 0 , 5 ) ,
477+ new vscode . Position ( 0 , 11 )
478+ ) ] ,
479+ activeTestEditorWithSelectionMock
468480 ) ;
469481
470482 const codeLens = testPlaygroundController . _partialExecutionCodeLensProvider ?. provideCodeLenses ( ) ;
471483
472- expect ( codeLens ? .length ) . to . be . equal ( 0 ) ;
484+ expect ( codeLens . length ) . to . be . equal ( 0 ) ;
473485 } ) ;
474486
475- test ( 'show code lens if whole line is selected' , ( ) => {
487+ test ( 'do not show code lens when it has no content (multi-line)' , ( ) => {
488+ testPlaygroundController . _selectedText = ' \n\n ' ;
489+ testPlaygroundController . _activeTextEditor = activeTestEditorWithSelectionMock ;
490+
476491 testPlaygroundController . _showCodeLensForSelection (
477- new vscode . Range ( 0 , 0 , 0 , 14 )
492+ [ new vscode . Selection (
493+ new vscode . Position ( 2 , 0 ) ,
494+ new vscode . Position ( 4 , 0 )
495+ ) ] ,
496+ activeTestEditorWithSelectionMock
478497 ) ;
479498
480499 const codeLens = testPlaygroundController . _partialExecutionCodeLensProvider ?. provideCodeLenses ( ) ;
481500
482- expect ( codeLens ?. length ) . to . be . equal ( 1 ) ;
501+ expect ( codeLens . length ) . to . be . equal ( 0 ) ;
502+ } ) ;
503+
504+ test ( 'show code lens if whole line is selected' , ( ) => {
505+ testPlaygroundController . _selectedText = 'use(\'dbName\');' ;
506+ testPlaygroundController . _showCodeLensForSelection (
507+ [ new vscode . Selection (
508+ new vscode . Position ( 0 , 0 ) ,
509+ new vscode . Position ( 0 , 15 )
510+ ) ] ,
511+ activeTestEditorWithSelectionMock
512+ ) ;
513+
514+ const codeLens = testPlaygroundController . _partialExecutionCodeLensProvider . provideCodeLenses ( ) ;
515+
516+ expect ( codeLens . length ) . to . be . equal ( 1 ) ;
517+ expect ( codeLens [ 0 ] . range . end . line ) . to . be . equal ( 1 ) ;
518+ } ) ;
519+
520+ test ( 'has the correct line number for code lens when the selection includes the last line' , ( ) => {
521+ testPlaygroundController . _selectedText = 'use(\'dbName\');\n\na' ;
522+ const fakeEdit = sinon . fake . returns ( ( ) => ( { insert : sinon . fake ( ) } ) ) ;
523+ sandbox . replace (
524+ activeTestEditorWithSelectionMock ,
525+ 'edit' ,
526+ fakeEdit
527+ ) ;
528+ testPlaygroundController . _showCodeLensForSelection (
529+ [ new vscode . Selection (
530+ new vscode . Position ( 0 , 0 ) ,
531+ new vscode . Position ( 5 , 5 )
532+ ) ] ,
533+ activeTestEditorWithSelectionMock
534+ ) ;
535+
536+ const codeLens = testPlaygroundController . _partialExecutionCodeLensProvider . provideCodeLenses ( ) ;
537+
538+ expect ( codeLens . length ) . to . be . equal ( 1 ) ;
539+ expect ( codeLens [ 0 ] . range . start . line ) . to . be . equal ( 6 ) ;
540+ expect ( codeLens [ 0 ] . range . end . line ) . to . be . equal ( 6 ) ;
541+ expect ( fakeEdit ) . to . be . called ;
542+ } ) ;
543+
544+ test ( 'it calls to edit the document to add an empty line if the selection includes the last line with content' , ( done ) => {
545+ testPlaygroundController . _selectedText = 'use(\'dbName\');\n\na' ;
546+ sandbox . replace (
547+ activeTestEditorWithSelectionMock ,
548+ 'edit' ,
549+ ( cb ) => {
550+ cb ( {
551+ insert : ( position : vscode . Position , toInsert : string ) => {
552+ expect ( position . line ) . to . equal ( 6 ) ;
553+ expect ( toInsert ) . to . equal ( '\n' ) ;
554+ done ( ) ;
555+ }
556+ } as unknown as vscode . TextEditorEdit ) ;
557+ return new Promise ( ( resolve ) => resolve ( true ) ) ;
558+ }
559+ ) ;
560+ testPlaygroundController . _showCodeLensForSelection (
561+ [ new vscode . Selection (
562+ new vscode . Position ( 0 , 0 ) ,
563+ new vscode . Position ( 5 , 5 )
564+ ) ] ,
565+ activeTestEditorWithSelectionMock
566+ ) ;
567+ } ) ;
568+
569+ test ( 'shows the codelens on the line above the last selected line when the last selected line is empty' , ( ) => {
570+ testPlaygroundController . _selectedText = 'use(\'dbName\');\n\n' ;
571+ testPlaygroundController . _showCodeLensForSelection (
572+ [ new vscode . Selection (
573+ new vscode . Position ( 0 , 0 ) ,
574+ new vscode . Position ( 1 , 3 )
575+ ) ] ,
576+ activeTestEditorWithSelectionMock
577+ ) ;
578+
579+ const codeLens = testPlaygroundController . _partialExecutionCodeLensProvider . provideCodeLenses ( ) ;
580+
581+ expect ( codeLens . length ) . to . be . equal ( 1 ) ;
582+ expect ( codeLens [ 0 ] . range . start . line ) . to . be . equal ( 2 ) ;
583+ expect ( codeLens [ 0 ] . range . end . line ) . to . be . equal ( 2 ) ;
483584 } ) ;
484585
485586 test ( 'playground controller loads the active editor on start' , ( ) => {
0 commit comments