import { Range , Position } from 'vscode-languageserver/node' ; import { TextDocument } from 'vscode-languageserver-textdocument' ; import * as vscode from 'vscode' ; import { Uri , workspace } from 'vscode' ; import * as path from 'path' ; import * as assert from 'assert' ; import { ANTLRBackend } from '../compiling/ANTLRBackend' ; const testFolderLocation = '../../src/test' ; suite ( 'Macro Replacement Tests' , ( ) => { test ( 'test #1: Macro replacement/definition removal for text without macro definitions' , async ( ) => { const input_file_name = 'test-files/MacroReplace.test/no_macros.sv' ; const expected_file_name = 'test-files/MacroReplace.test/no_macros_expected.sv' ; await macroReplaceTest ( input_file_name , expected_file_name ) ; } ) . timeout ( 10000 ) ; test ( 'test #2: Macro replacement/definition removal for text with single macro definition but no macro uses' , async ( ) => { const input_file_name = 'test-files/MacroReplace.test/single_macro_no_uses.sv' ; const expected_file_name = 'test-files/MacroReplace.test/single_macro_no_uses_expected.sv' ; await macroReplaceTest ( input_file_name , expected_file_name ) ; } ) . timeout ( 10000 ) ; test ( 'test #3: Macro replacement/definition removal for text with single macro definition with multiple macro uses' , async ( ) => { const input_file_name = 'test-files/MacroReplace.test/single_macro_multiple_uses.sv' ; const expected_file_name = 'test-files/MacroReplace.test/single_macro_multiple_uses_expected.sv' ; await macroReplaceTest ( input_file_name , expected_file_name ) ; } ) . timeout ( 10000 ) ; test ( 'test #4: Macro replacement/definition removal for text with multiple macro definitions with multiple macro uses' , async ( ) => { const input_file_name = 'test-files/MacroReplace.test/multiple_macros_multiple_uses.sv' ; const expected_file_name = 'test-files/MacroReplace.test/multiple_macros_multiple_uses_expected.sv' ; await macroReplaceTest ( input_file_name , expected_file_name ) ; } ) . timeout ( 10000 ) ; test ( 'test #5: Macro replacement/definition removal for text with multiple macro definitions interspersed throughout the file' , async ( ) => { const input_file_name = 'test-files/MacroReplace.test/multiple_macros_interspersed.sv' ; const expected_file_name = 'test-files/MacroReplace.test/multiple_macros_interspersed_expected.sv' ; await macroReplaceTest ( input_file_name , expected_file_name ) ; } ) . timeout ( 10000 ) ; test ( 'test #7: Macro replacement/definition removal for text with single multiline macro definition with one use' , async ( ) => { const input_file_name = 'test-files/MacroReplace.test/multiline_macro.sv' ; const expected_file_name = 'test-files/MacroReplace.test/multiline_macro_expected.sv' ; await macroReplaceTest ( input_file_name , expected_file_name ) ; } ) . timeout ( 10000 ) ; } ) ; param input_file param expected_file async function macroReplaceTest ( input_file , expected_file ) { const filePath = path . join ( __dirname , testFolderLocation , input_file ) ; const uriDoc = Uri . file ( filePath ) ; const documentWorkspace = await workspace . openTextDocument ( uriDoc ) ; const document = castTextDocument ( documentWorkspace ) ; const input_text = document . getText ( ) ; const filePath2 = path . join ( __dirname , testFolderLocation , expected_file ) ; const uriDoc2 = Uri . file ( filePath2 ) ; const documentWorkspace2 = await workspace . openTextDocument ( uriDoc2 ) ; const document2 = castTextDocument ( documentWorkspace2 ) ; const expected_text = document2 . getText ( ) . replace ( /\r\n/g , '\n' ) ; const output_text = new ANTLRBackend ( ) . macroReplace ( input_text ) ; if ( output_text !== expected_text ) { assert . fail ( ) ; } } param document returns function castTextDocument ( document ) { return { uri : document . uri . fsPath , languageId : document . languageId , version : document . version , getText ( range ? ) { if ( range ) return document . getText ( castRange ( range ) ) ; return document . getText ( ) ; } , lineCount : document . lineCount , positionAt ( offset ) { const position = document . positionAt ( offset ) ; return { line : position . line , character : position . character } ; } , offsetAt ( position ) { return document . offsetAt ( castPosition ( position ) ) ; } } ; } param document returns function castRange ( range ) { const startOld = range . start ; const endOld = range . end ; const start = new vscode . Position ( startOld . line , startOld . character ) ; const end = new vscode . Position ( endOld . line , endOld . character ) ; return new vscode . Range ( start , end ) ; } param document returns function castPosition ( position ) { return new vscode . Position ( position . line , position . character ) ; }