Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 220 lines (177 sloc) 8.344 kB
aa2e9ad [spec] simple tests for named parameters
moritz authored
1 use v6;
2 use Test;
3
b2558b0 [t/spec] Tests for renaming parameters in the signature.
jnthn authored
4 plan 74;
aa2e9ad [spec] simple tests for named parameters
moritz authored
5
6 # L<S06/Required parameters/"Passing a named argument that cannot be bound to
7 # a normal subroutine is also a fatal error.">
8
9 {
10 sub a($x = 4) {
11 return $x;
12 }
13 is a(3), 3, 'Can pass positional arguments';
14 eval_dies_ok('a(g=>7)', 'Dies on passing superflous arguments');
15 }
a5cd2dd [spec] more tests for named parameters, and fudged them for rakudo
moritz authored
16
17 {
18 # see http://rt.perl.org/rt3/Ticket/Display.html?id=54808
19 sub b($x) {
20 return $x;
21 }
22
23 #?rakudo skip 'Passing positional parameters as named ones'
24 is b(:x(3)), 3, 'Can pass positional parameters as named ones';
25
26 sub c(:$w=4){
27 return $w;
28 }
29 is c(w => 3), 3, 'Named argument passes an integer, not a Pair';
41281a2 [t/spec] Fix and unfudge a colonpair test.
jnthn authored
30 my $w = 5;
31 is c(:$w), 5, 'can use :$x colonpair syntax to call named arg';
ebb8b24 [t/spec] tests for mysub(:$named_arg)
moritz authored
32 eval_dies_ok 'my $y; c(:$y)', 'colonpair with wrong variable name dies';
a5cd2dd [spec] more tests for named parameters, and fudged them for rakudo
moritz authored
33 }
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
34
35 # L<S06/Named parameters>
36
37 sub simple_pos_param($x) { $x }
38 #?rakudo 2 skip 'x => 3 does not assign to $x in sub signature'
39 is simple_pos_param(x => 3), 3, "positional param may be addressed by name (1)";
40 is simple_pos_param(:x(3)), 3, "positional param may be addressed by name (2)";
41
42 # L<S06/Named parameters/marked by a prefix>
43 sub simple_pos_params (:$x) { $x }
44
45 is(simple_pos_params( x => 4 ), 4, "simple named param");
46
47
48 sub foo (:$x = 3) { $x }
49
50 is(foo(), 3, "not specifying named params that aren't mandatory works");
51 #?rakudo todo 'using named as positional should fail'
52 #?pugs todo 'bug'
53 dies_ok({foo(4)}, "using a named as a positional fails");
54
55 is(foo( x => 5), 5, "naming named param also works");
56 is(foo( :x<5> ), 5, "naming named param adverb-style also works");
57
58 sub foo2 (:$x = 3, :$y = 5) { $x + $y }
59
60 is(foo2(), 8, "not specifying named params that aren't mandatory works (foo2)");
61 #?pugs 2 todo 'bug'
62 #?rakudo 2 todo 'using named as positional should fail'
63 dies_ok({foo2(4)}, "using a named as a positional fails (foo2)");
64 dies_ok({foo2(4, 10)}, "using a named as a positional fails (foo2)");
65 is(foo2( x => 5), 10, "naming named param x also works (foo2)");
66 is(foo2( y => 3), 6, "naming named param y also works (foo2)");
67 is(foo2( x => 10, y => 10), 20, "naming named param x & y also works (foo2)");
68 is(foo2( :x(5) ), 10, "naming named param x adverb-style also works (foo2)");
69 is(foo2( :y(3) ), 6, "naming named param y adverb-style also works (foo2)");
70 is(foo2( :x(10), :y(10) ), 20, "naming named params x & y adverb-style also works (foo2)");
71 is(foo2( x => 10, :y(10) ), 20, "mixing fat-comma and adverb naming styles also works for named params (foo2)");
72 is(foo2( :x(10), y => 10 ), 20, "mixing adverb and fat-comma naming styles also works for named params (foo2)");
73
74 sub assign_based_on_named_positional ($x, :$y = $x) { $y }
75
76
77 is(assign_based_on_named_positional(5), 5, "When we don't explicitly specify, we get the original value");
78 is(assign_based_on_named_positional(5, y => 2), 2, "When we explicitly specify, we get our value");
79 is(assign_based_on_named_positional('y'=>2), ('y'=>2), "When we explicitly specify, we get our value");
80 my $var = "y";
81 is(assign_based_on_named_positional($var => 2), ("y"=>2),
82 "When we explicitly specify, we get our value");
83
84 # L<S06/Named arguments/multiple same-named arguments>
85 #?rakudo skip 'parsefail'
86 {
87 sub named_array(:@x) { +«@x }
88
89 is(eval('named_array(:x)'), (1), 'named array taking one named arg');
90 is(eval('named_array(:x, :!x)'), (1, 0), 'named array taking two named args');
91 is(eval('named_array(:x(1), :x(2), :x(3))'), (1, 2, 3), 'named array taking three named args');
92 }
93
94 # L<S06/Named arguments/Pairs intended as positional arguments>
95 #?rakudo skip 'parsefail'
96 {
97 sub named_array2(@x, :@y) { (+«@x, 42, +«@y) }
98 # +«(:x) is (0, 1)
99
100 is(eval('named_array2(:!x, :y)'), (0, 42, 1), 'named and unnamed args - two named');
101 is(eval('named_array2(:!x, y => 1)'), (0, 42, 1), 'named and unnamed args - two named - fatarrow');
102 is(eval('named_array2(:y, :!x)'), (0, 42, 1), 'named and unnamed args - two named - backwards');
103 is(eval('named_array2(:y, (:x))'), (0, 1, 42, 1), 'named and unnamed args - one named, one pair');
104 is(eval('named_array2(1, 2)'), (1, 42), 'named and unnamed args - two unnamed');
105 is(eval('named_array2(:!y, 1)'), (1, 42, 0), 'named and unnamed args - one named, one pos');
106 is(eval('named_array2(1, :!y)'), (1, 42, 0), 'named and unnamed args - one named, one pos - backwards');
107 is(eval('named_array2(:y, 1, :!y)'), (1, 42, 1, 0), 'named and unnamed args - two named, one pos');
de076fd [t/spec] removed some obsolete usages of 'is $stuff, undef'
moritz authored
108 ok(eval('named_array2(:y, :y)') ~~ undef, 'named and unnamed args - two named with same name');
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
109 is(eval('named_array2(:y, (:x))'), (0, 1, 42, 1), 'named and unnamed args - passing parenthesized pair');
110 is(eval('named_array2(:y, (:y))'), (0, 1, 42, 1), 'named and unnamed args - passing parenthesized pair of same name');
111 is(eval('named_array2(:y, :z)'), (0, 1, 42, 1), 'named and unnamed args - passing pair of unrelated name');
112 is(eval('named_array2(:y, "x" => 1)'), (0, 1, 42, 1), 'named and unnamed args - passing pair with quoted fatarrow');
113 }
114
115 # L<S06/Named parameters/They are marked by a prefix>
116 # L<S06/Required parameters/declared with a trailing>
117 sub mandatory (:$param!) {
118 return $param;
119 }
120
121 is(mandatory(param => 5) , 5, "named mandatory parameter is returned");
122 eval_dies_ok('mandatory()', "not specifying a mandatory parameter fails");
123
124 #?rakudo skip 'Cannot apply trait required to parameters yet'
125 {
126 sub mandatory_by_trait (:$param is required) {
127 return $param;
128 }
129
130 is(mandatory_by_trait(param => 5) , 5, "named mandatory parameter is returned");
131 dies_ok( { mandatory_by_trait() }, "not specifying a mandatory parameter fails");
132 }
133
134
135 # L<S06/Named parameters/sub formalize>
136 sub formalize($text, :$case, :$justify) returns List {
137 return($text,$case,$justify);
138 }
139
140 #?rakudo skip 'parsefail'
141 {
142 my ($text,$case,$justify) = formalize('title', case=>'upper');
143 is($text,'title', "text param was positional");
de076fd [t/spec] removed some obsolete usages of 'is $stuff, undef'
moritz authored
144 ok($justify ~~ undef, "justification param was not given");
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
145 is($case, 'upper', "case param was named, and in justification param's position");
146 }
147
148 #?rakudo skip 'parsefail'
149 {
150 my ($text,$case,$justify) = formalize('title', justify=>'left');
151 is($text,'title', "text param was positional");
152 is($justify, 'left', "justify param was named");
de076fd [t/spec] removed some obsolete usages of 'is $stuff, undef'
moritz authored
153 ok($case ~~ undef, "case was not given at all");
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
154 }
155
156 #?rakudo skip 'parsefail'
157 {
158 my ($text,$case,$justify) = formalize("title", :justify<right>, :case<title>);
159
160 is($text,'title', "title param was positional");
161 is($justify, 'right', "justify param was named with funny syntax");
162 is($case, 'title', "case param was named with funny syntax");
163 }
164
165 {
166 sub h($a,$b,$d) { $d ?? h($b,$a,$d-1) !! $a~$b }
167
168 is(h('a','b',1),'ba',"parameters don\'t bind incorrectly");
169 }
170
171 # Slurpy Hash Params
172 {
173 sub slurpee(*%args) { return %args }
174 my %fellowship = slurpee(hobbit => 'Frodo', wizard => 'Gandalf');
175 is(%fellowship<hobbit>, 'Frodo', "hobbit arg was slurped");
176 is(%fellowship<wizard>, 'Gandalf', "wizard arg was slurped");
177 is(+%fellowship, 2, "exactly 2 arguments were slurped");
de076fd [t/spec] removed some obsolete usages of 'is $stuff, undef'
moritz authored
178 ok(%fellowship<dwarf> ~~ undef, "dwarf arg was not given");
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
179 }
180
181 #?rakudo skip 'parsefail on lvalue'
182 {
183 sub named_and_slurp(:$grass, *%rest) { return($grass, %rest) }
184 my ($grass, %rest) = named_and_slurp(sky => 'blue', grass => 'green', fire => 'red');
185 is($grass, 'green', "explicit named arg received despite slurpy hash");
186 is(+%rest, 2, "exactly 2 arguments were slurped");
187 is(%rest<sky>, 'blue', "sky argument was slurped");
188 is(%rest<fire>, 'red', "fire argument was slurped");
de076fd [t/spec] removed some obsolete usages of 'is $stuff, undef'
moritz authored
189 ok(%rest<grass> ~~ undef, "grass argument was NOT slurped");
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
190 }
191
192 #?rakudo skip 'positional value passed by name did not work'
193 {
194 my $ref;
195 sub setref($refin) {
196 $ref = $refin;
197 }
198 my $aref = [0];
199 setref(refin => $aref);
200 $aref[0]++;
201 is($aref[0], 1, "aref actually implemented");
202 is($ref[0], 1, "ref is the same as aref");
203 }
204
baf377f [t/spec] Basic tests for typed named parameters including optional ch…
jnthn authored
205 {
206 sub typed_named(Int :$x) { 1 }
207 is(typed_named(:x(42)), 1, 'typed named parameters work...');
208 is(typed_named(), 1, '...when value not supplied also...');
209 dies_ok({ typed_named("BBQ") }, 'and the type check is enforced');
210 }
211
b2558b0 [t/spec] Tests for renaming parameters in the signature.
jnthn authored
212 {
213 sub renames(:y($x)) { $x }
214 is(renames(:y(42)), 42, 'renaming of parameters works');
215 is(renames(y => 42), 42, 'renaming of parameters works');
216 }
217
218
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
219 # vim: ft=perl6
Something went wrong with that request. Please try again.