import * as assert from 'assert' ; import { Labels } from '../src/labels/labels' ; import { readFileSync } from 'fs' ; import { SjasmplusSldLabelParser } from '../src/labels/sjasmplussldlabelparser' ; suite ( 'Labels (sjasmplus)' , ( ) => { setup ( ( ) => { Labels . init ( 250 ) ; } ) ; suite ( 'Labels' , ( ) => { test ( 'Labels' , ( ) => { const labelsFile = readFileSync ( './tests/data/labels/projects/sjasmplus/general/general.labels' ) . toString ( ) . split ( '\n' ) ; const config = { sjasmplus : [ { path : './tests/data/labels/projects/sjasmplus/general/general.sld' , srcDirs : [ "" ] , excludeFiles : [ ] , disableBanking : true } ] } ; 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 = { sjasmplus : [ { path : './tests/data/labels/projects/sjasmplus/general/general.sld' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; const res = Labels . getNumberForLabel ( 'label5' ) ; assert . equal ( undefined , res ) ; } ) ; suite ( 'Sources-Mode' , ( ) => { test ( 'Labels location' , ( ) => { const config = { sjasmplus : [ { path : './tests/data/labels/projects/sjasmplus/general/general.sld' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; let res = Labels . getLocationOfLabel ( 'label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'main.asm' , res . file ) ; assert . equal ( 18 - 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 ( 'modfilea.fa_label2' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea.asm' , res . file ) ; assert . equal ( 6 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'modfilea.fa_label3.mid' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea.asm' , res . file ) ; assert . equal ( 9 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'modfilea.fab_label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea_b.asm' , res . file ) ; assert . equal ( 3 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'modfilea.modfileb.fab_label2' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea_b.asm' , res . file ) ; assert . equal ( 8 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'global_label1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea_b.asm' , res . file ) ; assert . equal ( 12 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'global_label2' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea_b.asm' , res . file ) ; assert . equal ( 14 - 1 , res . lineNr ) ; res = Labels . getLocationOfLabel ( 'modfilea.fab_label_equ1' ) ! ; assert . notEqual ( undefined , res ) ; assert . equal ( 'filea_b.asm' , res . file ) ; assert . equal ( 22 - 1 , res . lineNr ) ; } ) ; test ( 'address -> file/line' , ( ) => { const config = { sjasmplus : [ { path : './tests/data/labels/projects/sjasmplus/general/general.sld' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; let res = Labels . getFileAndLineForAddress ( 0x10000 + 0x8000 ) ; assert . ok ( res . fileName . endsWith ( 'main.asm' ) ) ; assert . equal ( 19 - 1 , res . lineNr ) ; res = Labels . getFileAndLineForAddress ( 0x10000 + 0x9001 ) ; assert . ok ( res . fileName . endsWith ( 'filea.asm' ) ) ; assert . equal ( 7 - 1 , res . lineNr ) ; res = Labels . getFileAndLineForAddress ( 0x10000 + 0x9005 ) ; assert . ok ( res . fileName . endsWith ( 'filea_b.asm' ) ) ; assert . equal ( 4 - 1 , res . lineNr ) ; res = Labels . getFileAndLineForAddress ( 0x10000 + 0x900B ) ; assert . ok ( res . fileName . endsWith ( 'filea.asm' ) ) ; assert . equal ( 17 - 1 , res . lineNr ) ; } ) ; test ( 'file/line -> address' , ( ) => { const config = { sjasmplus : [ { path : './tests/data/labels/projects/sjasmplus/general/general.sld' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; let address = Labels . getAddrForFileAndLine ( 'main.asm' , 19 - 1 ) ; assert . equal ( 0x10000 + 0x8000 , address ) ; address = Labels . getAddrForFileAndLine ( 'filea.asm' , 7 - 1 ) ; assert . equal ( 0x10000 + 0x9001 , address ) ; address = Labels . getAddrForFileAndLine ( 'filea_b.asm' , 4 - 1 ) ; assert . equal ( 0x10000 + 0x9005 , address ) ; address = Labels . getAddrForFileAndLine ( 'filea.asm' , 17 - 1 ) ; assert . equal ( 0x10000 + 0x900B , address ) ; } ) ; } ) ; } ) ; test ( 'Occurence of WPMEM, ASSERTION, LOGPOINT' , ( ) => { const config = { sjasmplus : [ { path : './tests/data/labels/projects/sjasmplus/general/general.sld' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; const wpLines = Labels . getWatchPointLines ( ) ; assert . equal ( wpLines . length , 1 ) ; assert . equal ( wpLines [ 0 ] . address , 0x10000 + 0x8200 ) ; assert . equal ( wpLines [ 0 ] . line , "WPMEM" ) ; const assertionLines = Labels . getAssertionLines ( ) ; assert . equal ( assertionLines . length , 1 ) ; assert . equal ( assertionLines [ 0 ] . address , 0x10000 + 0x8005 ) ; assert . equal ( assertionLines [ 0 ] . line , "ASSERTION" ) ; const lpLines = Labels . getLogPointLines ( ) ; assert . equal ( lpLines . length , 1 ) ; assert . equal ( lpLines [ 0 ] . address , 0x10000 + 0x800F ) ; assert . equal ( lpLines [ 0 ] . line , "LOGPOINT" ) ; } ) ; suite ( 'Self modifying code' , ( ) => { setup ( ( ) => { const config = { sjasmplus : [ { path : './tests/data/labels/projects/sjasmplus/sld_self_modifying_code/main.sld' , srcDirs : [ "" ] , excludeFiles : [ ] } ] } ; Labels . readListFiles ( config ) ; } ) ; test ( 'Start addresses found' , ( ) => { let entry = Labels . getFileAndLineForAddress ( 0x058000 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058100 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058200 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058201 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058203 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058206 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x05820A ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058300 ) ; assert . notEqual ( entry . fileName , '' ) ; } ) ; test ( 'Address ranges (after start address) found' , ( ) => { let entry = Labels . getFileAndLineForAddress ( 0x058001 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058002 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058101 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058102 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058202 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058204 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058205 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058207 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058208 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058209 ) ; assert . notEqual ( entry . fileName , '' ) ; entry = Labels . getFileAndLineForAddress ( 0x058301 ) ; assert . notEqual ( entry . fileName , '' ) ; } ) ; test ( 'addressAdd4' , ( ) => { const sdlParser = new SjasmplusSldLabelParser ( undefined as any , undefined as any , undefined as any , undefined as any , undefined as any , undefined as any , undefined as any , undefined as any , undefined as any ) as any ; sdlParser . bankSize = 0x10000 ; assert . equal ( sdlParser . addressAdd4 ( 0x0000 ) , 0x0004 ) ; assert . equal ( sdlParser . addressAdd4 ( 0xFFFF ) , 0xFFFF ) ; assert . equal ( sdlParser . addressAdd4 ( 0xFFFE ) , 0xFFFF ) ; assert . equal ( sdlParser . addressAdd4 ( 0xFFFD ) , 0xFFFF ) ; assert . equal ( sdlParser . addressAdd4 ( 0xFFFC ) , 0xFFFF ) ; assert . equal ( sdlParser . addressAdd4 ( 0xFFFB ) , 0xFFFF ) ; assert . equal ( sdlParser . addressAdd4 ( 0xFFFA ) , 0xFFFE ) ; sdlParser . bankSize = 0x2000 ; assert . equal ( sdlParser . addressAdd4 ( 0x018000 ) , 0x018004 ) ; assert . equal ( sdlParser . addressAdd4 ( 0x019FFE ) , 0x019FFF ) ; assert . equal ( sdlParser . addressAdd4 ( 0x01FFFF ) , 0x01FFFF ) ; } ) ; } ) ; } ) ;