Skip to content

HTTPS clone URL

Subversion checkout URL

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