Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 593 lines (484 sloc) 16.897 kb
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
1 use v6;
2
3 use Test;
4
2723f3b replace plan *; with number of tests S09-S16
mberends authored
5 plan 131;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
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');
08f1960 @moritz start to remove .notdef
moritz authored
28 nok($foo.bar().defined, '.. autogenerated accessor works');
29 nok($foo.bar.defined, '.. autogenerated accessor works w/out parens');
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
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
36 my $foo = Foo2.new();
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
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';
08f1960 @moritz start to remove .notdef
moritz authored
57 nok($foo.bar().defined, '.. autogenerated accessor works');
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
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');
62 }
63
64 # L<S12/Attributes/Private attributes use an exclamation to indicate that no public accessor is>
65
66
67 {
68 class Foo4 { has $!bar; };
69 my $foo = Foo4.new();
70 ok($foo ~~ Foo4, '... our Foo4 instance was created');
71 #?pugs eval 'todo'
72 ok(!$foo.can("bar"), '.. checking autogenerated accessor existence', );
73 }
74
75
76 {
77 class Foo4a { has $!bar = "baz"; };
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
78 my $foo = Foo4a.new();
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
79 ok($foo ~~ Foo4a, '... our Foo4a instance was created');
80 #?pugs eval 'todo'
81 ok(!$foo.can("bar"), '.. checking autogenerated accessor existence');
82 }
83
84
85 # L<S12/Attributes>
86
87
88 {
89 class Foo5 {
90 has $.tail is rw;
91 has @.legs;
92 has $!brain;
93
94 method set_legs (*@legs) { @.legs = @legs }
95 method inc_brain () { $!brain++ }
96 method get_brain () { $!brain }
97 };
98 my $foo = Foo5.new();
99 ok($foo ~~ Foo5, '... our Foo5 instance was created');
100
101 lives_ok {
102 $foo.tail = "a";
103 }, "setting a public rw attribute";
104 is($foo.tail, "a", "getting a public rw attribute");
105
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored
106 #?rakudo 2 todo 'oo'
107 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
108 is($foo.legs.[1], 2, "getting a public ro attribute (1)");
109
110 dies_ok {
111 $foo.legs = (4,5,6);
112 }, "setting a public ro attribute (2)";
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored
113 #?rakudo todo 'oo'
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
114 is($foo.legs.[1], 2, "getting a public ro attribute (2)");
115
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored
116 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
117 is($foo.get_brain, 1, "getting a private attribute (1)");
118 lives_ok {
119 $foo.inc_brain();
120 }, "modifiying a private attribute (2)";
121 is($foo.get_brain, 2, "getting a private attribute (2)");
122 }
123
124 # L<S12/Construction and Initialization/If you name an attribute as a parameter, that attribute is initialized directly, so>
125
126
127 {
128 class Foo6 {
129 has $.bar is rw;
b02b1ec [t/spec] Unskip/untodo and correct some tests for attributive parameters...
jnthn authored
130 has $.baz is rw;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
131 has $!hidden;
132
b02b1ec [t/spec] Unskip/untodo and correct some tests for attributive parameters...
jnthn authored
133 submethod BUILD($.bar, $.baz, $!hidden) {}
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
134 method get_hidden() { $!hidden }
135 }
136
137 my $foo = Foo6.new(bar => 1, baz => 2, hidden => 3);
138 ok($foo ~~ Foo6, '... our Foo6 instance was created');
139
140 is($foo.bar, 1, "getting a public rw attribute (1)" );
141 is($foo.baz, 2, "getting a public ro attribute (2)" );
142 is($foo.get_hidden, 3, "getting a private ro attribute (3)" );
143 }
144
145 # check that doing something in submethod BUILD works
146
147 {
148 class Foo6a {
149 has $.bar is rw;
b02b1ec [t/spec] Unskip/untodo and correct some tests for attributive parameters...
jnthn authored
150 has $.baz is rw;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
151 has $!hidden;
152
153 submethod BUILD ($!hidden, $.bar = 10, $.baz?) {
154 $.baz = 5;
155 }
156 method get_hidden() { $!hidden }
157 }
158
159 my $foo = Foo6a.new(bar => 1, hidden => 3);
160 ok($foo ~~ Foo6a, '... our Foo6a instance was created');
161
162 is($foo.bar, 1, "getting a public rw attribute (1)" );
163 is($foo.baz, 5, "getting a public rw attribute (2)" );
164 is($foo.get_hidden, 3, "getting a private ro attribute (3)" );
165 }
166
167 # check that assignment in submethod BUILD works with a bare return, too
168 {
169 class Foo6b {
170 has $.bar is rw;
b02b1ec [t/spec] Unskip/untodo and correct some tests for attributive parameters...
jnthn authored
171 has $.baz is rw;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
172
173 submethod BUILD ($.bar = 10, $.baz?) {
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
174 $!baz = 9;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
175 return;
176 }
177 }
178
179 my $foo = Foo6b.new(bar => 7);
180 ok($foo ~~ Foo6b, '... our Foo6b instance was created');
181
182 is($foo.bar, 7, "getting a public rw attribute (1)" );
183 is($foo.baz, 9, "getting a public rw attribute (2)" );
184 }
185
d154816 [t/spec] more smartlink fixing
moritz authored
186 # L<S12/Attributes>
254123a [t/spec] Little more fudge tweaks.
jnthn authored
187 class Foo7e { has $.attr = 42 }
188 is Foo7e.new.attr, 42, "default attribute value (1)";
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
189
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored
190 {
191 my $was_in_supplier = 0;
192 sub forty_two_supplier() { $was_in_supplier++; 42 }
bba87f6 [t/spec] Re-fudgings and other tweaks to S12-attributes/instance.t; toss...
jnthn authored
193 class Foo10e { has $.attr = forty_two_supplier() }
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
194 is eval('Foo10e.new.attr'), 42, "default attribute value (4)";
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for attr...
jnthn authored
195 is $was_in_supplier, 1, "forty_two_supplier() was actually executed";
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
196 eval('Foo10e.new');
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for attr...
jnthn authored
197 is $was_in_supplier, 2, "forty_two_supplier() is executed per instantiation";
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored
198 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
199
200 # check that doing something in submethod BUILD works
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
201 {
202 class Foo7 {
b02b1ec [t/spec] Unskip/untodo and correct some tests for attributive parameters...
jnthn authored
203 has $.bar is rw;
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
204 has $.baz;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
205
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
206 submethod BUILD ($.bar = 5, $baz = 10 ) {
207 $!baz = 2 * $baz;
208 }
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
209 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
210
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
211 my $foo7 = Foo7.new();
212 is( $foo7.bar, 5,
213 'optional attribute should take default value without passed-in value' );
214 is( $foo7.baz, 20,
215 '... optional non-attribute should too' );
216 $foo7 = Foo7.new( :bar(4), :baz(5) );
217 is( $foo7.bar, 4,
218 'optional attribute should take passed-in value over default' );
219 is( $foo7.baz, 10,
220 '... optional non-attribute should too' );
221 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
222
223
224 # check that args are passed to BUILD
225 {
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
226 class Foo8 {
227 has $.a;
228 has $.b;
229
230 submethod BUILD(:$foo, :$bar) {
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
231 $!a = $foo;
232 $!b = $bar;
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
233 }
234 }
235
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
236 my $foo = Foo8.new(foo => 'c', bar => 'd');
237 ok($foo.isa(Foo8), '... our Foo8 instance was created');
238
239 is($foo.a, 'c', 'BUILD received $foo');
240 is($foo.b, 'd', 'BUILD received $bar');
241 }
242
243 # check mixture of positional/named args to BUILD
244
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
245 {
246 class Foo9 {
247 has $.a;
248 has $.b;
249
250 submethod BUILD($foo, :$bar) {
251 $.a = $foo;
252 $.b = $bar;
253 }
254 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
255
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
256 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
257 }
258
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
259 # check $self is passed to BUILD
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
260 {
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
261 class Foo10 {
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
262 has $.a;
263 has $.b;
264 has $.c;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
265
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
266 submethod BUILD($self: :$foo, :$bar) {
267 $!a = $foo;
268 $!b = $bar;
269 $!c = 'y' if $self.isa(Foo10);
270 }
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
271 }
272
273 {
274 my $foo = Foo10.new(foo => 'c', bar => 'd');
275 ok($foo.isa(Foo10), '... our Foo10 instance was created');
276
277 is($foo.a, 'c', 'BUILD received $foo');
278 is($foo.b, 'd', 'BUILD received $bar');
279 is($foo.c, 'y', 'BUILD received $self');
280 }
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
281 }
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
282
e913c98 [t/spec] regression tests for RT #61100
moritz authored
283 {
284 class WHAT_ref { };
285 class WHAT_test {
286 has WHAT_ref $.a;
287 has WHAT_test $.b is rw;
288 }
289 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
290 isa_ok $o.a.WHAT, WHAT_ref, '.WHAT on attributes';
291 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
292 my $r = WHAT_test.new();
293 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
294 isa_ok $r.b.WHAT, WHAT_test, '.WHAT on recursive data structure';
e913c98 [t/spec] regression tests for RT #61100
moritz authored
295
296 }
297
40a5eeb [t/spec] Add test for RT#64654 issue, now that Rakudo seems to handle it...
jnthn authored
298 {
299 class ClosureWithself {
300 has $.cl = { self.foo }
301 method foo { 42 }
302 }
303 is ClosureWithself.new.cl().(), 42, 'use of self in closure on RHS of attr init works';
304 }
305
306
e47e77f [t/spec] Tests for .clone with parameters.
jnthn authored
307 # Tests for clone.
308 {
309 class CloneTest { has $.x is rw; has $.y is rw; }
310 my $a = CloneTest.new(x => 1, y => 2);
311 my $b = $a.clone();
312 is $b.x, 1, 'attribute cloned';
313 is $b.y, 2, 'attribute cloned';
314 $b.x = 3;
315 is $b.x, 3, 'changed attribute on clone...';
316 is $a.x, 1, '...and original not affected';
317 my $c = $a.clone(x => 42);
318 is $c.x, 42, 'clone with parameters...';
319 is $a.x, 1, '...leaves original intact...';
320 is $c.y, 2, '...and copies what we did not change.';
321 }
322
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
323 # tests for *-1 indexing on classes, RT #61766
324 {
325 class ArrayAttribTest {
326 has @.a is rw;
327 method init {
328 @.a = <a b c>;
329 }
330 method m0 { @.a[0] };
331 method m1 { @.a[*-2] };
332 method m2 { @.a[*-1] };
333 }
334 my $o = ArrayAttribTest.new;
335 $o.init;
336 is $o.m0, 'a', '@.a[0] works';
337 is $o.m1, 'b', '@.a[*-2] works';
29b569a [t/spec] unfudge @.a[*-1] tests for rakudo
moritz authored
338 is $o.m2, 'c', '@.a[*-1] works';
f1fac49 [t/spec] test for RT #75266, indexing array attributes with non-Ints
moritz authored
339
340 # RT #75266
341 is ArrayAttribTest.new(a => <x y z>).a[2.0], 'z',
342 'Can index array attributes with non-integers';
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
343 }
344
345 {
346 class AttribWriteTest {
347 has @.a;
348 has %.h;
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
349 method set_array1 {
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
350 @.a = <c b a>;
351 }
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
352 method set_array2 {
353 @!a = <c b a>;
354 }
355 method set_hash1 {
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
356 %.h = (a => 1, b => 2);
357 }
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
358 method set_hash2 {
359 %!h = (a => 1, b => 2);
360 }
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
361 }
362
363 my $x = AttribWriteTest.new;
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
364 # see Larry's reply to
365 # http://groups.google.com/group/perl.perl6.language/browse_thread/thread/2bc6dfd8492b87a4/9189d19e30198ebe?pli=1
366 # on why these should fail.
367 dies_ok { $x.set_array1 }, 'can not assign to @.array attribute';
368 dies_ok { $x.set_hash1 }, 'can not assign to %.hash attribute';
369 lives_ok { $x.set_array2 }, 'can assign to @!array attribute';
370 lives_ok { $x.set_hash2 }, 'can assign to %!hash attribute';
2e82ab2 [t/spec] tests for RT #61914
moritz authored
371 }
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
372
2e82ab2 [t/spec] tests for RT #61914
moritz authored
373 # test that whitespaces after 'has (' are allowed.
374 # This used to be a Rakudo bug (RT #61914)
375 {
376 class AttribWsTest {
377 has ( $.this,
378 $.that,
379 );
380 }
381 my AttribWsTest $o .= new( this => 3, that => 4);
382 is $o.this, 3, 'could use whitespace after "has ("';
383 is $o.that, 4, '.. and a newline within the has() declarator';
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
384 }
62eb139 [t/spec] Test for RT#62902.
jnthn authored
385
386 # test typed attributes and === (was Rakudo RT#62902).
387 {
388 class TA1 { }
389 class TA2 {
390 has TA1 $!a;
391 method foo { $!a === TA1 }
392 }
bba87f6 [t/spec] Re-fudgings and other tweaks to S12-attributes/instance.t; toss...
jnthn authored
393 #?rakudo todo 'Attribute type init'
62eb139 [t/spec] Test for RT#62902.
jnthn authored
394 ok(TA2.new.foo, '=== works on typed attribute initialized with proto-object');
395 }
396
8cdbdfd [t/spec] merge attribute_of_return_value.t into instance.t
moritz authored
397 # used to be pugs regression
398 {
399 class C_Test { has $.a; }
400 sub f() { C_Test.new(:a(123)) }
401 sub g() { my C_Test $x .= new(:a(123)); $x }
402
403 is(C_Test.new(:a(123)).a, 123, 'C_Test.new().a worked');
404
405 my $o = f();
406 is($o.a, 123, 'my $o = f(); $o.a worked');
407
408 is((try { f().a }), 123, 'f().a worked (so the pugsbug is fixed (part 1))');
409
410 is((try { g().a }), 123, 'g().a worked (so the pugsbug is fixed (part 2))');
411 }
412
1e1543a [t] merge oo/attributes/attribute_list.t into instance.t
moritz authored
413 # was also a pugs regression:
414 # Modification of list attributes created with constructor fails
415
416 {
417 class D_Test {
418 has @.test is rw;
419 method get () { shift @.test }
420 }
421
422 my $test1 = D_Test.new();
423 $test1.test = [1];
424 is($test1.test, [1], "Initialized outside constructor");
425 is($test1.get , 1 , "Get appears to have worked");
426 is($test1.test, [], "Get Worked!");
427
428 my $test2 = D_Test.new( :test([1]) );
429 is($test2.test, [1], "Initialized inside constructor");
430 is($test2.get , 1 , "Get appears to have worked");
431 is($test2.test, [], "Get Worked!");
432 }
433
c077ab0 [t/spec] typed array and hash attributes
moritz authored
434 # test typed attributes
435 # TODO: same checks on private attributes
436 {
437 class TypedAttrib {
438 has Int @.a is rw;
439 has Int %.h is rw;
05c70f6 [t/spec] tests for RT #62838
moritz authored
440 has Int @!pa;
441 has Int %!ph;
442 method pac { @!pa.elems };
443 method phc { %!ph.elems };
c077ab0 [t/spec] typed array and hash attributes
moritz authored
444 }
445 my $o = try { TypedAttrib.new };
446 ok $o.defined, 'created object with typed attributes';
05c70f6 [t/spec] tests for RT #62838
moritz authored
447 is $o.a.elems, 0, 'typed public array attribute is empty';
448 is $o.h.elems, 0, 'typed public hash attribute is empty';
449 is $o.pac, 0, 'typed private array attribute is empty';
450 is $o.phc, 0, 'typed private hash attribute is empty';
71ef855 [t/spec] Some unfuding for Rakudo.
jnthn authored
451
bba87f6 [t/spec] Re-fudgings and other tweaks to S12-attributes/instance.t; toss...
jnthn authored
452 #?rakudo todo 'typed arrays'
c077ab0 [t/spec] typed array and hash attributes
moritz authored
453 ok $o.a.of === Int, 'array attribute is typed';
454 lives_ok { $o.a = (2, 3) }, 'Can assign to typed drw-array-attrib';
455 lives_ok { $o.a[2] = 4 }, 'Can insert into typed rw-array-attrib';
456 lives_ok { $o.a.push: 5 }, 'Can push onto typed rw-array-attrib';
457 is $o.a.join('|'), '2|3|4|5',
458 '... all of the above actually worked (not only lived)';
459
d5fdcaf [t/spec]: Fudge and fix some tests after autoviv changes.
pmichaud authored
460 #?rakudo 4 todo 'typed arrays'
c077ab0 [t/spec] typed array and hash attributes
moritz authored
461 dies_ok { $o.a = <foo bar> }, 'type enforced on array attrib (assignment)';
462 dies_ok { $o.a[2] = $*IN }, 'type enforced on array attrib (item assignment)';
463 dies_ok { $o.a.push: [2, 3]}, 'type enforced on array attrib (push)';
464 dies_ok { $o.a[42]<foo> = 3}, 'no autovivification (typed array)';
465
71ef855 [t/spec] Some unfuding for Rakudo.
jnthn authored
466 #?rakudo todo 'over-eager auto-vivification bugs'
c077ab0 [t/spec] typed array and hash attributes
moritz authored
467 is $o.a.join('|'), '2|3|4|5',
468 '... all of the above actually did nothing (not just died)';
469
bba87f6 [t/spec] Re-fudgings and other tweaks to S12-attributes/instance.t; toss...
jnthn authored
470 #?rakudo todo 'typed hash'
c077ab0 [t/spec] typed array and hash attributes
moritz authored
471 ok $o.h.of === Int, 'hash attribute is typed';
472 lives_ok {$o.h = { a => 1, b => 2 } }, 'assign to typed hash attrib';
473 lives_ok {$o.h<c> = 3}, 'insertion into typed hash attrib';
474 lives_ok {$o.h.push: (d => 4) }, 'pushing onto typed hash attrib';
9647099 [t/spec] some unfudges for rakudo
moritz authored
475
c077ab0 [t/spec] typed array and hash attributes
moritz authored
476 is_deeply $o.h<a b c d>, (1, 2, 3, 4), '... all of them worked';
477
d5fdcaf [t/spec]: Fudge and fix some tests after autoviv changes.
pmichaud authored
478 #?rakudo 5 todo 'typed hash'
c077ab0 [t/spec] typed array and hash attributes
moritz authored
479 dies_ok {$o.h = { :a<b> } }, 'Type enforced (hash, assignment)';
480 dies_ok {$o.h<a> = 'b' }, 'Type enforced (hash, insertion)';
481 dies_ok {$o.h.push: (g => 'f') }, 'Type enforced (hash, push)';
482 dies_ok {$o.h<blubb><bla> = 3 }, 'No autovivification (typed hash)';
483 is_deeply $o.h<a b c d>, (1, 2, 3, 4), 'hash still unchanged';
484 }
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for attr...
jnthn authored
485
486 # attribute initialization based upon other attributes
487 {
488 class AttrInitTest {
489 has $.a = 1;
490 has $.b = 2;
491 has $.c = $.a + $.b;
492 }
493 is AttrInitTest.new.c, 3, 'Can initialize one attribute based on another (1)';
494 is AttrInitTest.new(a => 2).c, 4, 'Can initialize one attribute based on another (2)';
495 is AttrInitTest.new(c => 9).c, 9, 'Can initialize one attribute based on another (3)';
496 }
497
e9a791f [t/spec] Tests for attributes with the & sigil.
jnthn authored
498 # attributes with & sigil
499 {
500 class CodeAttr1 { has &!m = sub { "ok" }; method f { &!m() } }
501 is CodeAttr1.new.f, "ok", '&!m = sub { ... } works and an be called';
502
503 class CodeAttr2 { has &.a = { "woot" }; method foo { &!a() } }
504 is CodeAttr2.new.foo, "woot", '&.a = { ... } works and also declares &!a';
505 is CodeAttr2.new.a().(), "woot", '&.a has accessor returning closure';
506
507 class CodeAttr3 { has &!m = method { "OH HAI" }; method f { self.&!m() } }
508 is CodeAttr3.new.f, 'OH HAI', '&!m = method { ... } and self.&!m() work';
509 }
510
9c00c3e [t] merge oo/class_inclusion_with_inherited_class.t into spec/
moritz authored
511 {
512 # from t/oo/class_inclusion_with_inherited_class.t
513 # used to be a pugs regression
514
515 role A {
516 method t ( *@a ) {
517 [+] @a;
518 }
519 }
520
521 class B does A {}
522
523 class C does A {
524 has $.s is rw;
525 has B $.b is rw;
526 submethod BUILD {
527 $.b = B.new;
528 $.s = $.b.t(1, 2, 3);
529 }
530 }
531
532 is C.new.s, 6, "Test class include another class which inherited from same role";
533 }
534
58322aa [t/spec] Test for RT #68370
kyle authored
535 # RT #68370
536 {
537 class RT68370 {
538 has $!a;
539 method rt68370 { $!a = 68370 }
540 }
541
542 dies_ok { RT68370.rt68370() },
a89bb47 [t/spec] Unfudge a test Rakudo now passes, and tweak its explanation a l...
jnthn authored
543 'dies: trying to modify instance attribute when invocant is type object';
58322aa [t/spec] Test for RT #68370
kyle authored
544 }
545
a2787a7 [t/spec] Test for binding to an attribute.
jnthn authored
546 # Binding an attribute (was RT #64850)
547 {
548 class RT64850 {
549 has $.x;
550 method foo { $!x := 42 }
551 }
552 my $a = RT64850.new;
553 $a.foo;
554 is $a.x, 42, 'binding to an attribute works';
555 }
556
9e0e58a [t/spec] test that lexicals escape thunks
moritz authored
557 {
558 class InitializationThunk {
559 has $.foo = my $x = 5;
560 method bar { $x };
561 }
562
563 is InitializationThunk.new.bar, 5, 'a lexical is not tied to a thunk';
564 }
565
a134bd1 [t/spec] test that you can call a method all(), and access attributes in...
moritz authored
566 # http://rt.perl.org/rt3/Ticket/Display.html?id=69202
567 {
568 class TestMethodAll {
569 has $.a;
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
570 method x(Str $x) {}; #OK not used
a134bd1 [t/spec] test that you can call a method all(), and access attributes in...
moritz authored
571 method all() { $!a }
572 }
573 is TestMethodAll.new(a => 5).all, 5, 'Can call a method all()';
574 }
575
f1fac49 [t/spec] test for RT #75266, indexing array attributes with non-Ints
moritz authored
576
d706990 [t/spec] tests for RT #74186, unfudge two similar tests, and switch to p...
moritz authored
577 # RT #74186
578 {
579 sub outer { 42 };
580 class AttribLex {
581 sub inner { 23 };
582 has $.outer = outer();
583 has $.inner = inner();
584 }
585 is AttribLex.new.outer, 42, 'Can use outer lexicals in attribut initialization';
586 is AttribLex.new.inner, 23, 'Can use lexicals in attribut initialization';
587
588 }
589
e76dd4c @moritz s/done_testing/done/ as per recent S24 changes
moritz authored
590 done();
d706990 [t/spec] tests for RT #74186, unfudge two similar tests, and switch to p...
moritz authored
591
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
592 # vim: ft=perl6
Something went wrong with that request. Please try again.