import * as assert from 'assert' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { formatInstance } from '../providers/ModuleInstantiator' ; const testFolderLocation = '../../src/test' ; suite ( 'ModuleInstantiator Tests' , ( ) => { test ( 'test #1: formatInstance without parameters' , async ( ) => { let uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.1.v' ) ) ; let document = await vscode . workspace . openTextDocument ( uri ) ; let fullRange = new vscode . Range ( document . positionAt ( 585 ) , document . positionAt ( 1129 ) ) ; const container = document . getText ( fullRange ) . replace ( /^\s+|\s+$/g , '' ) ; uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.2.v' ) ) ; document = await vscode . workspace . openTextDocument ( uri ) ; fullRange = new vscode . Range ( document . positionAt ( 152 ) , document . positionAt ( 354 ) ) ; let instance = document . getText ( fullRange ) . trim ( ) ; instance = instance . replace ( / +/g , ' ' ) ; let actual_instance ; try { actual_instance = formatInstance ( 'adder' , container ) . trim ( ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } actual_instance = actual_instance . replace ( / +/g , ' ' ) ; assert . strictEqual ( actual_instance , instance ) ; } ) ; test ( 'test #2: formatInstance with parameters' , async ( ) => { let uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.1.v' ) ) ; let document = await vscode . workspace . openTextDocument ( uri ) ; let fullRange = new vscode . Range ( document . positionAt ( 1278 ) , document . positionAt ( 1872 ) ) ; let container = document . getText ( fullRange ) . replace ( /^\s+|\s+$/g , '' ) ; container = container . replace ( / +/g , ' ' ) ; uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.2.v' ) ) ; document = await vscode . workspace . openTextDocument ( uri ) ; fullRange = new vscode . Range ( document . positionAt ( 506 ) , document . positionAt ( 785 ) ) ; let instance = document . getText ( fullRange ) . trim ( ) ; instance = instance . replace ( / +/g , ' ' ) ; let actual_instance ; try { actual_instance = formatInstance ( 'bar' , container ) . trim ( ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } actual_instance = actual_instance . replace ( / +/g , ' ' ) ; assert . strictEqual ( actual_instance , instance ) ; } ) ; test ( 'test #3: formatInstance without parameters, ports in header' , async ( ) => { let uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.1.v' ) ) ; let document = await vscode . workspace . openTextDocument ( uri ) ; let fullRange = new vscode . Range ( document . positionAt ( 2041 ) , document . positionAt ( 2611 ) ) ; let container = document . getText ( fullRange ) . replace ( /^\s+|\s+$/g , '' ) ; container = container . replace ( / +/g , ' ' ) ; uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.2.v' ) ) ; document = await vscode . workspace . openTextDocument ( uri ) ; fullRange = new vscode . Range ( document . positionAt ( 954 ) , document . positionAt ( 1071 ) ) ; let instance = document . getText ( fullRange ) . trim ( ) ; instance = instance . replace ( / +/g , ' ' ) ; let actual_instance ; try { actual_instance = formatInstance ( 'akker' , container ) . trim ( ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } actual_instance = actual_instance . replace ( / +/g , ' ' ) ; assert . strictEqual ( actual_instance , instance ) ; } ) ; test ( 'test #4: formatInstance with parameters, ports in header' , async ( ) => { let uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.1.v' ) ) ; let document = await vscode . workspace . openTextDocument ( uri ) ; let fullRange = new vscode . Range ( document . positionAt ( 2777 ) , document . positionAt ( 3398 ) ) ; let container = document . getText ( fullRange ) . replace ( /^\s+|\s+$/g , '' ) ; container = container . replace ( / +/g , ' ' ) ; uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.2.v' ) ) ; document = await vscode . workspace . openTextDocument ( uri ) ; fullRange = new vscode . Range ( document . positionAt ( 1240 ) , document . positionAt ( 1407 ) ) ; let instance = document . getText ( fullRange ) . trim ( ) ; instance = instance . replace ( / +/g , ' ' ) ; let actual_instance ; try { actual_instance = formatInstance ( 'accer' , container ) . trim ( ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } actual_instance = actual_instance . replace ( / +/g , ' ' ) ; assert . strictEqual ( actual_instance , instance ) ; } ) ; test ( 'test #5: formatInstance with defaulted parameters.' , async ( ) => { let uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.1.v' ) ) ; let document = await vscode . workspace . openTextDocument ( uri ) ; let fullRange = new vscode . Range ( document . positionAt ( 3597 ) , document . positionAt ( 3942 ) ) ; let container = document . getText ( fullRange ) . replace ( /^\s+|\s+$/g , '' ) ; container = container . replace ( / +/g , ' ' ) ; uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.2.v' ) ) ; document = await vscode . workspace . openTextDocument ( uri ) ; fullRange = new vscode . Range ( document . positionAt ( 1590 ) , document . positionAt ( 1747 ) ) ; let instance = document . getText ( fullRange ) . trim ( ) ; instance = instance . replace ( / +/g , ' ' ) ; let actual_instance ; try { actual_instance = formatInstance ( 'anner' , container ) . trim ( ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } actual_instance = actual_instance . replace ( / +/g , ' ' ) ; assert . strictEqual ( actual_instance , instance ) ; } ) ; test ( 'test #6: empty and undefined container/symbol scenarios' , async ( ) => { let actual_instance ; try { actual_instance = formatInstance ( 'bar' , '' ) ; assert . strictEqual ( actual_instance , undefined ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } try { actual_instance = formatInstance ( 'bar' , undefined ) ; assert . strictEqual ( actual_instance , undefined ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } const uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.1.v' ) ) ; const document = await vscode . workspace . openTextDocument ( uri ) ; const fullRange = new vscode . Range ( document . positionAt ( 1293 ) , document . positionAt ( 1899 ) ) ; let container = document . getText ( fullRange ) . replace ( /^\s+|\s+$/g , '' ) ; container = container . replace ( / +/g , ' ' ) ; try { actual_instance = formatInstance ( '' , container ) ; assert . strictEqual ( actual_instance , undefined ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } try { actual_instance = formatInstance ( undefined , container ) ; assert . strictEqual ( actual_instance , undefined ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } try { actual_instance = formatInstance ( '' , '' ) ; assert . strictEqual ( actual_instance , undefined ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } try { actual_instance = formatInstance ( undefined , undefined ) ; assert . strictEqual ( actual_instance , undefined ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } try { actual_instance = formatInstance ( 'bar' , null ) ; assert . strictEqual ( actual_instance , undefined ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } try { actual_instance = formatInstance ( null , null ) ; assert . strictEqual ( actual_instance , undefined ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } } ) ; test ( 'test #7: formatInstance golden output.' , async ( ) => { let uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.1.v' ) ) ; let document = await vscode . workspace . openTextDocument ( uri ) ; let fullRange = new vscode . Range ( document . positionAt ( 5269 ) , document . positionAt ( 5814 ) ) ; let container = document . getText ( fullRange ) . replace ( /^\s+|\s+$/g , '' ) ; container = container . replace ( / +/g , ' ' ) ; uri = vscode . Uri . file ( path . join ( __dirname , testFolderLocation , 'test-files' , 'ModuleInstantiator.test.2.v' ) ) ; document = await vscode . workspace . openTextDocument ( uri ) ; fullRange = new vscode . Range ( document . positionAt ( 2636 ) , document . positionAt ( 2856 ) ) ; const instance = document . getText ( fullRange ) . trim ( ) ; let actual_instance ; try { actual_instance = formatInstance ( 'golden' , container ) . trim ( ) ; } catch ( error ) { assert . fail ( `formatInstance produced an error: ${ error }` ) ; } assert . strictEqual ( actual_instance , instance ) ; } ) ; } ) ;