Changeset 22514
- Timestamp:
- 10/05/08 19:32:08 (7 weeks ago)
- Location:
- t
- Files:
-
- 1 removed
- 3 modified
- 4 moved
-
blocks/closure.t (modified) (1 diff)
-
closure_traits/in_loop.t (modified) (1 diff)
-
spec/S04-closure-traits/keep-undo.t (moved) (moved from t/closure_traits/keep_undo.t)
-
spec/S04-closure-traits/multiple.t (moved) (moved from t/closure_traits/multiple.t)
-
spec/S04-closure-traits/next. (moved) (moved from t/closure_traits/next.t) (13 diffs)
-
spec/S04-closure-traits/pre-post.t (modified) (5 diffs)
-
spec/S06-signature/sub-ref.t (moved) (moved from t/blocks/sub_ref.t) (6 diffs)
-
unspecced/p5 (deleted)
Legend:
- Unmodified
- Added
- Removed
-
t/blocks/closure.t
r22399 r22514 5 5 6 6 diag "Testing for calling block bindings..."; 7 okeval(q[7 is eval(q[ 8 8 my &foo := { "foo" }; 9 9 foo; 10 ]), "Calling block binding without argument. (Runtime)";10 ]), 'foo', "Calling block binding without argument. (Runtime)"; 11 11 12 okeval(q[12 is eval(q[ 13 13 my &foo ::= { "foo" }; 14 14 foo; 15 ]), "Calling block binding without argument. (Compile-time)";15 ]), 'foo', "Calling block binding without argument. (Compile-time)"; 16 16 17 okeval(q[17 is eval(q[ 18 18 my &foo := { $^a }; 19 19 foo(1); 20 ]), "Calling block binding with argument. (Runtime, with parens)";20 ]), 1, "Calling block binding with argument. (Runtime, with parens)"; 21 21 22 okeval(q[22 is eval(q[ 23 23 my &foo := { $^a }; 24 24 foo 1; 25 ]), "Calling block binding with argument. (Runtime, no parens)";25 ]), 1, "Calling block binding with argument. (Runtime, no parens)"; 26 26 27 okeval(q[27 is eval(q[ 28 28 my &foo ::= { $^a }; 29 29 foo(1); 30 ]), "Calling block binding with argument. (Compile-time, with parens)";30 ]), 1, "Calling block binding with argument. (Compile-time, with parens)"; 31 31 32 okeval(q[32 is eval(q[ 33 33 my &foo ::= { $^a }; 34 34 foo 1; 35 ]), "Calling block binding with argument. (Compile-time, no parens)";35 ]), 1, "Calling block binding with argument. (Compile-time, no parens)"; 36 36 37 37 -
t/closure_traits/in_loop.t
r20490 r22514 8 8 # 9 9 # * KEEP, UNDO, PRE, POST, CONTROL 10 # CATCH is tested in t/ base/try.t10 # CATCH is tested in t/spec/S04-statements/try.t 11 11 # 12 12 # * $var will undo, etc -
t/spec/S04-closure-traits/next.
r20490 r22514 8 8 # "end of the loop block" or "explicit next"> 9 9 { 10 my $str ;10 my $str = ''; 11 11 for 1..5 { 12 12 NEXT { $str ~= ':' } … … 19 19 # NEXT is positioned at the bottom: 20 20 { 21 my $str ;21 my $str = ''; 22 22 for 1..5 { 23 23 next if $_ % 2 == 1; … … 30 30 # NEXT is positioned in the middle: 31 31 { 32 my $str ;32 my $str = ''; 33 33 for 1..5 { 34 34 next if $_ % 2 == 1; … … 41 41 # NEXT is evaluated even at the last iteration 42 42 { 43 my $str ;43 my $str = ''; 44 44 for 1..2 { 45 45 NEXT { $str ~= 'n'; } … … 53 53 54 54 { 55 my $str ;55 my $str = ''; 56 56 try { 57 57 for 1..5 { … … 64 64 65 65 { 66 my $str ;66 my $str = ''; 67 67 try { 68 68 for 1..5 { … … 75 75 76 76 { 77 my $str ;77 my $str = ''; 78 78 my sub foo { 79 79 for 1..5 { … … 88 88 # L<S04/Closure traits/last bypasses "NEXT blocks"> 89 89 { 90 my $str ;90 my $str = ''; 91 91 for 1..5 { 92 92 NEXT { $str ~= $_; } … … 99 99 100 100 { 101 my $str ;101 my $str = ''; 102 102 for 1..2 { 103 103 NEXT { $str ~= 'n' } … … 109 109 # reversed order 110 110 { 111 my $str ;111 my $str = ''; 112 112 for 1..2 { 113 113 LEAVE { $str ~= 'l' } … … 122 122 123 123 { 124 my $str ;124 my $str = ''; 125 125 my $n = 0; 126 126 my $i; … … 134 134 135 135 { 136 my $str ;136 my $str = ''; 137 137 loop (my $n = 0; $n < 5; ++$n) { 138 138 NEXT { $str ~= $n } … … 143 143 { 144 144 my @x = 0..4; 145 my $str ;145 my $str = ''; 146 146 for @x { 147 147 NEXT { $str ~= $_; } -
t/spec/S04-closure-traits/pre-post.t
r22195 r22514 9 9 # TODO: 10 10 # * Multiple inheritance + PRE/POST blocks 11 # * check that the POST block receives the return value as topic ($_)12 11 13 plan 1 6;12 plan 18; 14 13 15 my $foo = '16 14 sub foo(Num $i) { 17 15 PRE { … … 20 18 return 1; 21 19 } 22 ';23 20 24 sub bar( int $i) {21 sub bar(Int $i) { 25 22 return 1; 26 23 POST { … … 29 26 } 30 27 31 ok eval($foo ~ 'foo(2);'), 'sub with PREcompiles and runs';32 ok eval(bar(3)), 'sub with POST compiles';28 lives_ok { foo(2) }, 'sub with PRE compiles and runs'; 29 lives_ok { bar(3) }, 'sub with POST compiles and runs'; 33 30 34 try { 35 eval($foo ~ 'foo(10)'); 36 } 37 38 ok defined($!), 'Violated PRE fails OK'; 39 40 try { 41 bar(10); 42 } 43 ok defined($!), 'violated POST fails OK'; 31 dies_ok { foo(10) }, 'Violated PRE throws (catchable) exception'; 32 dies_ok { bar(10) }, 'Violated POST throws (catchable) exception'; 44 33 45 34 # multiple PREs und POSTs 46 35 47 my $baz = '48 36 sub baz (Num $i) { 49 37 PRE { … … 55 43 return 1; 56 44 } 57 '; 58 ok($baz ~ 'baz(2)', 'sub with two PREs compiles and runs'); 45 lives_ok { baz(2) }, 'sub with two PREs compiles and runs'; 59 46 60 eval( $baz ~ 'baz(-1)');61 ok(defined($!), 'sub with two PREs fails when firstis violated');47 dies_ok { baz(-1)}, 'sub with two PREs fails when first is violated'; 48 dies_ok { baz(42)}, 'sub with two PREs fails when second is violated'); 62 49 63 eval( $baz ~ 'baz(42)');64 ok(defined($!), 'sub with two PREs fails when second is violated');65 50 66 51 sub qox (Num $i) { … … 74 59 } 75 60 76 ok(qox(23), "sub with two POSTs compiles and runs"); 77 78 try { 79 qox(-1); 80 } 81 82 ok(defined($!), "sub with two POSTs fails if first POST is violated"); 83 84 try { 85 qox(123); 86 } 87 88 ok(defined($!), "sub with two POSTs fails if second POST is violated"); 61 lives_ok({ qox(23) }, "sub with two POSTs compiles and runs"); 62 dies_ok( { qox(-1) }, "sub with two POSTs fails if first POST is violated"); 63 dies_ok( { qox(123)}, "sub with two POSTs fails if second POST is violated"); 89 64 90 65 # inheritance 91 66 92 my $ih_pre = 93 ' class Foo { 67 class PRE_Parent { 94 68 method test(Num $i) { 95 69 PRE { 96 $i >2370 $i < 23 97 71 } 98 99 72 return 1; 100 73 } 101 74 } 102 75 103 class Bar is Foo{76 class PRE_Child is PRE_Parent { 104 77 method test(Num $i){ 105 78 PRE { 106 $i < -2379 $i > 0; 107 80 } 108 81 return 1; 109 82 } 110 83 } 111 my $foo = Bar.new; ';112 84 113 ok(eval($ih_pre ~ '$foo.test(-42)'), "PRE in methods compiles and runs"); 114 ok(eval($ih_pre ~ '$foo.test(42)'), "inherited PRE in compiles and runs"); 85 my $foo = PRE_Child.new; 115 86 116 try { 117 eval($ih_pre ~ '$foo.test(0)'); 118 } 119 120 ok(defined($!), "violated PRE in methods fails OK"); 87 lives_ok { $foo.test(5) }, 'PRE in methods compiles and runs'; 88 dies_ok { $foo.test(-42) }, 'PRE in child throws'; 89 dies_ok { $foo.test(78) }, 'PRE in parent throws'; 121 90 122 91 123 class Foo{92 class POST_Parent { 124 93 method test(Num $i) { 125 94 return 1; 126 95 POST { 127 $i <2396 $i > 23 128 97 } 129 98 } 130 99 } 131 100 132 class Bar is Foo{101 class POST_Child is POST_Parent { 133 102 method test(Num $i){ 134 103 return 1; 135 104 POST { 136 $i >-23105 $i < -23 137 106 } 138 107 } 139 108 } 140 my $ foo_post = Bar.new;109 my $mp = POST_Child.new; 141 110 142 ok(eval('$foo_post.test(0)'), "Inherited POST compiles and runs"); 111 lives_ok { $mp.test(-42) }, "It's enough if we satisfy one of the POST blocks (Child)"; 112 lives_ok { $mp.test(42) }, "It's enough if we satisfy one of the POST blocks (Parent)"; 113 dies_ok { $tmp.test(12) }, 'Violating poth POST blocks throws an error'; 143 114 144 try { 145 $foo_post.test(42); 115 class Another { 116 method test(Num $x) { 117 return 3 * $x; 118 POST { 119 $_ > 4 120 } 121 } 146 122 } 147 ok(defined($!), "Inherited POST fails ok");148 123 149 try { 150 $foo_post.test(-42); 151 } 152 ok(defined($!), "Own POST fails ok"); 124 my $pt = Another.new; 125 lives_ok { $pt.test(2) }, 'POST receives return value as $_ (succeess)'; 126 dies_ok { $pt.test(1) }, 'POST receives return value as $_ (failure)'; -
t/spec/S06-signature/sub-ref.t
r21997 r22514 27 27 { 28 28 my $foo = sub () { 42 }; 29 isa_ok($foo, 'Code'); 30 isa_ok($foo, 'Routine'); 31 isa_ok($foo, 'Sub'); 29 isa_ok($foo, Code); 30 #?rakudo 2 todo 'types Sub, Routine' 31 isa_ok($foo, Routine); 32 isa_ok($foo, Sub); 32 33 is $foo.(), 42, "basic invocation of an anonymous sub"; 33 try { $foo.(23) };34 ok($!, "invocation of an parameterless anonymous sub with a parameter dies");34 #?rakudo todo 'signature error checking' 35 dies_ok { $foo.(23) }, "invocation of an parameterless anonymous sub with a parameter dies"; 35 36 } 36 37 38 #?rakudo skip 'pointy blocks' 37 39 { 38 40 my $foo = -> { 42 }; 39 isa_ok($foo, 'Code');40 isa_ok($foo, 'Block');41 isa_ok($foo, Code); 42 isa_ok($foo, Block); 41 43 is $foo.(), 42, "basic invocation of a pointy block"; 42 try { $foo.(23) }; 43 ok($!, "invocation of an parameterless pointy block with a parameter dies"); 44 dies_ok { $foo.(23) }, "invocation of an parameterless pointy block with a parameter dies"; 44 45 } 45 46 … … 49 50 isa_ok($foo, 'Block'); 50 51 is $foo.(42), 142, "basic invocation of a pointy block with a param"; 51 try { $foo.() }; 52 ok($!, "invocation of an parameterized block expecting a param without a param dies"); 52 dies_ok { $foo.() }, "invocation of an parameterized block expecting a param without a param dies"; 53 53 } 54 54 55 55 { 56 56 my $foo = sub { 100 + (@_[0] // -1) }; 57 isa_ok($foo, 'Code'); 58 isa_ok($foo, 'Routine'); 59 isa_ok($foo, 'Sub'); 57 isa_ok($foo, Code); 58 #?rakudo 2 todo 'types Sub, Routine' 59 isa_ok($foo, Routine); 60 isa_ok($foo, Sub); 60 61 is $foo.(42), 142, "basic invocation of a perl5-like anonymous sub (1)"; 61 62 is $foo.(), 99, "basic invocation of a perl5-like anonymous sub (2)"; … … 64 65 { 65 66 my $foo = sub ($x) { 100 + $x }; 66 isa_ok($foo, 'Code'); 67 isa_ok($foo, 'Routine'); 68 isa_ok($foo, 'Sub'); 67 isa_ok($foo, Code); 68 #?rakudo 2 todo 'types Sub, Routine' 69 isa_ok($foo, Routine); 70 isa_ok($foo, Sub); 69 71 is $foo.(42), 142, "calling an anonymous sub with a positional param"; 72 #?rakudo skip 'calling positiona parameters by name' 70 73 is $foo.(x => 42), 142, "calling an anonymous sub with a positional param addressed by name"; 71 74 dies_ok { $foo.() }, … … 77 80 # Confirmed by p6l, see thread "Anonymous macros?" by Ingo Blechschmidt 78 81 # L<"http://www.nntp.perl.org/group/perl.perl6.language/21825"> 82 #?rakudo skip 'macros, compile time binding' 79 83 { 80 84 # We do all this in a eval() not because the code doesn't parse, … … 86 90 # think.) 87 91 our &foo_macro ::= macro ($x) { "1000 + $x" }; 88 isa_ok(&foo_macro, "Code"); 89 isa_ok(&foo_macro, "Routine"); 90 isa_ok(&foo_macro, "Macro", :todo<feature>); 92 isa_ok(&foo_macro, Code); 93 isa_ok(&foo_macro, Routine); 94 #?pugs todo 'macros' 95 isa_ok(&foo_macro, Macro); 91 96 92 97 is foo_macro(3), 1003, "anonymous macro worked"; 93 98 } 94 99 100 #?rakudo skip 'scoping/closures' 95 101 { 96 102 my $mkinc = sub { my $x = 0; return sub { $x++ }; }; … … 99 105 my $inc2 = $mkinc(); 100 106 101 is($inc1(), 0, " inc1 == 0");102 is($inc1(), 1, " inc1 == 1");103 is($inc2(), 0, " inc2 == 0");104 is($inc2(), 1, " inc2 == 1");107 is($inc1(), 0, "clousures: inc1 == 0"); 108 is($inc1(), 1, "clousures: inc1 == 1"); 109 is($inc2(), 0, "clousures: inc2 == 0"); 110 is($inc2(), 1, "clousures: inc2 == 1"); 105 111 }
