77 4. tests should use TKUnit.assert(condition, message) to mark error. If no assert fails test is successful
88 5. (if exists) at the end of each test tearDown() module function is called
99 6. (if exists) at the end of module test tearDownModule() module function is called
10-
10+
1111*/
1212
1313import * as Application from "tns-core-modules/application" ;
@@ -317,6 +317,12 @@ export function assertAreClose(actual: number, expected: number, delta: number,
317317 }
318318}
319319
320+ export function assertMatches ( actual : string , expected : RegExp , message ?: string ) {
321+ if ( expected . test ( actual ) !== true ) {
322+ throw new Error ( `"${ actual } " doesn't match "${ expected } ". ${ message } ` ) ;
323+ }
324+ }
325+
320326export function arrayAssert ( actual : Array < any > , expected : Array < any > , message ?: string ) {
321327 if ( actual . length !== expected . length ) {
322328 throw new Error ( message + " Actual array length: " + actual . length + " Expected array length: " + expected . length ) ;
@@ -330,6 +336,11 @@ export function arrayAssert(actual: Array<any>, expected: Array<any>, message?:
330336}
331337
332338export function assertThrows ( testFunc : ( ) => void , assertMessage ?: string , expectedMessage ?: string ) {
339+ const re = expectedMessage ? new RegExp ( `^${ expectedMessage } $` ) : null ;
340+ return assertThrowsRegExp ( testFunc , assertMessage , re ) ;
341+ }
342+
343+ export function assertThrowsRegExp ( testFunc : ( ) => void , assertMessage ?: string , expectedMessage ?: RegExp ) {
333344 let actualError : Error ;
334345 try {
335346 testFunc ( ) ;
@@ -341,8 +352,8 @@ export function assertThrows(testFunc: () => void, assertMessage?: string, expec
341352 throw new Error ( "Missing expected exception. " + assertMessage ) ;
342353 }
343354
344- if ( expectedMessage && actualError . message !== expectedMessage ) {
345- throw new Error ( "Got unwanted exception. Actual error: " + actualError . message + " Expected error : " + expectedMessage ) ;
355+ if ( expectedMessage && ! expectedMessage . test ( actualError . message ) ) {
356+ throw new Error ( "Got unwanted exception. Actual error: " + actualError . message + " Expected to match : " + expectedMessage ) ;
346357 }
347358}
348359
@@ -455,4 +466,4 @@ function doModalAndroid(quitLoop: () => boolean, timeoutSec: number, shouldThrow
455466 quit = true ;
456467 }
457468 }
458- }
469+ }
0 commit comments