@@ -24,6 +24,9 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
2424
2525 const extensionContextStub = new ExtensionContextStub ( ) ;
2626
27+ const EXPORT_LANGUAGES_CODEACTIONS_COUNT = 8 ;
28+ const TOTAL_CODEACTIONS_COUNT = EXPORT_LANGUAGES_CODEACTIONS_COUNT + 1 ;
29+
2730 // The test extension runner.
2831 extensionContextStub . extensionPath = '../../' ;
2932
@@ -182,7 +185,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
182185 expect ( codeActions ) . to . exist ;
183186
184187 if ( codeActions ) {
185- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
188+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
186189 const actionCommand = codeActions [ 2 ] . command ;
187190
188191 if ( actionCommand ) {
@@ -218,7 +221,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
218221 expect ( codeActions ) . to . exist ;
219222
220223 if ( codeActions ) {
221- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
224+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
222225 const actionCommand = codeActions [ 2 ] . command ;
223226
224227 if ( actionCommand ) {
@@ -283,7 +286,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
283286 expect ( codeActions ) . to . exist ;
284287
285288 if ( codeActions ) {
286- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
289+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
287290 const actionCommand = codeActions [ 3 ] . command ;
288291
289292 if ( actionCommand ) {
@@ -352,7 +355,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
352355 expect ( codeActions ) . to . exist ;
353356
354357 if ( codeActions ) {
355- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
358+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
356359 const actionCommand = codeActions [ 1 ] . command ;
357360
358361 if ( actionCommand ) {
@@ -426,7 +429,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
426429 expect ( codeActions ) . to . exist ;
427430
428431 if ( codeActions ) {
429- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
432+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
430433 const actionCommand = codeActions [ 5 ] . command ;
431434
432435 if ( actionCommand ) {
@@ -500,7 +503,7 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
500503 expect ( codeActions ) . to . exist ;
501504
502505 if ( codeActions ) {
503- expect ( codeActions . length ) . to . be . equal ( 7 ) ;
506+ expect ( codeActions . length ) . to . be . equal ( TOTAL_CODEACTIONS_COUNT ) ;
504507 const actionCommand = codeActions [ 6 ] . command ;
505508
506509 if ( actionCommand ) {
@@ -547,6 +550,154 @@ suite('Playground Selected CodeAction Provider Test Suite', function () {
547550 }
548551 }
549552 } ) ;
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+ } ) ;
550701 } ) ;
551702
552703 suite ( 'the regular JS file' , ( ) => {
0 commit comments