diff --git runtime/parrot/include/test_more.pir runtime/parrot/include/test_more.pir index 318828b..3afbf74 100644 --- runtime/parrot/include/test_more.pir +++ runtime/parrot/include/test_more.pir @@ -20,7 +20,7 @@ simple test file written in parrot. .local pmc exports, curr_namespace, test_namespace curr_namespace = get_namespace test_namespace = get_root_namespace [ 'parrot'; 'Test'; 'More' ] - exports = split ' ', 'plan diag ok nok is is_deeply like substring isa_ok skip isnt todo throws_like throws_substring' + exports = split ' ', 'plan diag ok nok is is_deeply like substring isa_ok skip isnt todo throws_like lives_ok throws_substring' test_namespace.'export_to'(curr_namespace, exports) diff --git runtime/parrot/library/Test/More.pir runtime/parrot/library/Test/More.pir index de4b7f9..7bde336 100644 --- runtime/parrot/library/Test/More.pir +++ runtime/parrot/library/Test/More.pir @@ -13,7 +13,7 @@ Test::More - Parrot extension for testing modules .local pmc exports, curr_namespace, test_namespace curr_namespace = get_namespace test_namespace = get_namespace [ 'Test'; 'More' ] - exports = split ' ', 'plan diag ok nok is is_deeply like isa_ok skip isnt todo throws_like' + exports = split ' ', 'plan diag ok nok is is_deeply like isa_ok skip isnt todo throws_like lives_ok' test_namespace.'export_to'(curr_namespace, exports) @@ -838,6 +838,55 @@ This handles comparisons of array-like and hash-like structures. .return( equal ) .end +=item C + +Takes PIR code in C and an optional message in C. +Passes a test if the PIR does not throw any exception. + +=cut + +.sub lives_ok + .param string target + .param string description :optional + + .local pmc test + get_hll_global test, [ 'Test'; 'More' ], '_test' + + .local pmc comp + .local pmc compfun + .local pmc compiler + compiler = compreg 'PIR' + + .local pmc eh + eh = new 'ExceptionHandler' + set_addr eh, handler # set handler label for exceptions + push_eh eh + + compfun = compiler(target) + compfun() # eval the target code + + pop_eh + + # if it doesn't throw an exception pass + test.'ok'( 1, description ) + + goto done + + handler: + .local pmc ex + .local string error_msg + .local string diagnostic + + .get_results (ex) + pop_eh + error_msg = ex + test.'ok'( 0, description ) + test.'diag'(error_msg) + + done: + +.end + =item C Takes PIR code in C and a PGE pattern to match in C, as diff --git t/library/test_more.t t/library/test_more.t index d511233..a5ea0a8 100644 --- t/library/test_more.t +++ t/library/test_more.t @@ -15,7 +15,7 @@ .local pmc exports, curr_namespace, test_namespace curr_namespace = get_namespace test_namespace = get_namespace [ 'Test'; 'More' ] - exports = split " ", "ok nok is diag like skip todo is_deeply isa_ok isnt throws_like" + exports = split " ", "ok nok is diag like skip todo is_deeply isa_ok isnt throws_like lives_ok" test_namespace.'export_to'(curr_namespace, exports) test_namespace = get_namespace [ 'Test'; 'Builder'; 'Tester' ] @@ -33,12 +33,51 @@ test_like() test_is_deeply() test_diagnostics() + test_lives_ok() test_throws_like() test_isa_ok() test.'finish'() .end +.sub test_lives_ok + + test_pass( 'lives_ok passes when there is no error' ) + lives_ok( <<'CODE', 'lives_ok passes when there is no error' ) +.sub main + $I0 = 42 +.end +CODE + test_test( 'lives_ok passes when there is no error' ) + + test_fail( 'lives_ok fails when there is an error') + lives_ok( <<'CODE', 'lives_ok fails when there is an error') +.sub main + die 'I did it for the lulz' +.end +CODE + test_diag( 'I did it for the lulz' ) + test_test( 'lives_ok fails when there is an error' ) + + test_pass( 'lives_ok passes when there is no error (with diagnostic message)' ) + lives_ok( <<'CODE', 'lives_ok passes when there is no error (with diagnostic message)' ) +.sub main + $I0 = 42 +.end +CODE + test_diag( '' ) + test_test( 'lives_ok passes when there is no error (with diagnostic message)' ) + + test_fail( 'lives_ok fails when there is an error (with diagnostic message)' ) + lives_ok( <<'CODE', 'lives_ok fails when there is an error (with diagnostic message)' ) +.sub main + die 'I did it for the lulz' +.end +CODE + test_diag( 'I did it for the lulz' ) + test_test( 'lives_ok fails when there is an error' ) +.end + .sub test_throws_like test_fail('throws_like fails when there is no error')