|
| 1 | +use v6; |
| 2 | +use Test; |
| 3 | +plan 24; |
| 4 | + |
| 5 | +# Positionals |
| 6 | +{ |
| 7 | + sub greet($name) { "hello $name" } |
| 8 | + |
| 9 | + is greet("joe"), "hello joe"; |
| 10 | + dies_ok {EVAL 'greet()'}; |
| 11 | +} |
| 12 | + |
| 13 | +our sub guess($who, $what?) { |
| 14 | + # manually check to see if $what is defined |
| 15 | + $what.defined |
| 16 | +} |
| 17 | + |
| 18 | +dies_ok {EVAL 'guess()'}; |
| 19 | +is_deeply guess("World"), False, 'optional'; |
| 20 | +is_deeply guess("World",37), True, 'optional'; |
| 21 | + |
| 22 | +{ |
| 23 | + sub dance($who, $dance = "Salsa") { |
| 24 | + $dance; |
| 25 | + } |
| 26 | + is dance("Rachael"), "Salsa", 'default'; |
| 27 | + is dance("Rachael", "Watusi"), "Watusi", 'default'; |
| 28 | +} |
| 29 | + |
| 30 | +{ |
| 31 | + multi sub dance($who, $dance) { |
| 32 | + "$who is doing the $dance"; |
| 33 | + } |
| 34 | + multi sub dance($who) { |
| 35 | + dance($who, "Salsa"); |
| 36 | + } |
| 37 | + |
| 38 | + is dance("Rachael"), "Rachael is doing the Salsa", 'multi'; |
| 39 | + is dance("Rachael", "Watusi"), "Rachael is doing the Watusi", 'multi'; |
| 40 | +} |
| 41 | + |
| 42 | +# Types |
| 43 | +{ |
| 44 | + sub greet(Str $name) {"hello $name"} |
| 45 | + lives_ok {EVAL 'greet("joe")'},'type check'; |
| 46 | + dies_ok {EVAL 'greet(3)'},'type check'; |
| 47 | +} |
| 48 | + |
| 49 | +{ |
| 50 | + multi odd-or-even(Int $i where * %% 2) { "even" }; |
| 51 | + multi odd-or-even(Int $i) { "odd"}; |
| 52 | + is odd-or-even(42), "even", 'where clause'; |
| 53 | + is odd-or-even(37), "odd", 'where clause'; |
| 54 | +} |
| 55 | + |
| 56 | +{ |
| 57 | + multi fib(1) { 1 } |
| 58 | + multi fib(2) { 1 } |
| 59 | + multi fib(Int $i) { fib($i-1) + fib($i-2) } |
| 60 | + |
| 61 | + is fib(10), 55, 'literal arguments' |
| 62 | +} |
| 63 | + |
| 64 | +# Named |
| 65 | +{ |
| 66 | + sub doctor(:$number, :$prop) { |
| 67 | + "Doctor # $number liked to play with his $prop"; |
| 68 | + } |
| 69 | + |
| 70 | + is doctor(:prop("cricket bat"), :number<5>), |
| 71 | + 'Doctor # 5 liked to play with his cricket bat', |
| 72 | + 'named'; |
| 73 | + |
| 74 | + is doctor(:number<4>, :prop<scarf>), |
| 75 | + 'Doctor # 4 liked to play with his scarf', |
| 76 | + 'named'; |
| 77 | + |
| 78 | + my $prop = "fez"; |
| 79 | + my $number = 11; |
| 80 | + is doctor(:$prop, :$number), |
| 81 | + 'Doctor # 11 liked to play with his fez', |
| 82 | + 'named'; |
| 83 | +} |
| 84 | + |
| 85 | +{ |
| 86 | + sub doctor(:number($incarnation), :prop($accoutrement)) { |
| 87 | + "Doctor # $incarnation liked to play with his $accoutrement"; |
| 88 | + } |
| 89 | + my $number = 2; |
| 90 | + my $prop = "recorder"; |
| 91 | + is doctor(:$number, :$prop), |
| 92 | + 'Doctor # 2 liked to play with his recorder', |
| 93 | + 'named (mapped)'; |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +# Slurpy |
| 98 | +{ |
| 99 | + sub Sprintf(Cool $format, *@args) { |
| 100 | + return $format => @args |
| 101 | + } |
| 102 | + is_deeply Sprintf("%d plus %d is %d", 37, 5, 42), ("%d plus %d is %d" => [37, 5, 42]), 'sprintf example'; |
| 103 | +} |
| 104 | + |
| 105 | +{ |
| 106 | + my &callwith := -> *@pos, *%named { |
| 107 | + @pos => %named |
| 108 | + }; |
| 109 | + is_deeply callwith(10, 20, :a(30), :b(40)), |
| 110 | + [10,20] => {a => 30, b => 40}, |
| 111 | + 'pointy block syntax'; |
| 112 | +} |
| 113 | + |
| 114 | +# Methods |
| 115 | +{ |
| 116 | + class Foo { |
| 117 | + method explode($self: $method) { |
| 118 | + [$self, $method]; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + my $obj = Foo.new; |
| 123 | + my $r = $obj.explode(42); |
| 124 | + is_deeply $r, [$obj, 42], 'method invocant'; |
| 125 | +} |
| 126 | + |
| 127 | +# Parameter Traits |
| 128 | +{ |
| 129 | + my $a = 35; |
| 130 | + sub tst-ro($p is readonly) {$p = 42;} |
| 131 | + dies_ok {EVAL 'test-ro($a)'}, 'readonly trait'; |
| 132 | +} |
| 133 | +{ |
| 134 | + my $a = 35; |
| 135 | + sub tst-rw($p is rw) {$p = 42;} |
| 136 | + tst-rw($a); |
| 137 | + is $a, 42, 'rw trait'; |
| 138 | +} |
| 139 | +{ |
| 140 | + my $a = 35; |
| 141 | + sub tst-ro($p is copy) {$p = 42;} |
| 142 | + is $a, 35, 'copy trait'; |
| 143 | +} |
0 commit comments