Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 244 lines (196 sloc) 9.011 kb
aa2e9ad8 » moritz
2008-07-07 [spec] simple tests for named parameters
1 use v6;
2 use Test;
3
8ebc55e0 » kyle
2009-10-21 [t/spec] more tests for duplicate sub params
4 plan *;
aa2e9ad8 » moritz
2008-07-07 [spec] simple tests for named parameters
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';
13cc9c44 » Util
2009-10-09 [t/spec] 71 typos in 32 files.
14 eval_dies_ok('a(g=>7)', 'Dies on passing superfluous arguments');
aa2e9ad8 » moritz
2008-07-07 [spec] simple tests for named parameters
15 }
a5cd2dd4 » moritz
2008-07-07 [spec] more tests for named parameters, and fudged them for rakudo
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';
41281a2d » jnthn
2009-01-20 [t/spec] Fix and unfudge a colonpair test.
28 my $w = 5;
29 is c(:$w), 5, 'can use :$x colonpair syntax to call named arg';
ebb8b248 » moritz
2009-01-20 [t/spec] tests for mysub(:$named_arg)
30 eval_dies_ok 'my $y; c(:$y)', 'colonpair with wrong variable name dies';
a5cd2dd4 » moritz
2008-07-07 [spec] more tests for named parameters, and fudged them for rakudo
31 }
c6a61f5a » Auzon
2008-08-15 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
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");
198d3539 » ron
2009-04-28 documented some tests and added some tests for RT #53814
48
49 # part of RT 53814
c6a61f5a » Auzon
2008-08-15 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
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');
de076fdd » moritz
2008-09-15 [t/spec] removed some obsolete usages of 'is $stuff, undef'
105 ok(eval('named_array2(:y, :y)') ~~ undef, 'named and unnamed args - two named with same name');
c6a61f5a » Auzon
2008-08-15 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
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>
133 sub formalize($text, :$case, :$justify) returns List {
134 return($text,$case,$justify);
135 }
136
137 #?rakudo skip 'parsefail'
138 {
139 my ($text,$case,$justify) = formalize('title', case=>'upper');
140 is($text,'title', "text param was positional");
de076fdd » moritz
2008-09-15 [t/spec] removed some obsolete usages of 'is $stuff, undef'
141 ok($justify ~~ undef, "justification param was not given");
c6a61f5a » Auzon
2008-08-15 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
142 is($case, 'upper', "case param was named, and in justification param's position");
143 }
144
145 #?rakudo skip 'parsefail'
146 {
147 my ($text,$case,$justify) = formalize('title', justify=>'left');
148 is($text,'title', "text param was positional");
149 is($justify, 'left', "justify param was named");
de076fdd » moritz
2008-09-15 [t/spec] removed some obsolete usages of 'is $stuff, undef'
150 ok($case ~~ undef, "case was not given at all");
c6a61f5a » Auzon
2008-08-15 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
151 }
152
153 #?rakudo skip 'parsefail'
154 {
155 my ($text,$case,$justify) = formalize("title", :justify<right>, :case<title>);
156
157 is($text,'title', "title param was positional");
158 is($justify, 'right', "justify param was named with funny syntax");
159 is($case, 'title', "case param was named with funny syntax");
160 }
161
162 {
163 sub h($a,$b,$d) { $d ?? h($b,$a,$d-1) !! $a~$b }
164
165 is(h('a','b',1),'ba',"parameters don\'t bind incorrectly");
166 }
167
168 # Slurpy Hash Params
169 {
170 sub slurpee(*%args) { return %args }
171 my %fellowship = slurpee(hobbit => 'Frodo', wizard => 'Gandalf');
172 is(%fellowship<hobbit>, 'Frodo', "hobbit arg was slurped");
173 is(%fellowship<wizard>, 'Gandalf', "wizard arg was slurped");
174 is(+%fellowship, 2, "exactly 2 arguments were slurped");
de076fdd » moritz
2008-09-15 [t/spec] removed some obsolete usages of 'is $stuff, undef'
175 ok(%fellowship<dwarf> ~~ undef, "dwarf arg was not given");
c6a61f5a » Auzon
2008-08-15 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
176 }
177
178 #?rakudo skip 'parsefail on lvalue'
179 {
180 sub named_and_slurp(:$grass, *%rest) { return($grass, %rest) }
181 my ($grass, %rest) = named_and_slurp(sky => 'blue', grass => 'green', fire => 'red');
182 is($grass, 'green', "explicit named arg received despite slurpy hash");
183 is(+%rest, 2, "exactly 2 arguments were slurped");
184 is(%rest<sky>, 'blue', "sky argument was slurped");
185 is(%rest<fire>, 'red', "fire argument was slurped");
de076fdd » moritz
2008-09-15 [t/spec] removed some obsolete usages of 'is $stuff, undef'
186 ok(%rest<grass> ~~ undef, "grass argument was NOT slurped");
c6a61f5a » Auzon
2008-08-15 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
187 }
188
189 {
190 my $ref;
191 sub setref($refin) {
192 $ref = $refin;
193 }
194 my $aref = [0];
195 setref(refin => $aref);
196 $aref[0]++;
197 is($aref[0], 1, "aref actually implemented");
198 is($ref[0], 1, "ref is the same as aref");
199 }
200
baf377f1 » jnthn
2009-02-11 [t/spec] Basic tests for typed named parameters including optional ch…
201 {
202 sub typed_named(Int :$x) { 1 }
203 is(typed_named(:x(42)), 1, 'typed named parameters work...');
204 is(typed_named(), 1, '...when value not supplied also...');
205 dies_ok({ typed_named("BBQ") }, 'and the type check is enforced');
206 }
207
b2558b01 » jnthn
2009-03-25 [t/spec] Tests for renaming parameters in the signature.
208 {
209 sub renames(:y($x)) { $x }
210 is(renames(:y(42)), 42, 'renaming of parameters works');
211 is(renames(y => 42), 42, 'renaming of parameters works');
797076de » moritz
2009-04-14 [t/spec] renamed parameters are tested; remove from TASKS
212 dies_ok { renames(:x(23)) }, 'old name is not available';
b2558b01 » jnthn
2009-03-25 [t/spec] Tests for renaming parameters in the signature.
213 }
214
8ebc55e0 » kyle
2009-10-21 [t/spec] more tests for duplicate sub params
215 # L<06/Parameters and arguments/"All parameters must either have a unique name">
216
846962c2 » kyle
2009-08-01 [t/spec] Test for RT #68086
217 #?rakudo todo 'RT #68086'
218 eval_dies_ok 'sub rt68086( $a, $a ) { }', 'two sub params with the same name';
b2558b01 » jnthn
2009-03-25 [t/spec] Tests for renaming parameters in the signature.
219
8ebc55e0 » kyle
2009-10-21 [t/spec] more tests for duplicate sub params
220 #?rakudo 3 todo 'sub params with the same name'
221 eval_dies_ok 'sub svn28865( $a, :a($b) ) {}',
222 'sub params with the same name via renaming';
223 eval_dies_ok 'sub svn28865( $a, :a(@b) ) {}',
224 'sub params with same name via renaming and different types';
225 eval_dies_ok 'sub svn28865( :$a, :@a ) {}',
226 'sub params with the same name and different types';
227
7f14f44d » kyle
2009-10-09 [t/spec] Test for RT #68524
228 # RT #68524
229 {
230 sub rt68524( :$a! ) {}
231 ok( &rt68524.signature.perl ~~ m/\!/,
232 '.signature.perl with required parameter includes requirement' );
233 }
234
2d1d8c61 » kyle
2009-10-09 [t/spec] Test for RT #69516
235 # RT #69516
236 {
237 sub rt69516( :f($foo) ) { "You passed '$foo' as 'f'" }
238 ok( &rt69516.signature.perl ~~ m/ ':f(' \s* '$foo' \s* ')' /,
239 'parameter rename appears in .signature.perl' );
240 }
241
8ebc55e0 » kyle
2009-10-21 [t/spec] more tests for duplicate sub params
242 done_testing;
243
c6a61f5a » Auzon
2008-08-15 [gsoc_spectest] moved sub_named_params.t content to S06-signature/nam…
244 # vim: ft=perl6
Something went wrong with that request. Please try again.