import * as assert from 'assert' ; import * as vscode from 'vscode' ; import * as path from 'path' ; import { PLSQLCompletionItemProvider } from '../src/provider/plsqlCompletionItem.provider' ; interface ICase { curTextLine : string ; atBeginning : boolean ; expCompletion : any [ ] ; } suite ( 'PLSQL Completion' , ( ) => { const provider = new PLSQLCompletionItemProvider ( ) ; function runTest ( file , cases , done ) { const uri = vscode . Uri . file ( path . join ( vscode . workspace . rootPath , file ) ) ; vscode . workspace . openTextDocument ( uri ) . then ( textDocument => { return Promise . all ( cases . map ( ( test , index ) => { const regExp = new RegExp ( test . curTextLine , 'gi' ) ; const found = regExp . exec ( textDocument . getText ( ) ) ; assert . notEqual ( found , null , `curText: ${ test . curTextLine } not found` ) ; const curPos = textDocument . positionAt ( test . atBeginning ? found . index + 2 : regExp . lastIndex ) ; return provider . provideCompletionItems ( textDocument , curPos , null ) . then ( completion => { if ( completion ) { test . expCompletion . forEach ( ( expItem ) => { const foundItem = completion . find ( item => item . label . toLowerCase ( ) === expItem . text . toLowerCase ( ) ) ; if ( ! foundItem ) assert . ok ( false , `not Found ${ expItem . text }` ) ; assert . equal ( foundItem . kind , expItem . kind , `wrong kind ${ expItem . text }` ) ; } ) ; } else assert . ok ( false , `Found no completion ${ test . curTextLine }` ) ; } ) ; } ) ) ; } , err => assert . ok ( false , `error in OpenTextDocument ${ err }` ) ) . then ( ( ) => done ( ) , done ) ; } function buildCase ( curTextLine , atBeginning , expCompletion ) { return { atBeginning : atBeginning , curTextLine : curTextLine , expCompletion : expCompletion } ; } test ( 'Package' , ( done ) => { let testCases = [ buildCase ( '-- complete\\s*MyPackage2.' , false , [ { text : 'txyz_myType' , kind : vscode . CompletionItemKind . Struct } , { text : 'ttxyz_myType' , kind : vscode . CompletionItemKind . Struct } , { text : 'myConst' , kind : vscode . CompletionItemKind . Constant } , { text : 'myGlobalVar' , kind : vscode . CompletionItemKind . Variable } , { text : 'get_myValue' , kind : vscode . CompletionItemKind . Function } , { text : 'set_myValue' , kind : vscode . CompletionItemKind . Method } , { text : 'myCall' , kind : vscode . CompletionItemKind . Method } ] ) , buildCase ( '-- complete\\s*return MyPackage2.myV' , false , [ { text : 'myGlobalVar' , kind : vscode . CompletionItemKind . Variable } , { text : 'get_myValue' , kind : vscode . CompletionItemKind . Function } , { text : 'set_myValue' , kind : vscode . CompletionItemKind . Method } , ] ) , buildCase ( '\\s*procedure set_myValue\\(param1 in varchar2\\)\\s*is' , true , [ { text : '__doc' , kind : vscode . CompletionItemKind . Snippet } , { text : 'begin' , kind : vscode . CompletionItemKind . Snippet } ] ) ] ; runTest ( 'xyz_myPackage.sql' , testCases , done ) ; } ) ; test ( 'Separate package body' , ( done ) => { let testCases = [ buildCase ( '-- complete\\s*MyPackage.' , false , [ { text : 'txyz_myType' , kind : vscode . CompletionItemKind . Struct } , { text : 'ttxyz_myType' , kind : vscode . CompletionItemKind . Struct } , { text : 'myConst' , kind : vscode . CompletionItemKind . Constant } , { text : 'myGlobalVar' , kind : vscode . CompletionItemKind . Variable } , { text : 'get_myValue' , kind : vscode . CompletionItemKind . Function } , { text : 'set_myValue' , kind : vscode . CompletionItemKind . Method } , { text : 'myCall' , kind : vscode . CompletionItemKind . Method } ] ) , buildCase ( '-- complete\\s*MyPackage.myV' , false , [ { text : 'myGlobalVar' , kind : vscode . CompletionItemKind . Variable } , { text : 'get_myValue' , kind : vscode . CompletionItemKind . Function } , { text : 'set_myValue' , kind : vscode . CompletionItemKind . Method } , ] ) , buildCase ( '\\s*procedure set_myValue\\(param1 in varchar2\\)\\s*is' , true , [ { text : '__doc' , kind : vscode . CompletionItemKind . Snippet } , { text : 'begin' , kind : vscode . CompletionItemKind . Snippet } ] ) ] ; runTest ( 'xyz_myPackage2.pkb' , testCases , done ) ; } ) ; } ) ;