Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 343 lines (267 sloc) 10.007 kb
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
08d48bd [t] Test suite refactoring.
cosimo authored
2
3 use Test;
4
f660aa6 [t/spec] Possibly converted all remaining pod markers to pod6
cosimo authored
5 =begin pod
08d48bd [t] Test suite refactoring.
cosimo authored
6
7 `undef` and `undefine` tests
8
9 This test file contains two sections: a port of the perl5 `undef.t` tests, and
10 perl6-specific tests.
11
f660aa6 [t/spec] Possibly converted all remaining pod markers to pod6
cosimo authored
12 =end pod
08d48bd [t] Test suite refactoring.
cosimo authored
13
14 # Note: See thread "Undef issues" by Adrian Taylor on p6l
15 # L<http://groups.google.com/groups?threadm=20050601002444.GB32060@wall.org>
16 # On Tue, May 24, 2005 at 10:53:59PM +1000, Stuart Cook wrote:
17 # : I'm not sure whether this behaviour is supposed to be changing.
18 #
19 # It is. I think we decided to make the value undef, and the function
20 # undefine(). (But these days most values of undef really ought to
21 # be constructed and returned (or thrown) using fail().)
22 #
23 # Larry
24
e702d26 [t/spec] really test for #61868 this time
moritz authored
25 plan 75;
08d48bd [t] Test suite refactoring.
cosimo authored
26
27 our $GLOBAL;
f660aa6 [t/spec] Possibly converted all remaining pod markers to pod6
cosimo authored
28
08d48bd [t] Test suite refactoring.
cosimo authored
29 # L<S29/Scalar/"=item undef">
f660aa6 [t/spec] Possibly converted all remaining pod markers to pod6
cosimo authored
30
08d48bd [t] Test suite refactoring.
cosimo authored
31 is(undef, undef, "undef is equal to undef");
32 ok(!defined(undef), "undef is not defined");
33
34 {
35 my $a;
9efed85 [t/spec]: Correct tests for definedness in undef.t .
pmichaud authored
36 ok(!defined($a), "uninitialized lexicals are undef");
08d48bd [t] Test suite refactoring.
cosimo authored
37
9efed85 [t/spec]: Correct tests for definedness in undef.t .
pmichaud authored
38 ok(!defined($GLOBAL), "uninitialized package vars are undef");
08d48bd [t] Test suite refactoring.
cosimo authored
39
40 $a += 1; # should not emit a warning. how to test that?
41 ok(defined($a), "initialized var is defined");
42
43 undefine $a;
44 ok(!defined($a), "undefine($a) does");
45
46 $a = "hi";
47 ok(defined($a), "string");
48
49 my $b;
50 $a = $b;
51 ok(!defined($a), "assigning another undef lexical");
52
53 $a = $GLOBAL;
54 ok(!defined($a), "assigning another undef global");
55 }
f660aa6 [t/spec] Possibly converted all remaining pod markers to pod6
cosimo authored
56
08d48bd [t] Test suite refactoring.
cosimo authored
57 # L<S29/Scalar/"=item undefine">
58 {
59 my @ary = "arg1";
60 my $a = @ary.pop;
61 ok(defined($a), "pop from array");
62 $a = @ary.pop;
63 ok(!defined($a), "pop from empty array");
64
65 @ary = "arg1";
66 $a = @ary.shift;
67 ok(defined($a), "shift from array");
68 $a = @ary.shift;
69 ok(!defined($a), "shift from empty array");
70
71 my %hash = ( bar => 'baz', quux => 'quuz' );
72 ok(defined(%hash<bar>), "hash subscript");
73 ok(!defined(%hash<bargho>), "non-existent hash subscript");
74
75 undefine %hash<bar>;
76 ok(!defined(%hash<bar>), "undefine hash subscript");
77
78 %hash<bar> = "baz";
79 %hash.delete("bar");
80 ok(!defined(%hash<bar>), "delete hash subscript");
81
82 ok(defined(@ary), "aggregate array defined");
83 ok(defined(%hash), "aggregate hash defined");
84
85 undefine(@ary);
a20c69c [t/spec/*.t] :todo whackage
lwall authored
86 #?pugs todo 'bug'
87 ok(!defined(@ary), "undefine array");
08d48bd [t] Test suite refactoring.
cosimo authored
88
89 undefine(%hash);
a20c69c [t/spec/*.t] :todo whackage
lwall authored
90 #?pugs todo 'bug'
91 ok(!defined(%hash), "undefine hash");
08d48bd [t] Test suite refactoring.
cosimo authored
92
93 @ary = (1);
94 ok(defined(@ary), "define array again");
95 %hash = (1,1);
96 ok(defined(%hash), "define hash again");
97 }
98
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
99 #?rakudo skip 'access to &your_sub'
08d48bd [t] Test suite refactoring.
cosimo authored
100 {
101 sub a_sub { "møøse" }
102
103 ok(defined(&a_sub), "defined sub");
a20c69c [t/spec/*.t] :todo whackage
lwall authored
104 #?pugs todo 'parsefail'
105 ok(eval('defined(%«$?PACKAGE\::»<&a_sub>)'), "defined sub (symbol table)");
08d48bd [t] Test suite refactoring.
cosimo authored
106
a20c69c [t/spec/*.t] :todo whackage
lwall authored
107 #?pugs todo 'feature'
108 ok(eval('!defined(&a_subwoofer)'), "undefined sub");
109 #?pugs todo 'feature'
110 ok(eval('!defined(%«$?PACKAGE\::»<&a_subwoofer>)'), "undefined sub (symbol table)");
08d48bd [t] Test suite refactoring.
cosimo authored
111 }
112
113 # TODO: find a read-only value to try and assign to, since we don't
114 # have rules right now to play around with (the p5 version used $1)
115 #eval { "constant" = "something else"; };
116 #is($!, "Modification of a read", "readonly write yields exception");
117
118 # skipped tests for tied things
119
120 # skipped test for attempt to undef a bareword -- no barewords here.
121
122 # TODO: p5 "bugid 3096
123 # undefing a hash may free objects with destructors that then try to
124 # modify the hash. To them, the hash should appear empty."
125
126
127 # Test LHS assignment to undef:
297e783 [t] and [t/spec]
moritz authored
128 # XXX shouldn't that be * instead of undef?
129 # if it's correct like this, we need a smartlink for it.
08d48bd [t] Test suite refactoring.
cosimo authored
130
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
131 {
132 my $interesting;
133 (undef, undef, $interesting) = (1,2,3);
134 is($interesting, 3, "Undef on LHS of list assignment");
08d48bd [t] Test suite refactoring.
cosimo authored
135
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
136 (undef, $interesting, undef) = (1,2,3);
137 is($interesting, 2, "Undef on LHS of list assignment");
08d48bd [t] Test suite refactoring.
cosimo authored
138
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
139 ($interesting, undef, undef) = (1,2,3);
140 is($interesting, 1, "Undef on LHS of list assignment");
08d48bd [t] Test suite refactoring.
cosimo authored
141
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
142 sub two_elements() { (1,2) };
143 (undef,$interesting) = two_elements();
144 is($interesting, 2, "Undef on LHS of function assignment");
08d48bd [t] Test suite refactoring.
cosimo authored
145
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
146 ($interesting, undef) = two_elements();
147 is($interesting, 1, "Undef on LHS of function assignment");
148 }
08d48bd [t] Test suite refactoring.
cosimo authored
149
f660aa6 [t/spec] Possibly converted all remaining pod markers to pod6
cosimo authored
150 =begin pod
08d48bd [t] Test suite refactoring.
cosimo authored
151
152 Perl6-specific tests
153
f660aa6 [t/spec] Possibly converted all remaining pod markers to pod6
cosimo authored
154 =end pod
08d48bd [t] Test suite refactoring.
cosimo authored
155
156 {
157 # aggregate references
158
159 my @ary = (<a b c d e>);
160 my $ary_r = @ary; # ref
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
161 isa_ok($ary_r, Array);
08d48bd [t] Test suite refactoring.
cosimo authored
162 ok(defined($ary_r), "array reference");
163
164 undefine @ary;
165 ok(!+$ary_r, "undef array referent");
166
167 is(+$ary_r, 0, "dangling array reference");
168
169 my %hash = (1, 2, 3, 4);
170 my $hash_r = %hash;
171 isa_ok($hash_r, "Hash");
172 ok(defined($hash_r), "hash reference");
173 undefine %hash;
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
174 #?rakudo 2 skip 'hash binding (?)'
08d48bd [t] Test suite refactoring.
cosimo authored
175 ok(defined($hash_r), "undefine hash referent:");
176 is(+$hash_r.keys, 0, "dangling hash reference");
177 }
178
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
179 #?rakudo skip 'Autovivify arrays'
08d48bd [t] Test suite refactoring.
cosimo authored
180 {
181 # types
182 # TODO: waiting on my Dog $spot;
183
184 my Array $an_ary;
185 ok(!defined($an_ary), "my Array");
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
186 ok((try { !defined($an_ary[0]) }), "my Array subscript - undef");
08d48bd [t] Test suite refactoring.
cosimo authored
187 try { $an_ary.push("blergh") };
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
188 ok((try { defined($an_ary.pop) }), "push");
189 ok((try { !defined($an_ary.pop) }), "comes to shove");
08d48bd [t] Test suite refactoring.
cosimo authored
190
191 my Hash $a_hash;
192
193 ok(!defined($a_hash), "my Hash");
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
194 ok((try { !defined($a_hash<blergh>) }), "my Hash subscript - undef");
195 ok((try { !defined($a_hash<blergh>) }), "my Hash subscript - undef, no autovivification happened");
08d48bd [t] Test suite refactoring.
cosimo authored
196
197 $a_hash<blergh> = 1;
198 ok(defined($a_hash.delete('blergh')), "delete");
199 ok(!defined($a_hash.delete("blergh")), " - once only");
200
201 class Dog {};
202 my Dog $spot;
203
204 ok(!defined($spot), "Unelaborated mutt");
205 $spot .= new;
206 ok(defined $spot, " - now real");
207 }
208
209 # rules
210 # TODO. refer to S05
211 # L<S05/Match objects/"they will all be undefined" closure
212 # "let keyword">
213
ea2ee24 [STD] various bugs
lwall authored
214 # - unmatched alternative should bind to undef
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
215 #?rakudo skip 'null PMC access in type()'
ece272c [t/spec] fudged undef.t for rakudo
moritz authored
216 #?DOES 10
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
217 {
4ad9719 [undef.t] there is no my() function...declarators require space
lwall authored
218 my ($num, $alpha);
219 my ($rx1, $rx2);
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
220 eval '
221 $rx1 = rx
222 / [ (\d+) { let $<num> := $0 }
223 | (<alpha>+) { let $<alpha> := $1 }
224 ]
225 /;
226 $rx2 = rx
227 / [ $<num> := (\d+)
228 | $<alpha>:= (<alpha>+)
229 ]
230 /;
231 ';
232 for (<rx1 rx2>) {
233 # I want symbolic lookups because I need the rx names for test results.
234
235 eval '"1" ~~ %MY::{$_}';
236 #?pugs todo 'unimpl'
237 ok(defined($num), '{$_}: successful hypothetical');
238 ok(!defined($alpha), '{$_}: failed hypothetical');
239
240 eval '"A" ~~ %MY::{$_}';
241 ok(!defined($num), '{$_}: failed hypothetical (2nd go)');
242 #?pugs todo 'unimpl'
243 ok(defined($alpha), '{$_}: successful hypothetical (2nd go)');
244 }
08d48bd [t] Test suite refactoring.
cosimo authored
245
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
246 # - binding to hash keys only would leave values undef
247 eval '"a=b\nc=d\n" ~~ / $<matches> := [ (\w) = \N+ ]* /';
248 #?pugs todo 'unimpl'
249 ok(eval('$<matches> ~~ all(<a b>)'), "match keys exist");
08d48bd [t] Test suite refactoring.
cosimo authored
250
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
251 #ok(!defined($<matches><a>) && !defined($<matches><b>), "match values don't");
252 #?pugs todo 'unimpl'
253 ok(0 , "match values don't");
254 }
3604100 more buglets
lwall authored
255
ece272c [t/spec] fudged undef.t for rakudo
moritz authored
256 #?DOES 1
08d48bd [t] Test suite refactoring.
cosimo authored
257 {
258 # - $0, $1 etc. should all be undef after a failed match
259 # (except for special circumstances)
972b6e4 [t/spec] simplify a few tests to use /../ instead of rx/../
moritz authored
260 "abcde" ~~ /(.)(.)(.)/;
261 "abcde" ~~ /(\d)/;
08d48bd [t] Test suite refactoring.
cosimo authored
262 ok((!try { grep { defined($_) }, ($0, $1, $2, $3, $4, $5) }),
263 "all submatches undefined after failed match") or
264 diag("match state: " ~ eval '$/');
265
266 # XXX write me: "special circumstances"
267 }
268
269
270 # subroutines
271 {
272 sub bar ($bar, $baz?, :$quux) {
273 is($bar, "BAR", "defined param"); # sanity
274
60d29c6 fix some legacy smartlink syntax
szabgab authored
275 # L<S06/Optional parameters/Missing optional arguments>
08d48bd [t] Test suite refactoring.
cosimo authored
276 ok(!defined($baz), "unspecified optional param");
277
60d29c6 fix some legacy smartlink syntax
szabgab authored
278 # L<S06/Named parameters/Named parameters are optional>
08d48bd [t] Test suite refactoring.
cosimo authored
279 ok(!defined($quux), "unspecified optional param");
280 }
281
282 bar("BAR");
283
284 }
285
286 # autoloading
287 # L<S10/Autoloading>
288
289 # Currently waiting on
290 # - packages
291 # - symtable hash
292 # - autoloading itself
293
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
294 #?pugs skip 'parsefail'
295 #?rakudo skip 'parsefail'
296 flunk('FIXME: parsefail');
f9002e6 [STD] now does reduce tokens correctly; we now use a variable length
lwall authored
297 # {
08d48bd [t] Test suite refactoring.
cosimo authored
298 # package AutoMechanic {
299 # AUTOSCALAR { \my $_scalar }
300 # AUTOARRAY { \my @_array }
301 # AUTOHASH { \my %_hash }
302 # AUTOSUB { { "code" } }
303 # AUTOMETH { { "code" } }
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
304 #
08d48bd [t] Test suite refactoring.
cosimo authored
305 # AUTOSCALARDEF { %::«{'$' ~ $_}» = "autoscalardef" }
306 # AUTOARRAYDEF { %::«{'@' ~ $_}» = "autoarraydef".split("") }
307 # AUTOHASHDEF { %::«{'%' ~ $_}» = <autohashdef yes> }
308 # AUTOSUBDEF { %::«{'&' ~ $_}» = { "autosubdef" } }
309 # AUTOMETHDEF { %::«{'&' ~ $_}» = { "automethdef" } }
310 # }
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
311 #
08d48bd [t] Test suite refactoring.
cosimo authored
312 # is(WHAT $AutoMechanic::scalar0, "Scalar", "autoload - scalar");
313 # is(WHAT @AutoMechanic::array0, "Array", "autoload - array");
314 # is(WHAT %AutoMechanic::hash, "Hash", "autoload - hash");
315 # is(WHAT &AutoMechanic::sub0, "Code", "autoload - sub");
316 # is(WHAT AutoMechanic.can("meth0"), "Code", "autoload - meth");
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
317 #
08d48bd [t] Test suite refactoring.
cosimo authored
318 # is($AutoMechanic::scalar, "autoscalardef", "autoloaddef - scalar");
319 # is(~@AutoMechanic::ary, ~("autoarraydef".split(""), "autoloaddef - array");
320 # is(~%AutoMechanic::hash, ~<autohashdef yes>, "autoloaddef - hash");
321 # is(&AutoMechanic::sub.(), "autosubdef", "autoloaddef - sub");
322 # is(AutoMechanic.meth(), "automethdef", "autoloaddef - method");
f9002e6 [STD] now does reduce tokens correctly; we now use a variable length
lwall authored
323 # }
08d48bd [t] Test suite refactoring.
cosimo authored
324
325 # Extra tests added due to apparent bugs
326 is((undef) + 1, 1, 'undef + 1');
327 is(1 + (undef), 1, '1 + undef');
328 is((undef) * 2, 0, 'undef * 2');
329 is(2 * (undef), 0, '2 * undef');
330 is((undef) xx 2, [undef, undef], 'undef xx 2');
331 is((undef) * (undef), 0, 'undef * undef');
332
333 # L<http://colabti.de/irclogger/irclogger_log/perl6?date=2006-09-12,Tue&sel=145#l186>
334 # See log above. From IRC, TimToady says that both of these
335 # should be false. (At time of writing, @(undef,) is true.)
a20c69c [t/spec/*.t] :todo whackage
lwall authored
336 #?pugs todo 'feature', :depends<@() imposing context and not [] constructor>;
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
337 #?rakudo 2 skip 'todo: lists, defined, truthness'
a20c69c [t/spec/*.t] :todo whackage
lwall authored
338 is ?(@(undef,)), Bool::False, '?(@(undef,)) is false';
08d48bd [t] Test suite refactoring.
cosimo authored
339 is ?(list(undef,)), Bool::False, '?(@(undef,)) is false';
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
340
e702d26 [t/spec] really test for #61868 this time
moritz authored
341 lives_ok { uc(eval("")) }, 'can use eval("") in further expressions';
5f0915d [t/spec] partially fudge undef.t for rakudo. Pending fudge bugs.
moritz authored
342 # vim: ft=perl6
Something went wrong with that request. Please try again.