diff --git runtime/parrot/include/test_more.pir runtime/parrot/include/test_more.pir index 3afbf74..e368535 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 lives_ok throws_substring' + exports = split ' ', 'plan diag ok nok is is_deeply like substring isa_ok skip isnt todo throws_like lives_ok dies_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 fa4d271..c34026f 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 lives_ok' + exports = split ' ', 'plan diag ok nok is is_deeply like isa_ok skip isnt todo throws_like lives_ok dies_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 throw any exception. + +=cut + +.sub dies_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 fail + test.'ok'( 0, description ) + test.'diag'('no error thrown') + + goto done + + handler: + .local pmc ex + .local string error_msg + .local string diagnostic + + .get_results (ex) + pop_eh + error_msg = ex + test.'ok'( 1, description ) + + done: + +.end + =item C Takes PIR code in C and an optional message in C. diff --git t/library/test_more.t t/library/test_more.t index 00bc89a..b4fa044 100644 --- t/library/test_more.t +++ t/library/test_more.t @@ -15,14 +15,14 @@ .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 lives_ok" + exports = split " ", "ok nok is diag like skip todo is_deeply isa_ok isnt throws_like lives_ok dies_ok" test_namespace.'export_to'(curr_namespace, exports) test_namespace = get_namespace [ 'Test'; 'Builder'; 'Tester' ] exports = split " ", "plan test_out test_diag test_fail test_pass test_test" test_namespace.'export_to'(curr_namespace, exports) - plan( 98 ) + plan( 102 ) test_skip() test_todo() @@ -34,12 +34,51 @@ test_is_deeply() test_diagnostics() test_lives_ok() + test_dies_ok() test_throws_like() test_isa_ok() test.'finish'() .end +.sub test_dies_ok + test_pass( 'dies_ok passes when there is an error' ) + dies_ok( <<'CODE', 'dies_ok passes when there is an error' ) +.sub main + die 'I did it for the lulz' +.end +CODE + test_test( 'dies_ok passes when there is an error' ) + + test_fail( 'dies_ok fails when there is no error' ) + dies_ok( <<'CODE', 'dies_ok fails when there is no error' ) +.sub main + $I0 = 42 +.end +CODE + test_diag( 'no error thrown' ) + test_test( 'dies_ok fails when there is no error' ) + + test_pass( 'dies_ok passes when there is an error with diagnostic message' ) + dies_ok( <<'CODE', 'dies_ok passes when there is an error with diagnostic message' ) +.sub main + die 'I did it for the lulz' +.end +CODE + test_diag( '' ) + test_test( 'dies_ok passes when there is an error with diagnostic message' ) + + test_fail( 'dies_ok fails when there is no error with diagnostic message' ) + dies_ok( <<'CODE', 'dies_ok fails when there is no error with diagnostic message' ) +.sub main + $I0 = 42 +.end +CODE + test_diag( 'no error thrown' ) + test_test( 'dies_ok fails when there is no error with diagnostic message' ) + +.end + .sub test_lives_ok test_pass( 'lives_ok passes when there is no error' )