@@ -24,6 +24,9 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
24
24
25
25
const extensionContextStub = new ExtensionContextStub ( ) ;
26
26
27
+ const EXPORT_LANGUAGES_CODEACTIONS_COUNT = 8 ;
28
+ const TOTAL_CODEACTIONS_COUNT = EXPORT_LANGUAGES_CODEACTIONS_COUNT + 1 ;
29
+
27
30
// The test extension runner.
28
31
extensionContextStub . extensionPath = '../../' ;
29
32
@@ -182,7 +185,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
182
185
expect ( codeActions ) . to . exist ;
183
186
184
187
if ( codeActions ) {
185
- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
188
+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
186
189
const actionCommand = codeActions [ 2 ] . command ;
187
190
188
191
if ( actionCommand ) {
@@ -218,7 +221,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
218
221
expect ( codeActions ) . to . exist ;
219
222
220
223
if ( codeActions ) {
221
- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
224
+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
222
225
const actionCommand = codeActions [ 2 ] . command ;
223
226
224
227
if ( actionCommand ) {
@@ -283,7 +286,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
283
286
expect ( codeActions ) . to . exist ;
284
287
285
288
if ( codeActions ) {
286
- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
289
+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
287
290
const actionCommand = codeActions [ 3 ] . command ;
288
291
289
292
if ( actionCommand ) {
@@ -352,7 +355,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
352
355
expect ( codeActions ) . to . exist ;
353
356
354
357
if ( codeActions ) {
355
- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
358
+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
356
359
const actionCommand = codeActions [ 1 ] . command ;
357
360
358
361
if ( actionCommand ) {
@@ -426,7 +429,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
426
429
expect ( codeActions ) . to . exist ;
427
430
428
431
if ( codeActions ) {
429
- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
432
+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
430
433
const actionCommand = codeActions [ 5 ] . command ;
431
434
432
435
if ( actionCommand ) {
@@ -500,7 +503,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
500
503
expect ( codeActions ) . to . exist ;
501
504
502
505
if ( codeActions ) {
503
- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
506
+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
504
507
const actionCommand = codeActions [ 6 ] . command ;
505
508
506
509
if ( actionCommand ) {
@@ -547,6 +550,154 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
547
550
}
548
551
}
549
552
} ) ;
553
+
554
+ test ( 'exports to rust and includes driver syntax' , async ( ) => {
555
+ const textFromEditor = "use('db'); db.coll.find({ name: '22' })" ;
556
+ const selection = {
557
+ start : { line : 0 , character : 24 } ,
558
+ end : { line : 0 , character : 38 } ,
559
+ } as vscode . Selection ;
560
+ const mode = ExportToLanguageMode . QUERY ;
561
+ const activeTextEditor = {
562
+ document : { getText : ( ) => textFromEditor } ,
563
+ } as vscode . TextEditor ;
564
+
565
+ mdbTestExtension . testExtensionController . _playgroundController . _selectedText =
566
+ "{ name: '22' }" ;
567
+ mdbTestExtension . testExtensionController . _playgroundController . _playgroundSelectedCodeActionProvider . selection =
568
+ selection ;
569
+ mdbTestExtension . testExtensionController . _playgroundController . _playgroundSelectedCodeActionProvider . mode =
570
+ mode ;
571
+ mdbTestExtension . testExtensionController . _playgroundController . _activeTextEditor =
572
+ activeTextEditor ;
573
+
574
+ testCodeActionProvider . refresh ( { selection, mode } ) ;
575
+
576
+ const codeActions = testCodeActionProvider . provideCodeActions ( ) ;
577
+ expect ( codeActions ) . to . exist ;
578
+
579
+ if ( codeActions ) {
580
+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
581
+ const actionCommand = codeActions [ 7 ] . command ;
582
+
583
+ if ( actionCommand ) {
584
+ expect ( actionCommand . command ) . to . be . equal ( 'mdb.exportToRust' ) ;
585
+ expect ( actionCommand . title ) . to . be . equal ( 'Export To Rust' ) ;
586
+
587
+ await vscode . commands . executeCommand ( actionCommand . command ) ;
588
+
589
+ let expectedResult : PlaygroundResult = {
590
+ namespace : 'DATABASE_NAME.COLLECTION_NAME' ,
591
+ type : null ,
592
+ content : 'doc! {\n "name": "22"\n}' ,
593
+ language : 'rust' ,
594
+ } ;
595
+ expect (
596
+ mdbTestExtension . testExtensionController . _playgroundController
597
+ . _playgroundResult
598
+ ) . to . be . deep . equal ( expectedResult ) ;
599
+
600
+ const codeLenses =
601
+ mdbTestExtension . testExtensionController . _playgroundController . _exportToLanguageCodeLensProvider . provideCodeLenses ( ) ;
602
+ expect ( codeLenses . length ) . to . be . equal ( 2 ) ;
603
+
604
+ await vscode . commands . executeCommand (
605
+ 'mdb.changeExportToLanguageAddons' ,
606
+ {
607
+ ...mdbTestExtension . testExtensionController . _playgroundController
608
+ . _exportToLanguageCodeLensProvider . _exportToLanguageAddons ,
609
+ driverSyntax : true ,
610
+ }
611
+ ) ;
612
+
613
+ expectedResult = {
614
+ namespace : 'db.coll' ,
615
+ type : null ,
616
+ content : `// Requires the MongoDB crate.\n// https://crates.io/crates/mongodb\n\nlet client = Client::with_uri_str(\"mongodb://localhost:27088/?appname=mongodb-vscode+${ version } \").await?;\nlet result = client.database(\"db\").collection::<Document>(\"coll\").find(doc! {\n \"name\": \"22\"\n}, None).await?;` ,
617
+ language : 'rust' ,
618
+ } ;
619
+
620
+ expect (
621
+ mdbTestExtension . testExtensionController . _playgroundController
622
+ . _playgroundResult
623
+ ) . to . be . deep . equal ( expectedResult ) ;
624
+ }
625
+ }
626
+ } ) ;
627
+
628
+ test ( 'exports to php and includes driver syntax' , async ( ) => {
629
+ const textFromEditor = "use('db'); db.coll.find({ name: '22' })" ;
630
+ const selection = {
631
+ start : { line : 0 , character : 24 } ,
632
+ end : { line : 0 , character : 38 } ,
633
+ } as vscode . Selection ;
634
+ const mode = ExportToLanguageMode . QUERY ;
635
+ const activeTextEditor = {
636
+ document : { getText : ( ) => textFromEditor } ,
637
+ } as vscode . TextEditor ;
638
+
639
+ mdbTestExtension . testExtensionController . _playgroundController . _selectedText =
640
+ "{ name: '22' }" ;
641
+ mdbTestExtension . testExtensionController . _playgroundController . _playgroundSelectedCodeActionProvider . selection =
642
+ selection ;
643
+ mdbTestExtension . testExtensionController . _playgroundController . _playgroundSelectedCodeActionProvider . mode =
644
+ mode ;
645
+ mdbTestExtension . testExtensionController . _playgroundController . _activeTextEditor =
646
+ activeTextEditor ;
647
+
648
+ testCodeActionProvider . refresh ( { selection, mode } ) ;
649
+
650
+ const codeActions = testCodeActionProvider . provideCodeActions ( ) ;
651
+ expect ( codeActions ) . to . exist ;
652
+
653
+ if ( codeActions ) {
654
+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
655
+ const actionCommand = codeActions [ 8 ] . command ;
656
+
657
+ if ( actionCommand ) {
658
+ expect ( actionCommand . command ) . to . be . equal ( 'mdb.exportToPHP' ) ;
659
+ expect ( actionCommand . title ) . to . be . equal ( 'Export To PHP' ) ;
660
+
661
+ await vscode . commands . executeCommand ( actionCommand . command ) ;
662
+
663
+ let expectedResult : PlaygroundResult = {
664
+ namespace : 'DATABASE_NAME.COLLECTION_NAME' ,
665
+ type : null ,
666
+ content : "['name' => '22']" ,
667
+ language : 'php' ,
668
+ } ;
669
+ expect (
670
+ mdbTestExtension . testExtensionController . _playgroundController
671
+ . _playgroundResult
672
+ ) . to . be . deep . equal ( expectedResult ) ;
673
+
674
+ const codeLenses =
675
+ mdbTestExtension . testExtensionController . _playgroundController . _exportToLanguageCodeLensProvider . provideCodeLenses ( ) ;
676
+ expect ( codeLenses . length ) . to . be . equal ( 2 ) ;
677
+
678
+ await vscode . commands . executeCommand (
679
+ 'mdb.changeExportToLanguageAddons' ,
680
+ {
681
+ ...mdbTestExtension . testExtensionController . _playgroundController
682
+ . _exportToLanguageCodeLensProvider . _exportToLanguageAddons ,
683
+ driverSyntax : true ,
684
+ }
685
+ ) ;
686
+
687
+ expectedResult = {
688
+ namespace : 'db.coll' ,
689
+ type : null ,
690
+ content : `// Requires the MongoDB PHP Driver\n// https://www.mongodb.com/docs/drivers/php/\n\n$client = new Client('mongodb://localhost:27088/?appname=mongodb-vscode+${ version } ');\n$collection = $client->selectCollection('db', 'coll');\n$cursor = $collection->find(['name' => '22']);` ,
691
+ language : 'php' ,
692
+ } ;
693
+
694
+ expect (
695
+ mdbTestExtension . testExtensionController . _playgroundController
696
+ . _playgroundResult
697
+ ) . to . be . deep . equal ( expectedResult ) ;
698
+ }
699
+ }
700
+ } ) ;
550
701
} ) ;
551
702
552
703
suite ( 'the regular JS file' , ( ) => {
0 commit comments