Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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