Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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