Skip to content
Newer
Older
100644 503 lines (413 sloc) 14.9 KB
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
1 use v6;
2
3 use Test;
4
e9a791f [t/spec] Tests for attributes with the & sigil.
jnthn authored May 20, 2009
5 plan 126;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
6
7 =begin pod
8
9 Class attributes tests from L<S12/Attributes>
10
11 =end pod
12
13 eval_dies_ok 'has $.x;', "'has' only works inside of class|role definitions";
14
15 # L<S12/Attributes/the automatic generation of an accessor method of the same name>
16
17 class Foo1 { has $.bar; };
18
19 {
20 my $foo = Foo1.new();
21 ok($foo ~~ Foo1, '... our Foo1 instance was created');
22 my $val;
23 #?pugs 2 todo 'feature'
24 lives_ok {
25 $val = $foo.can("bar")
26 }, '.. checking autogenerated accessor existence';
27 ok($val, '... $foo.can("bar") should have returned true');
28 ok($foo.bar() ~~ undef, '.. autogenerated accessor works');
29 ok($foo.bar ~~ undef, '.. autogenerated accessor works w/out parens');
30 }
31
32 # L<S12/Attributes/Pseudo-assignment to an attribute declaration specifies the default>
33
34 {
35 class Foo2 { has $.bar = "baz"; };
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
36 my $foo = Foo2.new();
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
37 ok($foo ~~ Foo2, '... our Foo2 instance was created');
38 ok($foo.can("bar"), '.. checking autogenerated accessor existence');
39 is($foo.bar(), "baz", '.. autogenerated accessor works');
40 is($foo.bar, "baz", '.. autogenerated accessor works w/out parens');
41 dies_ok { $foo.bar = 'blubb' }, 'attributes are ro by default';
42 }
43
44 # L<S12/Attributes/making it an lvalue method>
45
46
47 #?pugs todo 'instance attributes'
48 {
49 class Foo3 { has $.bar is rw; };
50 my $foo = Foo3.new();
51 ok($foo ~~ Foo3, '... our Foo3 instance was created');
52 my $val;
53 lives_ok {
54 $val = $foo.can("bar");
55 }, '.. checking autogenerated accessor existence';
56 ok $val, '... $foo.can("bar") should have returned true';
297e783 [t] and [t/spec]
moritz authored Jan 5, 2009
57 ok($foo.bar() ~~ undef, '.. autogenerated accessor works');
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
58 lives_ok {
59 $foo.bar = "baz";
60 }, '.. autogenerated mutator as lvalue works';
61 is($foo.bar, "baz", '.. autogenerated mutator as lvalue set the value correctly');
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored Dec 7, 2008
62 #?rakudo 2 todo 'oo'
63 lives_ok { $foo.bar("baz2"); }, '.. autogenerated mutator works as method';
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
64 is $foo.bar, "baz2", '.. autogenerated mutator as method set the value correctly';
65 }
66
67 # L<S12/Attributes/Private attributes use an exclamation to indicate that no public accessor is>
68
69
70 {
71 class Foo4 { has $!bar; };
72 my $foo = Foo4.new();
73 ok($foo ~~ Foo4, '... our Foo4 instance was created');
74 #?pugs eval 'todo'
75 ok(!$foo.can("bar"), '.. checking autogenerated accessor existence', );
76 }
77
78
79 {
80 class Foo4a { has $!bar = "baz"; };
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
81 my $foo = Foo4a.new();
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
82 ok($foo ~~ Foo4a, '... our Foo4a instance was created');
83 #?pugs eval 'todo'
84 ok(!$foo.can("bar"), '.. checking autogenerated accessor existence');
85 }
86
87
88 # L<S12/Attributes>
89
90
91 {
92 class Foo5 {
93 has $.tail is rw;
94 has @.legs;
95 has $!brain;
96
97 method set_legs (*@legs) { @.legs = @legs }
98 method inc_brain () { $!brain++ }
99 method get_brain () { $!brain }
100 };
101 my $foo = Foo5.new();
102 ok($foo ~~ Foo5, '... our Foo5 instance was created');
103
104 lives_ok {
105 $foo.tail = "a";
106 }, "setting a public rw attribute";
107 is($foo.tail, "a", "getting a public rw attribute");
108
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored Dec 7, 2008
109 #?rakudo 2 todo 'oo'
110 lives_ok { $foo.set_legs(1,2,3) }, "setting a public ro attribute (1)";
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
111 is($foo.legs.[1], 2, "getting a public ro attribute (1)");
112
113 dies_ok {
114 $foo.legs = (4,5,6);
115 }, "setting a public ro attribute (2)";
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored Dec 7, 2008
116 #?rakudo todo 'oo'
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
117 is($foo.legs.[1], 2, "getting a public ro attribute (2)");
118
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored Dec 7, 2008
119 lives_ok { $foo.inc_brain(); }, "modifiying a private attribute (1)";
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
120 is($foo.get_brain, 1, "getting a private attribute (1)");
121 lives_ok {
122 $foo.inc_brain();
123 }, "modifiying a private attribute (2)";
124 is($foo.get_brain, 2, "getting a private attribute (2)");
125 }
126
127 # L<S12/Construction and Initialization/If you name an attribute as a parameter, that attribute is initialized directly, so>
128
129
0e9f8f9 [t/spec] remove tests that used 'is build' or 'will build' from A12
moritz authored Mar 29, 2009
130 #?rakudo skip 'calling positional params by name'
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
131 {
132 class Foo6 {
133 has $.bar is rw;
134 has $.baz;
135 has $!hidden;
136
0e9f8f9 [t/spec] remove tests that used 'is build' or 'will build' from A12
moritz authored Mar 29, 2009
137 submethod BUILD($self, $.bar, $.baz, $!hidden) {}
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
138 method get_hidden() { $!hidden }
139 }
140
141 my $foo = Foo6.new(bar => 1, baz => 2, hidden => 3);
142 ok($foo ~~ Foo6, '... our Foo6 instance was created');
143
144 is($foo.bar, 1, "getting a public rw attribute (1)" );
145 is($foo.baz, 2, "getting a public ro attribute (2)" );
146 is($foo.get_hidden, 3, "getting a private ro attribute (3)" );
147 }
148
149 # check that doing something in submethod BUILD works
150
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
151 #?rakudo skip 'parse fail'
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
152 {
153 class Foo6a {
154 has $.bar is rw;
155 has $.baz;
156 has $!hidden;
157
158 submethod BUILD ($!hidden, $.bar = 10, $.baz?) {
159 $.baz = 5;
160 }
161 method get_hidden() { $!hidden }
162 }
163
164 my $foo = Foo6a.new(bar => 1, hidden => 3);
165 ok($foo ~~ Foo6a, '... our Foo6a instance was created');
166
167 is($foo.bar, 1, "getting a public rw attribute (1)" );
168 is($foo.baz, 5, "getting a public rw attribute (2)" );
169 is($foo.get_hidden, 3, "getting a private ro attribute (3)" );
170 }
171
172 # check that assignment in submethod BUILD works with a bare return, too
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
173 #?rakudo skip 'parse fail'
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
174 {
175 class Foo6b {
176 has $.bar is rw;
177 has $.baz;
178
179 submethod BUILD ($.bar = 10, $.baz?) {
180 $.baz = 9;
181 return;
182 }
183 }
184
185 my $foo = Foo6b.new(bar => 7);
186 ok($foo ~~ Foo6b, '... our Foo6b instance was created');
187
188 is($foo.bar, 7, "getting a public rw attribute (1)" );
189 is($foo.baz, 9, "getting a public rw attribute (2)" );
190 }
191
d154816 [t/spec] more smartlink fixing
moritz authored Apr 26, 2009
192 # L<S12/Attributes>
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
193 ok eval('class Foo7 { has $.attr = 42 }; 1'), "class definition worked";
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
194 is eval('Foo7.new.attr'), 42, "default attribute value (1)";
195
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored Dec 7, 2008
196 {
197 my $was_in_supplier = 0;
198 sub forty_two_supplier() { $was_in_supplier++; 42 }
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for a…
jnthn authored May 20, 2009
199 ok eval('class Foo10 { has $.attr = forty_two_supplier() }; 1'),
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored Dec 7, 2008
200 'class definition using "= {...}" worked';
201 is eval('Foo10.new.attr'), 42, "default attribute value (4)";
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for a…
jnthn authored May 20, 2009
202 is $was_in_supplier, 1, "forty_two_supplier() was actually executed";
203 eval('Foo10.new');
204 is $was_in_supplier, 2, "forty_two_supplier() is executed per instantiation";
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored Dec 7, 2008
205 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
206
207 # check that doing something in submethod BUILD works
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
208 #?rakudo skip 'parse failure'
209 {
210 class Foo7 {
211 has $.bar;
212 has $.baz;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
213
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
214 submethod BUILD ($.bar = 5, $baz = 10 ) {
215 $.baz = 2 * $baz;
216 }
217 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
218
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
219 my $foo7 = Foo7.new();
220 is( $foo7.bar, 5,
221 'optional attribute should take default value without passed-in value' );
222 is( $foo7.baz, 20,
223 '... optional non-attribute should too' );
224 $foo7 = Foo7.new( :bar(4), :baz(5) );
225 is( $foo7.bar, 4,
226 'optional attribute should take passed-in value over default' );
227 is( $foo7.baz, 10,
228 '... optional non-attribute should too' );
229 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
230
231
232 # check that args are passed to BUILD
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
233 #?rakudo skip 'submethod parsing'
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
234 {
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
235 class Foo8 {
236 has $.a;
237 has $.b;
238
239 submethod BUILD(:$foo, :$bar) {
240 $.a = $foo;
241 $.b = $bar;
242 }
243 }
244
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
245 my $foo = Foo8.new(foo => 'c', bar => 'd');
246 ok($foo.isa(Foo8), '... our Foo8 instance was created');
247
248 is($foo.a, 'c', 'BUILD received $foo');
249 is($foo.b, 'd', 'BUILD received $bar');
250 }
251
252 # check mixture of positional/named args to BUILD
253
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
254 {
255 class Foo9 {
256 has $.a;
257 has $.b;
258
259 submethod BUILD($foo, :$bar) {
260 $.a = $foo;
261 $.b = $bar;
262 }
263 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
264
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
265 dies_ok({ Foo9.new('pos', bar => 'd') }, 'cannot pass positional to .new');
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
266 }
267
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
268 # check $self is passed to BUILD
269 #?rakudo skip 'submethod parsing'
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
270 {
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
271 class Foo10 {
272 has $.a;
273 has $.b;
274 has $.c;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
275
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
276 submethod BUILD(Class $self: :$foo, :$bar) {
277 $.a = $foo;
278 $.b = $bar;
279 $.c = 'y' if $self.isa(Foo10);
280 }
281 }
282
283 {
284 my $foo = Foo10.new(foo => 'c', bar => 'd');
285 ok($foo.isa(Foo10), '... our Foo10 instance was created');
286
287 is($foo.a, 'c', 'BUILD received $foo');
288 is($foo.b, 'd', 'BUILD received $bar');
289 is($foo.c, 'y', 'BUILD received $self');
290 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored Dec 7, 2008
291 }
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
292
e913c98 [t/spec] regression tests for RT #61100
moritz authored Dec 7, 2008
293 {
294 class WHAT_ref { };
295 class WHAT_test {
296 has WHAT_ref $.a;
297 has WHAT_test $.b is rw;
298 }
299 my $o = WHAT_test.new(a => WHAT_ref.new(), b => WHAT_test.new());
ccbeedd [t/spec]: Use isa_ok instead of WHAT to test types.
pmichaud authored Apr 28, 2009
300 isa_ok $o.a.WHAT, WHAT_ref, '.WHAT on attributes';
301 isa_ok $o.b.WHAT, WHAT_test, '.WHAT on attributes of same type as class';
e913c98 [t/spec] regression tests for RT #61100
moritz authored Dec 7, 2008
302 my $r = WHAT_test.new();
303 lives_ok {$r.b = $r}, 'type check on recursive data structure';
ccbeedd [t/spec]: Use isa_ok instead of WHAT to test types.
pmichaud authored Apr 28, 2009
304 isa_ok $r.b.WHAT, WHAT_test, '.WHAT on recursive data structure';
e913c98 [t/spec] regression tests for RT #61100
moritz authored Dec 7, 2008
305
306 }
307
e47e77f [t/spec] Tests for .clone with parameters.
jnthn authored Dec 17, 2008
308 # Tests for clone.
309 {
310 class CloneTest { has $.x is rw; has $.y is rw; }
311 my $a = CloneTest.new(x => 1, y => 2);
312 my $b = $a.clone();
313 is $b.x, 1, 'attribute cloned';
314 is $b.y, 2, 'attribute cloned';
315 $b.x = 3;
316 is $b.x, 3, 'changed attribute on clone...';
317 is $a.x, 1, '...and original not affected';
318 my $c = $a.clone(x => 42);
319 is $c.x, 42, 'clone with parameters...';
320 is $a.x, 1, '...leaves original intact...';
321 is $c.y, 2, '...and copies what we did not change.';
322 }
323
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored Jan 6, 2009
324 # tests for *-1 indexing on classes, RT #61766
325 {
326 class ArrayAttribTest {
327 has @.a is rw;
328 method init {
329 @.a = <a b c>;
330 }
331 method m0 { @.a[0] };
332 method m1 { @.a[*-2] };
333 method m2 { @.a[*-1] };
334 }
335 my $o = ArrayAttribTest.new;
336 $o.init;
337 is $o.m0, 'a', '@.a[0] works';
338 is $o.m1, 'b', '@.a[*-2] works';
29b569a [t/spec] unfudge @.a[*-1] tests for rakudo
moritz authored Jan 22, 2009
339 is $o.m2, 'c', '@.a[*-1] works';
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored Jan 6, 2009
340 }
341
342 {
343 class AttribWriteTest {
344 has @.a;
345 has %.h;
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored Jan 7, 2009
346 method set_array1 {
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored Jan 6, 2009
347 @.a = <c b a>;
348 }
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored Jan 7, 2009
349 method set_array2 {
350 @!a = <c b a>;
351 }
352 method set_hash1 {
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored Jan 6, 2009
353 %.h = (a => 1, b => 2);
354 }
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored Jan 7, 2009
355 method set_hash2 {
356 %!h = (a => 1, b => 2);
357 }
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored Jan 6, 2009
358 }
359
360 my $x = AttribWriteTest.new;
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored Jan 7, 2009
361 # see Larry's reply to
362 # http://groups.google.com/group/perl.perl6.language/browse_thread/thread/2bc6dfd8492b87a4/9189d19e30198ebe?pli=1
363 # on why these should fail.
364 dies_ok { $x.set_array1 }, 'can not assign to @.array attribute';
365 dies_ok { $x.set_hash1 }, 'can not assign to %.hash attribute';
366 lives_ok { $x.set_array2 }, 'can assign to @!array attribute';
367 lives_ok { $x.set_hash2 }, 'can assign to %!hash attribute';
2e82ab2 [t/spec] tests for RT #61914
moritz authored Jan 11, 2009
368 }
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored Jan 6, 2009
369
2e82ab2 [t/spec] tests for RT #61914
moritz authored Jan 11, 2009
370 # test that whitespaces after 'has (' are allowed.
371 # This used to be a Rakudo bug (RT #61914)
372 {
373 class AttribWsTest {
374 has ( $.this,
375 $.that,
376 );
377 }
378 my AttribWsTest $o .= new( this => 3, that => 4);
379 is $o.this, 3, 'could use whitespace after "has ("';
380 is $o.that, 4, '.. and a newline within the has() declarator';
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored Jan 6, 2009
381 }
62eb139 [t/spec] Test for RT#62902.
jnthn authored Feb 25, 2009
382
383 # test typed attributes and === (was Rakudo RT#62902).
384 {
385 class TA1 { }
386 class TA2 {
387 has TA1 $!a;
388 method foo { $!a === TA1 }
389 }
390 ok(TA2.new.foo, '=== works on typed attribute initialized with proto-object');
391 }
392
8cdbdfd [t/spec] merge attribute_of_return_value.t into instance.t
moritz authored Mar 19, 2009
393 # used to be pugs regression
394 {
395 class C_Test { has $.a; }
396 sub f() { C_Test.new(:a(123)) }
397 sub g() { my C_Test $x .= new(:a(123)); $x }
398
399 is(C_Test.new(:a(123)).a, 123, 'C_Test.new().a worked');
400
401 my $o = f();
402 is($o.a, 123, 'my $o = f(); $o.a worked');
403
404 is((try { f().a }), 123, 'f().a worked (so the pugsbug is fixed (part 1))');
405
406 is((try { g().a }), 123, 'g().a worked (so the pugsbug is fixed (part 2))');
407 }
408
1e1543a [t] merge oo/attributes/attribute_list.t into instance.t
moritz authored Apr 4, 2009
409 # was also a pugs regression:
410 # Modification of list attributes created with constructor fails
411
412 {
413 class D_Test {
414 has @.test is rw;
415 method get () { shift @.test }
416 }
417
418 my $test1 = D_Test.new();
419 $test1.test = [1];
420 is($test1.test, [1], "Initialized outside constructor");
421 is($test1.get , 1 , "Get appears to have worked");
422 is($test1.test, [], "Get Worked!");
423
424 my $test2 = D_Test.new( :test([1]) );
425 is($test2.test, [1], "Initialized inside constructor");
426 is($test2.get , 1 , "Get appears to have worked");
427 is($test2.test, [], "Get Worked!");
428 }
429
c077ab0 [t/spec] typed array and hash attributes
moritz authored Apr 10, 2009
430 # test typed attributes
431 # TODO: same checks on private attributes
432 {
433 class TypedAttrib {
434 has Int @.a is rw;
435 has Int %.h is rw;
05c70f6 [t/spec] tests for RT #62838
moritz authored Apr 29, 2009
436 has Int @!pa;
437 has Int %!ph;
438 method pac { @!pa.elems };
439 method phc { %!ph.elems };
c077ab0 [t/spec] typed array and hash attributes
moritz authored Apr 10, 2009
440 }
441 my $o = try { TypedAttrib.new };
442 ok $o.defined, 'created object with typed attributes';
05c70f6 [t/spec] tests for RT #62838
moritz authored Apr 29, 2009
443 is $o.a.elems, 0, 'typed public array attribute is empty';
444 is $o.h.elems, 0, 'typed public hash attribute is empty';
445 is $o.pac, 0, 'typed private array attribute is empty';
446 is $o.phc, 0, 'typed private hash attribute is empty';
71ef855 [t/spec] Some unfuding for Rakudo.
jnthn authored May 20, 2009
447
c077ab0 [t/spec] typed array and hash attributes
moritz authored Apr 10, 2009
448 ok $o.a.of === Int, 'array attribute is typed';
449 lives_ok { $o.a = (2, 3) }, 'Can assign to typed drw-array-attrib';
450 lives_ok { $o.a[2] = 4 }, 'Can insert into typed rw-array-attrib';
451 lives_ok { $o.a.push: 5 }, 'Can push onto typed rw-array-attrib';
452 is $o.a.join('|'), '2|3|4|5',
453 '... all of the above actually worked (not only lived)';
454
455 dies_ok { $o.a = <foo bar> }, 'type enforced on array attrib (assignment)';
456 dies_ok { $o.a[2] = $*IN }, 'type enforced on array attrib (item assignment)';
457 dies_ok { $o.a.push: [2, 3]}, 'type enforced on array attrib (push)';
458 dies_ok { $o.a[42]<foo> = 3}, 'no autovivification (typed array)';
459
71ef855 [t/spec] Some unfuding for Rakudo.
jnthn authored May 20, 2009
460 #?rakudo todo 'over-eager auto-vivification bugs'
c077ab0 [t/spec] typed array and hash attributes
moritz authored Apr 10, 2009
461 is $o.a.join('|'), '2|3|4|5',
462 '... all of the above actually did nothing (not just died)';
463
464 ok $o.h.of === Int, 'hash attribute is typed';
465 lives_ok {$o.h = { a => 1, b => 2 } }, 'assign to typed hash attrib';
466 lives_ok {$o.h<c> = 3}, 'insertion into typed hash attrib';
467 lives_ok {$o.h.push: (d => 4) }, 'pushing onto typed hash attrib';
468 is_deeply $o.h<a b c d>, (1, 2, 3, 4), '... all of them worked';
469
470 dies_ok {$o.h = { :a<b> } }, 'Type enforced (hash, assignment)';
471 dies_ok {$o.h<a> = 'b' }, 'Type enforced (hash, insertion)';
472 dies_ok {$o.h.push: (g => 'f') }, 'Type enforced (hash, push)';
473 dies_ok {$o.h<blubb><bla> = 3 }, 'No autovivification (typed hash)';
474 is_deeply $o.h<a b c d>, (1, 2, 3, 4), 'hash still unchanged';
475 }
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for a…
jnthn authored May 20, 2009
476
477 # attribute initialization based upon other attributes
478 {
479 class AttrInitTest {
480 has $.a = 1;
481 has $.b = 2;
482 has $.c = $.a + $.b;
483 }
484 is AttrInitTest.new.c, 3, 'Can initialize one attribute based on another (1)';
485 is AttrInitTest.new(a => 2).c, 4, 'Can initialize one attribute based on another (2)';
486 is AttrInitTest.new(c => 9).c, 9, 'Can initialize one attribute based on another (3)';
487 }
488
e9a791f [t/spec] Tests for attributes with the & sigil.
jnthn authored May 20, 2009
489 # attributes with & sigil
490 {
491 class CodeAttr1 { has &!m = sub { "ok" }; method f { &!m() } }
492 is CodeAttr1.new.f, "ok", '&!m = sub { ... } works and an be called';
493
494 class CodeAttr2 { has &.a = { "woot" }; method foo { &!a() } }
495 is CodeAttr2.new.foo, "woot", '&.a = { ... } works and also declares &!a';
496 is CodeAttr2.new.a().(), "woot", '&.a has accessor returning closure';
497
498 class CodeAttr3 { has &!m = method { "OH HAI" }; method f { self.&!m() } }
499 is CodeAttr3.new.f, 'OH HAI', '&!m = method { ... } and self.&!m() work';
500 }
501
b8116e0 [t/spec] more cleanup of instance.t
moritz authored Dec 7, 2008
502 # vim: ft=perl6
Something went wrong with that request. Please try again.