Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 321 lines (259 sloc) 11.17 kB
aa2e9ad [spec] simple tests for named parameters
moritz authored
1 use v6;
2 use Test;
3
8ebc55e [t/spec] more tests for duplicate sub params
kyle authored
4 plan *;
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';
13cc9c4 [t/spec] 71 typos in 32 files.
Util authored
14 eval_dies_ok('a(g=>7)', 'Dies on passing superfluous arguments');
aa2e9ad [spec] simple tests for named parameters
moritz authored
15 }
a5cd2dd [spec] more tests for named parameters, and fudged them for rakudo
moritz authored
16
17 {
18 sub b($x) {
19 return $x;
20 }
21
22 is b(:x(3)), 3, 'Can pass positional parameters as named ones';
23
24 sub c(:$w=4){
25 return $w;
26 }
27 is c(w => 3), 3, 'Named argument passes an integer, not a Pair';
41281a2 [t/spec] Fix and unfudge a colonpair test.
jnthn authored
28 my $w = 5;
29 is c(:$w), 5, 'can use :$x colonpair syntax to call named arg';
ebb8b24 [t/spec] tests for mysub(:$named_arg)
moritz authored
30 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
31 }
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
32
ea467ee test colonpair shortcuts involving twigils
mmcleric authored
33 #?rakudo skip 'colonpair shortcuts with twigils (RT#73908)'
34 {
35 sub add5(:$g) {
36 return $g + 5;
37 }
38 class A {
39 has $!g = 3;
40 method colonpair_private { add5(:$!g) }
41 };
42 class B {
43 has $.g = 7;
44 method colonpair_public { add5(:$.g) }
45 };
46 sub colonpair_positional {
47 add5(:$^g);
48 }
49
50 is A.new.colonpair_private, 8, 'colonpair with a privare variable';
51 is B.new.colonpair_public, 12, 'colonpair with a public variable';
52 is colonpair_positional(:g<10>), 15, 'colonpair with a positional variable';
53 }
54
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
55 # L<S06/Named parameters>
56
57 sub simple_pos_param($x) { $x }
58 is simple_pos_param(x => 3), 3, "positional param may be addressed by name (1)";
59 is simple_pos_param(:x(3)), 3, "positional param may be addressed by name (2)";
60
61 # L<S06/Named parameters/marked by a prefix>
62 sub simple_pos_params (:$x) { $x }
63
64 is(simple_pos_params( x => 4 ), 4, "simple named param");
65
66
67 sub foo (:$x = 3) { $x }
68
69 is(foo(), 3, "not specifying named params that aren't mandatory works");
198d353 documented some tests and added some tests for RT #53814
ron authored
70
71 # part of RT 53814
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
72 #?pugs todo 'bug'
73 dies_ok({foo(4)}, "using a named as a positional fails");
74
75 is(foo( x => 5), 5, "naming named param also works");
76 is(foo( :x<5> ), 5, "naming named param adverb-style also works");
77
78 sub foo2 (:$x = 3, :$y = 5) { $x + $y }
79
80 is(foo2(), 8, "not specifying named params that aren't mandatory works (foo2)");
81 #?pugs 2 todo 'bug'
82 dies_ok({foo2(4)}, "using a named as a positional fails (foo2)");
83 dies_ok({foo2(4, 10)}, "using a named as a positional fails (foo2)");
84 is(foo2( x => 5), 10, "naming named param x also works (foo2)");
85 is(foo2( y => 3), 6, "naming named param y also works (foo2)");
86 is(foo2( x => 10, y => 10), 20, "naming named param x & y also works (foo2)");
87 is(foo2( :x(5) ), 10, "naming named param x adverb-style also works (foo2)");
88 is(foo2( :y(3) ), 6, "naming named param y adverb-style also works (foo2)");
89 is(foo2( :x(10), :y(10) ), 20, "naming named params x & y adverb-style also works (foo2)");
90 is(foo2( x => 10, :y(10) ), 20, "mixing fat-comma and adverb naming styles also works for named params (foo2)");
91 is(foo2( :x(10), y => 10 ), 20, "mixing adverb and fat-comma naming styles also works for named params (foo2)");
92
93 sub assign_based_on_named_positional ($x, :$y = $x) { $y }
94
95
96 is(assign_based_on_named_positional(5), 5, "When we don't explicitly specify, we get the original value");
97 is(assign_based_on_named_positional(5, y => 2), 2, "When we explicitly specify, we get our value");
98 is(assign_based_on_named_positional('y'=>2), ('y'=>2), "When we explicitly specify, we get our value");
99 my $var = "y";
100 is(assign_based_on_named_positional($var => 2), ("y"=>2),
101 "When we explicitly specify, we get our value");
102
103 # L<S06/Named arguments/multiple same-named arguments>
104 #?rakudo skip 'parsefail'
105 {
106 sub named_array(:@x) { +«@x }
107
108 is(eval('named_array(:x)'), (1), 'named array taking one named arg');
109 is(eval('named_array(:x, :!x)'), (1, 0), 'named array taking two named args');
110 is(eval('named_array(:x(1), :x(2), :x(3))'), (1, 2, 3), 'named array taking three named args');
111 }
112
113 # L<S06/Named arguments/Pairs intended as positional arguments>
114 #?rakudo skip 'parsefail'
115 {
116 sub named_array2(@x, :@y) { (+«@x, 42, +«@y) }
117 # +«(:x) is (0, 1)
118
119 is(eval('named_array2(:!x, :y)'), (0, 42, 1), 'named and unnamed args - two named');
120 is(eval('named_array2(:!x, y => 1)'), (0, 42, 1), 'named and unnamed args - two named - fatarrow');
121 is(eval('named_array2(:y, :!x)'), (0, 42, 1), 'named and unnamed args - two named - backwards');
122 is(eval('named_array2(:y, (:x))'), (0, 1, 42, 1), 'named and unnamed args - one named, one pair');
123 is(eval('named_array2(1, 2)'), (1, 42), 'named and unnamed args - two unnamed');
124 is(eval('named_array2(:!y, 1)'), (1, 42, 0), 'named and unnamed args - one named, one pos');
125 is(eval('named_array2(1, :!y)'), (1, 42, 0), 'named and unnamed args - one named, one pos - backwards');
126 is(eval('named_array2(:y, 1, :!y)'), (1, 42, 1, 0), 'named and unnamed args - two named, one pos');
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
127 ok(eval('named_array2(:y, :y)').notdef, 'named and unnamed args - two named with same name');
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
128 is(eval('named_array2(:y, (:x))'), (0, 1, 42, 1), 'named and unnamed args - passing parenthesized pair');
129 is(eval('named_array2(:y, (:y))'), (0, 1, 42, 1), 'named and unnamed args - passing parenthesized pair of same name');
130 is(eval('named_array2(:y, :z)'), (0, 1, 42, 1), 'named and unnamed args - passing pair of unrelated name');
131 is(eval('named_array2(:y, "x" => 1)'), (0, 1, 42, 1), 'named and unnamed args - passing pair with quoted fatarrow');
132 }
133
134 # L<S06/Named parameters/They are marked by a prefix>
135 # L<S06/Required parameters/declared with a trailing>
136 sub mandatory (:$param!) {
137 return $param;
138 }
139
140 is(mandatory(param => 5) , 5, "named mandatory parameter is returned");
141 eval_dies_ok('mandatory()', "not specifying a mandatory parameter fails");
142
143 #?rakudo skip 'Cannot apply trait required to parameters yet'
144 {
145 sub mandatory_by_trait (:$param is required) {
146 return $param;
147 }
148
149 is(mandatory_by_trait(param => 5) , 5, "named mandatory parameter is returned");
150 dies_ok( { mandatory_by_trait() }, "not specifying a mandatory parameter fails");
151 }
152
153
154 # L<S06/Named parameters/sub formalize>
6d9783d [t/spec] Remove bogus returns that's nothing to do with the test anyway.
jnthn authored
155 sub formalize($text, :$case, :$justify) {
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
156 return($text,$case,$justify);
157 }
158
159 {
160 my ($text,$case,$justify) = formalize('title', case=>'upper');
161 is($text,'title', "text param was positional");
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
162 ok($justify.notdef, "justification param was not given");
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
163 is($case, 'upper', "case param was named, and in justification param's position");
164 }
165
166 {
167 my ($text,$case,$justify) = formalize('title', justify=>'left');
168 is($text,'title', "text param was positional");
169 is($justify, 'left', "justify param was named");
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
170 ok($case.notdef, "case was not given at all");
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
171 }
172
173 {
174 my ($text,$case,$justify) = formalize("title", :justify<right>, :case<title>);
175
176 is($text,'title', "title param was positional");
177 is($justify, 'right', "justify param was named with funny syntax");
178 is($case, 'title', "case param was named with funny syntax");
179 }
180
181 {
182 sub h($a,$b,$d) { $d ?? h($b,$a,$d-1) !! $a~$b }
183
184 is(h('a','b',1),'ba',"parameters don\'t bind incorrectly");
185 }
186
187 # Slurpy Hash Params
188 {
189 sub slurpee(*%args) { return %args }
190 my %fellowship = slurpee(hobbit => 'Frodo', wizard => 'Gandalf');
191 is(%fellowship<hobbit>, 'Frodo', "hobbit arg was slurped");
192 is(%fellowship<wizard>, 'Gandalf', "wizard arg was slurped");
193 is(+%fellowship, 2, "exactly 2 arguments were slurped");
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
194 ok(%fellowship<dwarf>.notdef, "dwarf arg was not given");
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
195 }
196
197 {
198 sub named_and_slurp(:$grass, *%rest) { return($grass, %rest) }
199 my ($grass, %rest) = named_and_slurp(sky => 'blue', grass => 'green', fire => 'red');
200 is($grass, 'green', "explicit named arg received despite slurpy hash");
201 is(+%rest, 2, "exactly 2 arguments were slurped");
202 is(%rest<sky>, 'blue', "sky argument was slurped");
203 is(%rest<fire>, 'red', "fire argument was slurped");
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
204 ok(%rest<grass>.notdef, "grass argument was NOT slurped");
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
205 }
206
207 {
208 my $ref;
209 sub setref($refin) {
210 $ref = $refin;
211 }
212 my $aref = [0];
213 setref(refin => $aref);
214 $aref[0]++;
215 is($aref[0], 1, "aref actually implemented");
216 is($ref[0], 1, "ref is the same as aref");
217 }
218
baf377f [t/spec] Basic tests for typed named parameters including optional ch…
jnthn authored
219 {
220 sub typed_named(Int :$x) { 1 }
221 is(typed_named(:x(42)), 1, 'typed named parameters work...');
222 is(typed_named(), 1, '...when value not supplied also...');
223 dies_ok({ typed_named("BBQ") }, 'and the type check is enforced');
224 }
225
b2558b0 [t/spec] Tests for renaming parameters in the signature.
jnthn authored
226 {
227 sub renames(:y($x)) { $x }
228 is(renames(:y(42)), 42, 'renaming of parameters works');
229 is(renames(y => 42), 42, 'renaming of parameters works');
797076d [t/spec] renamed parameters are tested; remove from TASKS
moritz authored
230 dies_ok { renames(:x(23)) }, 'old name is not available';
b2558b0 [t/spec] Tests for renaming parameters in the signature.
jnthn authored
231 }
232
b8d99dd [t/spec] fix more smartlinks
moritz authored
233 # L<S06/Parameters and arguments/"A signature containing a name collision">
8ebc55e [t/spec] more tests for duplicate sub params
kyle authored
234
846962c [t/spec] Test for RT #68086
kyle authored
235 eval_dies_ok 'sub rt68086( $a, $a ) { }', 'two sub params with the same name';
b2558b0 [t/spec] Tests for renaming parameters in the signature.
jnthn authored
236
8ebc55e [t/spec] more tests for duplicate sub params
kyle authored
237 #?rakudo 3 todo 'sub params with the same name'
238 eval_dies_ok 'sub svn28865( $a, :a($b) ) {}',
239 'sub params with the same name via renaming';
240 eval_dies_ok 'sub svn28865( $a, :a(@b) ) {}',
241 'sub params with same name via renaming and different types';
242 eval_dies_ok 'sub svn28865( :$a, :@a ) {}',
243 'sub params with the same name and different types';
244
2ed580f [t/spec] test based on spec r28870
kyle authored
245 {
246 sub svn28870( $a, @a ) { return ( $a, +@a ) }
247
248 my $item = 'bughunt';
249 my @many = ( 22, 'twenty-two', 47 );
250
251 is( svn28870( $item, @many ), ( 'bughunt', 3 ),
252 'call to sub with position params of same name and different type' );
253 }
254
7f14f44 [t/spec] Test for RT #68524
kyle authored
255 # RT #68524
256 {
257 sub rt68524( :$a! ) {}
258 ok( &rt68524.signature.perl ~~ m/\!/,
259 '.signature.perl with required parameter includes requirement' );
260 }
261
2d1d8c6 [t/spec] Test for RT #69516
kyle authored
262 # RT #69516
263 {
264 sub rt69516( :f($foo) ) { "You passed '$foo' as 'f'" }
265 ok( &rt69516.signature.perl ~~ m/ ':f(' \s* '$foo' \s* ')' /,
266 'parameter rename appears in .signature.perl' );
267 }
268
82c4cad [t/spec] binding happens in parameter order
moritz authored
269 # L<S06/Named parameters/Bindings happen in declaration order>
270 {
271 my $t = '';
272 sub order_test($a where { $t ~= 'a' },
273 $b where { $t ~= 'b' },
274 $c where { $t ~= 'c' }) { 8 };
275 is order_test(c => 5, a => 3, b => 2), 8,
276 'can fill positional by name';
277 ok $t ~~ /a.*b/, '$a was bound before $b';
278 ok $t ~~ /a.*c/, '$a was bound before $c';
279 ok $t ~~ /b.*c/, '$b was bound before $c';
280 }
281
94cbe70 [t/spec] tests for RT #67558
moritz authored
282 # RT #67558
283 {
284 eval_dies_ok q[sub a(:$x, :foo($x) = $x) { $x }],
285 'Cannot rename a parameter to an already existing positional';
286 sub a(:$x, :foo($y) = $x) { $y };
287 is a(x => 2), 2, 'Can fill named parameter with default from other named';
288 is a(foo => 3), 3, 'Can fill in directly even it has a default value';
289 is a(x => 2, foo => 3), 3, 'direct fill takes precedence';
290
291
292 }
293
09c940c [t/spec] test that :a(1, 2, 3) can be passed to :@a named positional …
moritz authored
294 {
295 sub test_positional_named(:@a) { @a.join('|'); }
296 is test_positional_named(:a(3, 4, 5)), '3|4|5',
297 ':a(1, 2, 3) can be passed to a :@a parameter';
298 is test_positional_named(:a[3, 4, 5]), '3|4|5',
299 ':a[1, 2, 3] can be passed to a :@a parameter';
300 is test_positional_named(:a<3 4 5>), '3|4|5',
301 ':a<1 2 3> can be passed to a :@a parameter';
302
303 }
304
fe63ce6 [t/spec] test that quoted pair keys account for positional arguments,…
moritz authored
305 {
306 sub quoted_named(:$x = 5) { $x };
307 dies_ok { quoted_named( "x" => 5 ) }, 'quoted pair key => positional parameter';
308 }
309
1dfdd28 [t/spec] calling a named parameter with empty name
moritz authored
310 {
311 sub named_empty(:$) {
312 42
313 }
314 my %h = '' => 500;
315 is named_empty(|%h), 42, 'can call function with empty named argument';
316 }
317
8ebc55e [t/spec] more tests for duplicate sub params
kyle authored
318 done_testing;
319
c6a61f5 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
Auzon authored
320 # vim: ft=perl6
Something went wrong with that request. Please try again.