[svn:parrot] r42233 - in trunk: runtime/parrot/include runtime/parrot/library/Test t/library

dukeleto at svn.parrot.org dukeleto at svn.parrot.org
Tue Nov 3 01:34:48 UTC 2009


Author: dukeleto
Date: Tue Nov  3 01:34:47 2009
New Revision: 42233
URL: https://trac.parrot.org/parrot/changeset/42233

Log:
[t][TT #1183] dies_ok() for Test::More, bubaflub++

Modified:
   trunk/runtime/parrot/include/test_more.pir
   trunk/runtime/parrot/library/Test/More.pir
   trunk/t/library/test_more.t

Modified: trunk/runtime/parrot/include/test_more.pir
==============================================================================
--- trunk/runtime/parrot/include/test_more.pir	Tue Nov  3 01:19:32 2009	(r42232)
+++ trunk/runtime/parrot/include/test_more.pir	Tue Nov  3 01:34:47 2009	(r42233)
@@ -20,7 +20,7 @@
     .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)
 

Modified: trunk/runtime/parrot/library/Test/More.pir
==============================================================================
--- trunk/runtime/parrot/library/Test/More.pir	Tue Nov  3 01:19:32 2009	(r42232)
+++ trunk/runtime/parrot/library/Test/More.pir	Tue Nov  3 01:34:47 2009	(r42233)
@@ -13,7 +13,7 @@
     .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,10 +838,59 @@
     .return( equal )
 .end
 
+=item C<dies_ok( codestring, description )>
+
+Takes PIR code in C<codestring> and an optional message in C<description>.
+Passes a test if the PIR code throws any exception, fails a test otherwise.
+
+=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<lives_ok( codestring, description )>
 
 Takes PIR code in C<codestring> and an optional message in C<description>.
-Passes a test if the PIR does not throw any exception.
+Passes a test if the PIR does not throw any exception, fails a test otherwise.
 
 =cut
 

Modified: trunk/t/library/test_more.t
==============================================================================
--- trunk/t/library/test_more.t	Tue Nov  3 01:19:32 2009	(r42232)
+++ trunk/t/library/test_more.t	Tue Nov  3 01:34:47 2009	(r42233)
@@ -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' )


More information about the parrot-commits mailing list