Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 68 lines (52 sloc) 2.173 kb
86188d0 [gsoc_spectest] new test for closure parameters. (added 9 tests)
Auzon authored
1 use v6;
2 use Test;
3
5ab90cf [t/spec] test multi dispatch over the signature of closure parameters
moritz authored
4 plan 14;
86188d0 [gsoc_spectest] new test for closure parameters. (added 9 tests)
Auzon authored
5
6 # L<S06/Closure parameters>
7
8 {
9 my sub testit (&testcode) {testcode()}
10
11 ok(testit({return 1}), 'code executes as testsub({...})');
12
13 my $code = {return 1};
14 ok(testit($code), 'code executes as testsub($closure)');
15
16 my sub returntrue {return 1}
17 ok(testit(&returntrue), 'code executes as testsub(&subroutine)');
18 }
19
20 # with a signature for the closure
b159547 [t] and [t/spec]
moritz authored
21 #?rakudo skip 'type syntax parse failure'
86188d0 [gsoc_spectest] new test for closure parameters. (added 9 tests)
Auzon authored
22 {
23 my sub testit (&testcode:(Int)) {testcode(12)}
24 my sub testint(Int $foo) {return 1}
25 my sub teststr(Str $foo) {return 'foo'}
26
27 ok(testit(&testint), 'code runs with proper signature (1)');
28 eval_dies_ok('testit(&teststr)', 'code dies with invalid signature (1)');
29 }
30
b159547 [t] and [t/spec]
moritz authored
31 #?rakudo skip 'type syntax parse failure'
86188d0 [gsoc_spectest] new test for closure parameters. (added 9 tests)
Auzon authored
32 {
c21309d [t/spec] when our closure parameter requires and Int, we should give …
moritz authored
33 my sub testit (&testcode:(Int --> Bool)) {testcode(3)}
86188d0 [gsoc_spectest] new test for closure parameters. (added 9 tests)
Auzon authored
34 my Bool sub testintbool(Int $foo) {return Bool::True}
35 my Bool sub teststrbool(Str $foo) {return Bool::False}
36 my Int sub testintint (Int $foo) {return 1}
37 my Int sub teststrint (Str $foo) {return 0}
38
39 ok(testit(&testintbool), 'code runs with proper signature (2)');
40 eval_dies_ok('testit(&testintint)', 'code dies with invalid signature (2)');
41 eval_dies_ok('testit(&teststrbool)', 'code dies with invalid signature (3)');
42 eval_dies_ok('testit(&teststrint)', 'code dies with invalid signature (4)');
43 }
c21309d [t/spec] when our closure parameter requires and Int, we should give …
moritz authored
44
5ab90cf [t/spec] test multi dispatch over the signature of closure parameters
moritz authored
45 #?rakudo skip 'type syntax parse failure'
46 {
47 multi sub t1 (&code:(Int)) { 'Int' };
48 multi sub t1 (&code:(Str)) { 'Str' };
49 multi sub t1 (&code:(Str --> Bool)) { 'Str --> Bool' };
50 multi sub t1 (&code:(Any, Any)) { 'Two' };
51
52 is t1(-> $a, $b { }), 'Two',
e2cac8b [closure-parameters.t] missing semicolons
lwall authored
53 'Multi dispatch based on closure parameter syntax (1)';
5ab90cf [t/spec] test multi dispatch over the signature of closure parameters
moritz authored
54 is t1(-> Int $a { }), 'Int',
e2cac8b [closure-parameters.t] missing semicolons
lwall authored
55 'Multi dispatch based on closure parameter syntax (2)';
5ab90cf [t/spec] test multi dispatch over the signature of closure parameters
moritz authored
56 is t1(-> Str $a { }), 'Str',
e2cac8b [closure-parameters.t] missing semicolons
lwall authored
57 'Multi dispatch based on closure parameter syntax (3)';
5ab90cf [t/spec] test multi dispatch over the signature of closure parameters
moritz authored
58
59 sub takes-str-returns-bool(Str $x --> Bool) { True }
60 is t1(&takes-str-returns-bool), 'Str --> Bool',
e2cac8b [closure-parameters.t] missing semicolons
lwall authored
61 'Multi dispatch based on closure parameter syntax (4)';
5ab90cf [t/spec] test multi dispatch over the signature of closure parameters
moritz authored
62
63 dies_ok { t1( -> { 3 }) },
e2cac8b [closure-parameters.t] missing semicolons
lwall authored
64 'Multi dispatch based on closure parameter syntax (5)';
5ab90cf [t/spec] test multi dispatch over the signature of closure parameters
moritz authored
65 }
66
c21309d [t/spec] when our closure parameter requires and Int, we should give …
moritz authored
67 # vim: ft=perl6
Something went wrong with that request. Please try again.