Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 193 lines (152 sloc) 5.947 kB
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
2 use Test;
3
4 # L<S09/"Autovivification">
5
c3c82c7 [t/spec] cleaned up autovivification.t a bit, and added a few more tests
moritz authored
6 plan 42;
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
7
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
8 {
9 my %hash;
10 %hash<a>;
11 ok !%hash.exists('a'), 'just mentioning a hash value should not autovivify it';
12 }
13
14 {
15 my %hash;
16
17 %hash<key>[42] = 17;
18 is %hash<key>[42], 17, "autovivification of a hash element to an arrayref worked";
c3c82c7 [t/spec] cleaned up autovivification.t a bit, and added a few more tests
moritz authored
19 is +%hash.keys, 1, 'Created one hash item';
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
20 }
21
ab923c7 [t/spec] Note an RT ticket that corresponds to an existing test.
kyle authored
22 # RT #61740
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
23 {
24 my %hash;
25
26 %hash<key><innerkey> = 17;
27 is %hash<key><innerkey>, 17, "autovivification of a hash element to a hashref worked";
c3c82c7 [t/spec] cleaned up autovivification.t a bit, and added a few more tests
moritz authored
28 isa_ok %hash<key>, Hash, 'Inner hash item is really a Hash';
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
29 }
30
31 # Autovification by push, unshift, etc.
c3c82c7 [t/spec] cleaned up autovivification.t a bit, and added a few more tests
moritz authored
32 # XXX I understand that @array[0].push(...) should autovivify an Array
33 # in @array[0], but is that also true for a normal scalar?
3a7da94 @coke niecza fudging
coke authored
34 #?niecza skip 'Unable to resolve method push in class Any'
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
35 {
36 my $arrayref;
37
38 push $arrayref, 1,2,3;
39 is ~$arrayref, "1 2 3", "autovivification to an array by &push";
c3c82c7 [t/spec] cleaned up autovivification.t a bit, and added a few more tests
moritz authored
40 isa_ok $arrayref, Array, 'autovivified to Array';
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
41 }
42
3a7da94 @coke niecza fudging
coke authored
43 #?niecza skip 'Unable to resolve method unshift in class Any'
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
44 {
45 my $arrayref;
46
47 unshift $arrayref, 1,2,3;
48 is ~$arrayref, "1 2 3", "autovivification to an array by &unshift";
49 }
50
51 # Autovification by push, unshift, etc. of an array/hash element
c3c82c7 [t/spec] cleaned up autovivification.t a bit, and added a few more tests
moritz authored
52 # L<S09/Autovivification/"push, unshift, .[]">
3a7da94 @coke niecza fudging
coke authored
53 #?niecza skip 'Unable to resolve method push in class Any'
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
54 {
55 my @array;
56
57 push @array[2], 1,2,3;
58 is ~@array, " 1 2 3", "autovivification of an array element to an array by &push";
59 }
60
ddb7f4f @coke unfudge passing tests, tag with RT#
coke authored
61 #RT #84000
3a7da94 @coke niecza fudging
coke authored
62 #?niecza skip 'Unable to resolve method push in class Any'
29e16b6 @coke pugs fudge
coke authored
63 #?pugs todo
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
64 {
65 my %hash;
66
67 push %hash<key>, 1,2,3;
ddb7f4f @coke unfudge passing tests, tag with RT#
coke authored
68 is ~%hash, "key\t1 2 3", "autovivification of an hash element to an array by &push";
1424542 Add test to check autovivification of accessing a top-level hash elem…
Ovid authored
69 }
3b37c07 Trying to make this suitable for "make spectest" with Rakudo.
Ovid authored
70
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
71 # Simple hash autovivification
81a7e46 Trying to make this fudgeable test work. Need to understand fudge bet…
Ovid authored
72 # Actually, that first test passes, but I can't figure out how to skip just
73 # the next two.
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
74 {
75 my $hashref;
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
76 ok $hashref !~~ Hash, "uninitialized variable is not a Hash (1)";
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
77
78 $hashref<key> = 23;
79 is $hashref<key>, 23, "hash element assignment worked";
3a7da94 @coke niecza fudging
coke authored
80 #?niecza skip 'No value for parameter \$other in CORE Any.isa'
29e16b6 @coke pugs fudge
coke authored
81 #?pugs skip 'isa multi variant'
ddb7f4f @coke unfudge passing tests, tag with RT#
coke authored
82 #?rakudo skip 'isa multi variant'
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
83 ok $hashref.isa !~~ Hash, "uninitialized variable was autovivified to a hash (1)";
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
84 }
85
86 {
87 my $hashref;
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
88 ok $hashref !~~ Hash, "uninitialized variable is not a Hash (2)";
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
89
90 # Note that
91 # Autovivification will only happen if the *vivifiable* *path* is used as a container
92 # ... value extraction does not autovivify.
93 lives_ok { my $elem = $hashref<key> },
94 "accessing a not existing hash element of an uninitialized variable works";
29e16b6 @coke pugs fudge
coke authored
95 #?pugs todo
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
96 ok $hashref !~~ Hash, "uninitialized variable is not autovivified to a hash (2)";
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
97
98 my $hashref2;
99 lives_ok { my $elem2 = $hashref2<key2><a><b><c><d><e><f> },
100 "accessing a not existing hash element of an uninitialized variable works (2)";
29e16b6 @coke pugs fudge
coke authored
101 #?pugs 2 todo
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
102 ok $hashref2 !~~ Hash, "uninitialized variable is not autovivified to a hash (3)";
103 ok $hashref2<key2><a><b><c><d><e> !~~ Hash, "uninitialized variable is not autovivified to a hash (4)";
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
104 }
105
106 {
107 my $hashref;
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
108 ok $hashref !~~ Hash, "uninitialized variable is not a Hash (3)";
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
109
110 lives_ok { my $elem := $hashref<key> },
111 "binding a not existing hash element of an uninitialized variable works";
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
112 #?rakudo todo 'autoviv, binding'
113 ok $hashref ~~ Hash, "uninitialized variable is autovivified to a hash (4)";
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
114
115 lives_ok { my $elem2 := $hashref<key2><a><b><c><d><e><f> },
116 "binding a not existing hash element of an uninitialized variable works (2)";
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
117 #?rakudo todo 'autoviv, binding'
118 ok $hashref<key2><a><b><c><d><e> ~~ Hash, "uninitialized variable is autovivified to a hash (5)";
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
119 }
120
121 # Simple array autovivification
122 {
123 my $arrayref;
124 ok !$arrayref.isa(Array), "uninitialized variable is not an Array (1)";
125
126 $arrayref[42] = 23;
127 ok $arrayref.isa(Array), "uninitialized variable was autovivified to an array (1)";
128 is $arrayref[42], 23, "array element assignment worked";
129 }
130
131 {
132 my $arrayref;
133 ok !$arrayref.isa(Array), "uninitialized variable is not an Array (2)";
134
135 # Note that
136 # Autovivification will only happen if the *vivifiable* *path* is used as a container
137 # ... value extraction does not autovivify.
138 lives_ok { my $elem = $arrayref[42] },
139 "accessing a not existing array element of an uninitialized variable works";
29e16b6 @coke pugs fudge
coke authored
140 #?pugs todo
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
141 ok !$arrayref.isa(Array), "uninitialized variable was not autovivified to an array (2)";
142
143 my $arrayref2;
144 lives_ok { my $elem = $arrayref2[1][2][3][4][5][6] },
145 "accessing a not existing array element of an uninitialized variable works";
29e16b6 @coke pugs fudge
coke authored
146 #?pugs 2 todo
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
147 ok !$arrayref2.isa(Array), "uninitialized variable was not autovivified to an array (3)";
148 ok !$arrayref2[1][2][3][4][5].isa(Array), "uninitialized variable was not autovivified to an array (4)";
149 }
150
151 {
152 my $arrayref;
153 ok !$arrayref.isa(Array), "uninitialized variable is not an Array (3)";
154
155 lives_ok { my $elem := $arrayref[42] },
156 "binding a not existing array element of an uninitialized variable works (1)";
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
157 #?rakudo todo 'unknown'
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
158 ok $arrayref.isa(Array), "uninitialized variable is autovivified to an array (1)";
159
160 lives_ok { my $elem2 := $arrayref[1][2][3][4][5][6] },
161 "binding a not existing array element of an uninitialized variable works (2)";
e3ca987 [t/spec] fudge autoviv tests for rakudo
moritz authored
162 #?rakudo todo 'unknown'
5a5864e [t/spec] autovivification test script moved to spec/S09
cosimo authored
163 ok $arrayref[1][2][3][4][5].isa(Array), "uninitialized variable is autovivified to an array (2)";
164 }
165
166
167 # Autovivification of an array/hash element
168 {
169 my @array;
170
171 @array[42][23] = 17;
172 is @array[42][23], 17, "autovivification of an array element to an arrayref worked";
173 }
174
175 {
176 my @array;
177
178 @array[42]<key> = 17;
179 is @array[42]<key>, 17, "autovivification of an array element to a hashref worked";
180 }
181
182
183 lives_ok {
184 &New::Package::foo;
185 # this is ok, as you don't have to predeclare globally qualified variables
186 }, "using an undeclared globaly qualified code variable in void context is ok";
187
188 dies_ok {
189 &New::Package::foo();
190 }, "...but invoking undeclared globally qualifed code variable should die";
c3c82c7 [t/spec] cleaned up autovivification.t a bit, and added a few more tests
moritz authored
191
192 # vim: ft=perl6
Something went wrong with that request. Please try again.