import * as assert from 'assert' ; import { Labels } from '../src/labels/labels' ; import { readFileSync } from 'fs' ; suite ( 'Labels (z80asm)' , ( ) => { setup ( ( ) => { Labels . init ( 250 ) ; } ) ; suite ( 'Labels' , ( ) => { test ( 'Labels' , ( ) => { const labelsFile = readFileSync ( './tests/data/labels/projects/z80asm/general/general.labels' ) . toString ( ) . split ( '\n' ) ; const config = { z80asm : [ { path : './tests/data/labels/projects/z80asm/general/general.list' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; for ( const labelLine of labelsFile ) { if ( labelLine == '' ) continue ; const match = /(.*):\s+equ\s+\$(.*)/i . exec ( labelLine ) ! ; assert . notEqual ( undefined , match ) ; const label = match [ 1 ] ; const value = parseInt ( match [ 2 ] , 16 ) ; const res = Labels . getNumberForLabel ( label ) ; assert . equal ( value , res ) ; } } ) ; test ( 'IF 0 Labels' , ( ) => { const config = { z80asm : [ { path : './tests/data/labels/projects/z80asm/general/general.list' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; const res = Labels . getNumberForLabel ( 'label5' ) ; assert . equal ( 0x8012 , res ) ; } ) ; suite ( 'ListFile-Mode' , ( ) => { test ( 'Labels location' , ( ) => { const fname = './tests/data/labels/projects/z80asm/general/general.list' ; const config = { z80asm : [ { path : fname , srcDirs : [ ] } ] } ; Labels . readListFiles ( config ) ; let res = Labels . getLocationOfLabel ( 'label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( fname , res . file ) ; assert . equal ( 16 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'fa_label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( fname , res . file ) ; assert . equal ( 68 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'global_label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( fname , res . file ) ; assert . equal ( 87 - 1 , res . lineNr ) ; } ) ; test ( 'address -> file/line' , ( ) => { const listFile = readFileSync ( './tests/data/labels/projects/z80asm/general/general.list' ) . toString ( ) . split ( '\n' ) ; const config = { z80asm : [ { path : './tests/data/labels/projects/z80asm/general/general.list' , srcDirs : [ ] } ] } ; Labels . readListFiles ( config ) ; const count = listFile . length ; for ( let lineNr = 0 ; lineNr < count ; lineNr ++ ) { const line = listFile [ lineNr ] ; const match = /^([0-9a-f]+)\s[0-9a-f]+/i . exec ( line ) ; if ( ! match ) continue ; const address = parseInt ( match [ 1 ] , 16 ) ; const res = Labels . getFileAndLineForAddress ( address ) ; assert . ok ( res . fileName . endsWith ( 'general.list' ) ) ; assert . equal ( lineNr , res . lineNr ) ; } } ) ; test ( 'file/line -> address' , ( ) => { const filename = './tests/data/labels/projects/z80asm/general/general.list' ; const listFile = readFileSync ( filename ) . toString ( ) . split ( '\n' ) ; const config = { z80asm : [ { path : filename , srcDirs : [ ] } ] } ; Labels . readListFiles ( config ) ; const count = listFile . length ; for ( let lineNr = 0 ; lineNr < count ; lineNr ++ ) { const line = listFile [ lineNr ] ; const match = /^([0-9a-f]+)\s[0-9a-f]+/i . exec ( line ) ; if ( ! match ) continue ; const address = parseInt ( match [ 1 ] , 16 ) ; let resultAddr = Labels . getAddrForFileAndLine ( filename , lineNr ) ; assert . equal ( address , resultAddr ) ; } } ) ; } ) ; suite ( 'Sources-Mode' , ( ) => { test ( 'Labels location' , ( ) => { const config = { z80asm : [ { path : './tests/data/labels/projects/z80asm/general/general.list' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; let res = Labels . getLocationOfLabel ( 'label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'main.asm' , res . file ) ; assert . equal ( 15 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'fa_label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea.asm' , res . file ) ; assert . equal ( 2 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'global_label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea_b.asm' , res . file ) ; assert . equal ( 10 - 1 , res . lineNr ) ; } ) ; test ( 'address -> file/line' , ( ) => { const config = { z80asm : [ { path : './tests/data/labels/projects/z80asm/general/general.list' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; let res = Labels . getFileAndLineForAddress ( 0x8000 ) ; assert . ok ( res . fileName . endsWith ( 'main.asm' ) ) ; assert . equal ( 16 - 1 , res . lineNr ) ; res = Labels . getFileAndLineForAddress ( 0x9001 ) ; assert . ok ( res . fileName . endsWith ( 'filea.asm' ) ) ; assert . equal ( 7 - 1 , res . lineNr ) ; res = Labels . getFileAndLineForAddress ( 0x9005 ) ; assert . ok ( res . fileName . endsWith ( 'filea_b.asm' ) ) ; assert . equal ( 11 - 1 , res . lineNr ) ; res = Labels . getFileAndLineForAddress ( 0x9008 ) ; assert . ok ( res . fileName . endsWith ( 'filea.asm' ) ) ; assert . equal ( 16 - 1 , res . lineNr ) ; } ) ; test ( 'file/line -> address' , ( ) => { const config = { z80asm : [ { path : './tests/data/labels/projects/z80asm/general/general.list' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; let address = Labels . getAddrForFileAndLine ( 'main.asm' , 16 - 1 ) ; assert . equal ( 0x8000 , address ) ; address = Labels . getAddrForFileAndLine ( 'filea.asm' , 7 - 1 ) ; assert . equal ( 0x9001 , address ) ; address = Labels . getAddrForFileAndLine ( 'filea_b.asm' , 7 - 1 ) ; assert . equal ( 0x9004 , address ) ; address = Labels . getAddrForFileAndLine ( 'filea.asm' , 16 - 1 ) ; assert . equal ( 0x9008 , address ) ; } ) ; } ) ; } ) ; test ( 'Occurence of WPMEM, ASSERTION, LOGPOINT' , ( ) => { const config = { z80asm : [ { path : './tests/data/labels/projects/z80asm/general/general.list' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; const wpLines = Labels . getWatchPointLines ( ) ; assert . equal ( wpLines . length , 1 ) ; assert . equal ( wpLines [ 0 ] . address , 0x8200 ) ; assert . equal ( wpLines [ 0 ] . line , "WPMEM" ) ; const assertionLines = Labels . getAssertionLines ( ) ; assert . equal ( assertionLines . length , 1 ) ; assert . equal ( assertionLines [ 0 ] . address , 0x8005 ) ; assert . equal ( assertionLines [ 0 ] . line , "ASSERTION" ) ; const lpLines = Labels . getLogPointLines ( ) ; assert . equal ( lpLines . length , 1 ) ; assert . equal ( lpLines [ 0 ] . address , 0x800F ) ; assert . equal ( lpLines [ 0 ] . line , "LOGPOINT" ) ; } ) ; } ) ;