2 # Copyright (C) 2005-2010, Parrot Foundation.
6 load_bytecode 'Test/Builder/Tester.pbc'
7 load_bytecode 'Test/More.pbc'
13 test = new [ 'Test'; 'Builder' ], tb_args
15 .local pmc exports, curr_namespace, test_namespace
16 curr_namespace = get_namespace
17 test_namespace = get_namespace [ 'Test'; 'More' ]
18 exports = split " ", "ok nok is diag like skip todo is_deeply is_null isa_ok isnt throws_like lives_ok dies_ok"
19 test_namespace.'export_to'(curr_namespace, exports)
21 test_namespace = get_namespace [ 'Test'; 'Builder'; 'Tester' ]
22 exports = split " ", "plan test_out test_diag test_fail test_pass test_test"
23 test_namespace.'export_to'(curr_namespace, exports)
46 test_pass( 'dies_ok passes when there is an error' )
47 dies_ok( <<'CODE', 'dies_ok passes when there is an error' )
49 die 'I did it for the lulz'
52 test_test( 'dies_ok passes when there is an error' )
54 test_fail( 'dies_ok fails when there is no error' )
55 dies_ok( <<'CODE', 'dies_ok fails when there is no error' )
60 test_diag( 'no error thrown' )
61 test_test( 'dies_ok fails when there is no error' )
63 test_pass( 'dies_ok passes when there is an error with diagnostic message' )
64 dies_ok( <<'CODE', 'dies_ok passes when there is an error with diagnostic message' )
66 die 'I did it for the lulz'
70 test_test( 'dies_ok passes when there is an error with diagnostic message' )
72 test_fail( 'dies_ok fails when there is no error with diagnostic message' )
73 dies_ok( <<'CODE', 'dies_ok fails when there is no error with diagnostic message' )
78 test_diag( 'no error thrown' )
79 test_test( 'dies_ok fails when there is no error with diagnostic message' )
85 test_pass( 'lives_ok passes when there is no error' )
86 lives_ok( <<'CODE', 'lives_ok passes when there is no error' )
91 test_test( 'lives_ok passes when there is no error' )
93 test_fail( 'lives_ok fails when there is an error')
94 lives_ok( <<'CODE', 'lives_ok fails when there is an error')
96 die 'I did it for the lulz'
99 test_diag( 'I did it for the lulz' )
100 test_test( 'lives_ok fails when there is an error' )
102 test_pass( 'lives_ok passes when there is no error (with diagnostic message)' )
103 lives_ok( <<'CODE', 'lives_ok passes when there is no error (with diagnostic message)' )
109 test_test( 'lives_ok passes when there is no error (with diagnostic message)' )
111 test_fail( 'lives_ok fails when there is an error (with diagnostic message)' )
112 lives_ok( <<'CODE', 'lives_ok fails when there is an error (with diagnostic message)' )
114 die 'I did it for the lulz'
117 test_diag( 'I did it for the lulz' )
118 test_test( 'lives_ok fails when there is an error' )
121 .sub test_throws_like
123 test_fail('throws_like fails when there is no error')
124 throws_like( <<'CODE', 'somejunk', 'throws_like fails when there is no error')
129 test_diag( 'no error thrown' )
130 test_test( 'throws_like fails when there is no error')
132 test_pass('throws_like passes when error matches pattern')
133 throws_like( <<'CODE', 'for\ the\ lulz','throws_like passes when error matches pattern')
135 die 'I did it for the lulz'
138 test_test( 'throws_like passes when error matches pattern' )
140 test_fail( 'throws_like fails when error does not match pattern' )
141 throws_like( <<'CODE', 'for\ the\ lulz','throws_like fails when error does not match pattern')
146 .local string diagnostic
147 diagnostic = "match failed: target 'DO NOT WANT' does not match pattern '"
148 diagnostic .= 'for\ the\ lulz'
150 test_diag( diagnostic )
151 test_test('throws_like fails when error does not match pattern' )
155 .namespace ['MyFalseClass']
157 .sub '' :anon :load :init
158 $P0 = newclass ['MyFalseClass']
161 .sub 'get_bool' :vtable
165 .sub 'get_integer' :vtable
169 .namespace ['MyTrueClass']
171 .sub '' :anon :load :init
172 $P0 = newclass ['MyTrueClass']
175 .sub 'get_bool' :vtable
179 .sub 'get_integer' :vtable
188 test_test( 'passing test ok()')
192 test_test( 'failing test ok()')
194 test_pass( 'with description' )
195 ok( 1, 'with description' )
196 test_test( 'passing test ok() with description')
198 test_fail( 'with description' )
199 ok( 0, 'with description' )
200 test_test( 'failing test ok() with description')
202 $P0 = new ['MyFalseClass']
205 test_test( 'failing ok() calls get_bool')
207 $P0 = new ['MyTrueClass']
210 test_test( 'passing ok() calls get_bool')
216 test_test( 'failing test nok()')
220 test_test( 'passing test nok()')
222 test_fail( 'with description' )
223 nok( 1, 'with description' )
224 test_test( 'failing test nok() with description')
226 test_pass( 'with description' )
227 nok( 0, 'with description' )
228 test_test( 'passing test nok() with description')
230 $P0 = new ['MyFalseClass']
233 test_test( 'passing nok() calls get_bool')
235 $P0 = new ['MyTrueClass']
238 test_test( 'failing nok() calls get_bool')
244 test_test( 'passing test is() for ints')
247 test_diag( 'Have: -100' )
248 test_diag( 'Want: 200' )
250 test_test( 'failing test is() for ints')
252 test_pass( 'comparing two integers' )
253 is( 512, 512, 'comparing two integers' )
254 test_test( 'passing test is() for ints with description')
256 test_fail( 'comparing two integers' )
257 is( -512, 5120, 'comparing two integers' )
258 test_diag( 'Have: -512' )
259 test_diag( 'Want: 5120' )
260 test_test( 'failing test is() for ints with description')
264 test_test( 'passing test is() for floats')
268 test_diag( 'Have: 1.235' )
269 test_diag( 'Want: 5.321' )
270 test_test( 'failing test is() for floats')
272 test_pass( 'comparing two floats' )
273 is( 80.80, 80.80, 'comparing two floats' )
274 test_test( 'passing test is() for floats with description')
276 test_fail( 'comparing two floats' )
277 is( 777.1, 888.8, 'comparing two floats' )
278 test_diag( 'Have: 777.1' )
279 test_diag( 'Want: 888.8' )
280 test_test( 'failing test is() for floats with description')
282 test_fail( 'comparing two floats with precision, failure' )
283 is( 777.1, 888.8, 'comparing two floats with precision, failure', 1e-6)
284 test_diag( 'Have: 777.1' )
285 test_diag( 'Want: 888.8' )
286 test_test( 'failing test is() for floats with precision')
288 test_pass( 'comparing two floats with precision, success' )
289 is( 666.222, 666.223, 'comparing two floats with precision, success', 1e-2)
290 test_test( 'passing test is() for floats with precision')
292 test_fail( 'comparing Float PMC and a float with precision, failure' )
295 is( $P0, 888.82, 'comparing Float PMC and a float with precision, failure', 1e-2)
296 test_diag( 'Have: 888.81' )
297 test_diag( 'Want: 888.82' )
298 test_test( 'failing test is() for comparing a Float PMC and float with precision')
300 test_pass( 'comparing Float PMC and a float with precision, success' )
302 is( $P0, 666.223, 'comparing Float PMC and a float with precision, success', 1e-2)
303 test_test( 'passing test is() for comparing Float PMC and float with precision')
305 test_pass( 'comparing integer and a Float PMC with precision, success' )
307 is( $I0, 41.99, 'comparing integer and a Float PMC with precision, success', 0.1)
308 test_test( 'passing test is() for comparing Integer PMC and float with precision')
310 test_fail( 'comparing integer and a Float PMC with precision, failure' )
312 is( $I0, 41.99, 'comparing integer and a Float PMC with precision, failure', 1e-3)
313 test_diag( 'Have: 42' )
314 test_diag( 'Want: 41.99' )
315 test_test( 'failing test is() for comparing Integer PMC and float with precision')
319 test_test( 'passing test is() for strings')
323 test_diag( 'Have: larry' )
324 test_diag( 'Want: bob' )
325 test_test( 'failing test is() for strings')
327 test_pass( 'comparing two strings' )
328 is( 'larry', 'larry', 'comparing two strings' )
329 test_test( 'passing test is() for strings with description')
331 test_fail( 'comparing two strings' )
332 is( 'zeke', 'zelda', 'comparing two strings' )
333 test_diag( 'Have: zeke' )
334 test_diag( 'Want: zelda' )
335 test_test( 'failing test is() for strings with description')
346 test_test( 'passing test is() for pmcs')
348 right = new 'Integer'
350 test_fail( 'zero versus 1' )
351 test_diag( 'Have: zero' )
352 test_diag( 'Want: 1' )
353 is( left, right, 'zero versus 1' )
354 test_test( 'failing test is() for pmcs')
357 test_pass( 'comparing two pmcs' )
358 is( left, right, 'comparing two pmcs' )
359 test_test( 'passing test is() for pmcs with description')
363 .local string expected
364 .local string hash_string
367 expected .= hash_string
369 test_fail( 'comparing two pmcs' )
370 test_diag( 'Have: 1' )
371 test_diag( expected )
372 is( left, right, 'comparing two pmcs' )
373 test_test( 'failing test is() for pmcs with description' )
375 test_pass( 'comparing two nulls' )
378 is( left, right, 'comparing two nulls' )
379 test_test( 'passing test is() for nulls')
381 test_fail('comparing null with Int')
384 is( left, right, 'comparing null with Int')
385 test_diag( 'Have: null' )
386 test_diag( 'Want: 1' )
387 test_test('failing test is() for null vs. pmc 1')
389 test_fail('comparing Int with null')
392 is( left, right, 'comparing Int with null')
393 test_diag( 'Have: 10' )
394 test_diag( 'Want: null' )
395 test_test('failing test is() for pmc 10 vs. null')
397 test_fail('comparing null with Float')
400 is( left, right, 'comparing null with Float')
401 test_diag( 'Have: null' )
402 test_diag( 'Want: 1.01' )
403 test_test('failing test is() for null vs. pmc 1.01')
405 test_fail('comparing Float with null')
408 is( left, right, 'comparing Float with null')
409 test_diag( 'Have: 2.787' )
410 test_diag( 'Want: null' )
411 test_test('failing test is() for pmc 2.787 vs. null')
413 test_fail('comparing null with String')
415 right = box 'September, when it comes'
416 is( left, right, 'comparing null with String')
417 test_diag( 'Have: null' )
418 test_diag( 'Want: September, when it comes' )
419 test_test('failing test is() for null vs. String pmc')
421 test_fail('comparing String with null')
422 left = box 'I cannot move a mountain now'
424 is( left, right, 'comparing String with null')
425 test_diag( 'Have: I cannot move a mountain now' )
426 test_diag( 'Want: null' )
427 test_test('failing test is() for String pmc vs. null')
434 test_test( 'passing test isnt() for ints')
437 test_diag( 'Have: -100' )
438 test_diag( 'Want: not -100' )
440 test_test( 'failing test isnt() for ints')
442 test_pass( 'comparing two unidentical integers' )
443 isnt( 512, 215, 'comparing two unidentical integers' )
444 test_test( 'passing test isnt() for ints with description')
446 test_fail( 'comparing two integers' )
447 isnt( -512, -512, 'comparing two integers' )
448 test_diag( 'Have: -512' )
449 test_diag( 'Want: not -512' )
450 test_test( 'failing test isnt() for ints with description')
454 test_test( 'passing test isnt() for floats')
458 test_diag( 'Have: 1.235' )
459 test_diag( 'Want: not 1.235' )
460 test_test( 'failing test isnt() for floats')
462 test_pass( 'comparing two floats' )
463 isnt( 777.1, 888.8, 'comparing two floats' )
464 test_test( 'passing test isnt() for floats with description')
466 test_fail( 'comparing two floats' )
467 isnt( 80.8, 80.8, 'comparing two floats' )
468 test_diag( 'Have: 80.8' )
469 test_diag( 'Want: not 80.8' )
470 test_test( 'failing test isnt() for floats with description')
474 test_test( 'passing test isnt() for strings')
477 isnt( 'larry', 'larry' )
478 test_diag( 'Have: larry' )
479 test_diag( 'Want: not larry' )
480 test_test( 'failing test isnt() for strings')
482 test_pass( 'comparing two strings' )
483 isnt( 'zeke', 'zelda', 'comparing two strings' )
484 test_test( 'passing test isnt() for strings with description')
486 test_fail( 'comparing two strings' )
487 isnt( 'larry', 'larry', 'comparing two strings' )
488 test_diag( 'Have: larry' )
489 test_diag( 'Want: not larry' )
490 test_test( 'failing test isnt() for strings with description')
501 test_test( 'passing test isnt() for pmcs')
506 test_diag( 'Have: zero' )
507 test_diag( 'Want: not zero' )
509 test_test( 'failing test isnt() for pmcs')
512 test_pass( 'comparing two pmcs' )
513 isnt( left, right, 'comparing two pmcs' )
514 test_test( 'passing test isnt() for pmcs with description')
518 .local string expected
519 .local string hash_string
522 expected .= hash_string
524 test_pass( 'comparing two pmcs' )
525 isnt( left, right, 'comparing two pmcs' )
526 test_test( 'failing test isnt() for pmcs with description')
531 like( 'abcdef', '<[c]>' )
532 test_test( 'passing test like()' )
534 test_pass( 'testing like()' )
535 like( 'abcdef', '<[c]>', 'testing like()' )
536 test_test( 'passing test like() with description' )
540 test_diag( "match failed: target 'abcdef' does not match pattern '<[g]>'" )
541 like( 'abcdef', '<[g]>' )
542 test_test( 'failing test like()' )
544 test_fail( 'testing like()' )
545 test_diag( "match failed: target 'abcdef' does not match pattern '<[g]>'" )
546 like( 'abcdef', '<[g]>', 'testing like()' )
547 test_test( 'failing test like() with description' )
549 test_pass( 'like() can match literal strings' )
550 like( 'foobar', 'foobar', 'like() can match literal strings' )
551 test_test( 'like() can match literal strings' )
553 test_pass( 'like() can match partial literal strings' )
554 like( 'foobar()', 'foobar', 'like() can match partial literal strings' )
555 test_test( 'like() can match partial literal strings' )
557 test_pass( 'like() can match partial literal strings with spaces' )
558 like( 'foo bar()', 'foo\ bar', 'like() can match partial literal strings with spaces' )
559 test_test( 'like() can match partial literal strings with spaces' )
563 test_is_deeply_array()
564 test_is_deeply_hash()
565 test_is_deeply_hash_tt763()
566 test_is_deeply_mismatch()
567 test_is_deeply_nested()
570 .sub test_is_deeply_array
573 left = new 'ResizablePMCArray'
574 right = new 'ResizablePMCArray'
582 is_deeply( left, right )
583 test_test( 'passing test is_deeply() for pmc arrays' )
585 test_pass( 'comparing two pmc arrays' )
586 is_deeply( left, right, 'comparing two pmc arrays' )
587 test_test( 'passing test is_deeply() for pmc arrays with description' )
592 test_diag( 'Mismatch: expected 3 elements, received 2' )
593 is_deeply( left, right )
594 test_test( 'failing test is_deeply() for pmc arrays' )
596 test_fail( 'comparing two pmc arrays' )
597 test_diag( 'Mismatch: expected 3 elements, received 2' )
598 is_deeply( left, right, 'comparing two pmc arrays' )
599 test_test( 'failing test is_deeply() for pmc arrays with description' )
603 test_diag( 'Mismatch at [2]: expected 9 - 2, received 9 - 3' )
604 is_deeply( left, right )
605 test_test( 'failing test is_deeply() for item mismatch at position' )
607 test_fail( 'comparing mismatch of elements' )
608 test_diag( 'Mismatch at [2]: expected 9 - 2, received 9 - 3' )
609 is_deeply( left, right, 'comparing mismatch of elements' )
610 test_test( 'failing test is_deeply() for item mismatch at position' )
613 .sub test_is_deeply_hash
625 is_deeply( left, right )
626 test_test( 'passing test is_deeply() for empty pmc hashes' )
628 test_pass( 'empty hashes match' )
629 is_deeply( left, right, 'empty hashes match' )
630 test_test( 'passing test is_deeply() for empty pmc hashes with diagnostic' )
635 is_deeply( left, right )
636 test_diag( 'Mismatch: expected 1 element, received 0' )
637 test_test( 'failing is_deeply() for hashes with different numbers of keys' )
642 test_fail( 'more diag' )
643 is_deeply( left, right, 'more diag' )
644 test_diag( 'Mismatch: expected 2 elements, received 1' )
645 test_test( '... with description and proper pluralization' )
650 is_deeply( left, right )
651 test_diag( 'Mismatch at [foo]: expected 1, received 2' )
652 test_test( 'failing is_deeply() for hash with value mismatch' )
654 test_fail( '2 is not 1' )
655 is_deeply( left, right, '2 is not 1' )
656 test_diag( 'Mismatch at [foo]: expected 1, received 2' )
657 test_test( '... with description' )
661 is_deeply( left, right )
662 test_test( 'passing test is_deeply() for hashes created in different orders' )
665 .sub test_is_deeply_hash_tt763
675 right['undef1'] = undef1
676 left['undef2'] = undef2
679 is_deeply( left, right )
680 test_diag( 'Mismatch at [undef2]: expected (undef), received nonexistent' )
681 test_test( 'failing is_deeply() for undef in left, nonexistent in right' )
684 is_deeply( right, left )
685 test_diag( 'Mismatch at [undef1]: expected (undef), received nonexistent' )
686 test_test( 'failing is_deeply() for undef in left, nonexistent in right' )
688 right['undef2'] = undef2
689 left['undef1'] = undef1
692 is_deeply( left, right )
693 test_test( 'passing is_deeply() with undef values' )
697 is_deeply( left, right )
698 test_diag( 'Mismatch: expected 3 elements, received 2')
699 test_test( 'failing is_deeply() for hashes differing by keys with undef values' )
701 right['bar'] = undef1
703 is_deeply( left, right )
704 test_diag( 'Mismatch at [foo]: expected (undef), received nonexistent')
705 test_test( 'failing is_deeply() for hashes differing by keys with undef values' )
708 .sub test_is_deeply_mismatch
711 .sub test_is_deeply_nested
718 .local pmc left_array
719 .local pmc right_array
720 left_array = new 'ResizablePMCArray'
722 right_array = new 'ResizablePMCArray'
725 left[ 'array' ] = left_array
726 right[ 'array' ] = right_array
732 is_deeply( left, right )
733 test_test( 'Nested data structure equality' )
735 test_pass( '... are they equal?' )
736 is_deeply( left, right, '... are they equal?' )
737 test_test( '... with description' )
742 is_deeply( left, right )
743 test_diag( 'Mismatch at [array][2]: expected (undef), received 2' )
744 test_test( 'Nested data structure inequality' )
746 test_fail( '... are they inequal?' )
747 is_deeply( left, right, '... are they inequal?' )
748 test_diag( 'Mismatch at [array][2]: expected (undef), received 2' )
749 test_test( '... with description' )
752 .sub test_diagnostics
755 diag( 'foo bar baz' )
756 test_diag( 'foo bar baz' )
757 test_test( 'single line diagnostics' )
761 diag( 'foo bar baz' )
762 diag( 'rum tum tugger')
763 test_diag( "foo bar baz\nrum tum tugger" )
764 test_test( 'multi line diagnostics' )
770 test = new [ 'Test'; 'Builder' ]
772 test_out( 'ok 1 #skip skipping' )
773 test_out( 'ok 2 #skip skipping' )
774 test.'skip'( 2, 'skipping' )
775 test_test( 'skip test should pass' )
777 test_out( 'ok 3 #skip skipped' )
779 test_test( 'skip(int)' )
781 test_out( 'ok 4 #skip jumping' )
783 test_test( 'skip(string)' )
785 test_out( 'ok 5 #skip lunch' )
786 test_out( 'ok 6 #skip lunch' )
788 test_test( 'skip(int, string)' )
790 test_out( 'ok 7 #skip skipped' )
792 test_test( 'skip()' )
798 test = new [ 'Test'; 'Builder' ]
800 test_out( 'ok 8 # TODO todo reason' )
801 test.'todo'( 1, 'passing test', 'todo reason' )
802 test_test( 'todo test should pass, marked as TODO' )
804 test_out( "not ok 9 # TODO todo reason\n\tFailed (TODO) test 'failing test'" )
805 test.'todo'( 0, 'failing test', 'todo reason' )
806 test_test( 'todo test should fail, marked as TODO' )
808 test_out( 'ok 10 # TODO todo reason' )
809 todo( 1, 'passing test', 'todo reason' )
810 test_test( 'todo test should pass, marked as TODO' )
812 test_out( "not ok 11 # TODO todo reason\n\tFailed (TODO) test 'failing test'" )
813 todo( 0, 'failing test', 'todo reason' )
814 test_test( 'todo test should fail, marked as TODO' )
816 test_out( "not ok 12 # TODO \n\tFailed (TODO) test ''" )
818 test_test( 'todo test with no description or reason' )
823 .local pmc dog, terrier, daschund, Spot, Sossy
826 terrier = subclass dog, "terrier"
827 daschund = subclass dog, "daschund"
830 Sossy = new "daschund"
832 test_pass( 'Spot isa terrier' )
833 isa_ok(Spot, "terrier", "Spot")
834 test_test( 'passing isa_ok for PMC/string (class =)' )
836 test_pass( 'Spot isa dog' )
837 isa_ok(Spot, "dog", "Spot")
838 test_test( 'passing isa_ok for PMC/string (super)')
840 test_pass( 'Sossy isa daschund' )
843 isa_ok(Sossy, $P1, "Sossy")
844 test_test( 'passing isa_ok for PMC/PMC (class =)' )
846 test_pass( 'Sossy isa dog' )
849 isa_ok(Sossy, $P2, "Sossy")
850 test_test( 'passing isa_ok for PMC/PMC (super)')
852 test_pass( 'The object isa terrier' )
853 isa_ok(Spot, "terrier")
854 test_test( 'passing isa_ok with no object description (class =)' )
856 test_fail( 'Spot isa daschund' )
857 test_diag( "Spot isn't a daschund it's a terrier" )
858 isa_ok(Spot, 'daschund', "Spot")
859 test_test( 'failing test isnt() for PMC/string')
864 test_pass( 'null is_null')
866 is_null($P0, 'null is_null')
867 test_test( 'passing is_null with null')
869 test_fail( 'String is not null' )
871 is_null($P0, 'String is not null')
872 test_diag( "Have: Concerto" )
873 test_diag( "Want: null" )
874 test_test( 'failing test is_null for String pmc')
881 # vim: expandtab shiftwidth=4 ft=pir: