Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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