Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 638 lines (526 sloc) 18.469 kB
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
1 use v6;
2
3 use Test;
4
bddc607 @moritz RT #74274, sneaking in access to private attributes through the back …
moritz authored
5 plan 137;
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
8aaac76 @diakopter fix a bunch of smartlinks in S12, broken by me and TimToady++
diakopter authored
32 # L<S12/Attribute default values/Pseudo-assignment to an attribute declaration specifies the default>
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
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');
4bc3e06 @colomon Fudge for niecza.
colomon authored
41 #?niecza todo
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
42 dies_ok { $foo.bar = 'blubb' }, 'attributes are ro by default';
43 }
44
45 # L<S12/Attributes/making it an lvalue method>
46
47
48 #?pugs todo 'instance attributes'
49 {
50 class Foo3 { has $.bar is rw; };
51 my $foo = Foo3.new();
52 ok($foo ~~ Foo3, '... our Foo3 instance was created');
53 my $val;
54 lives_ok {
55 $val = $foo.can("bar");
56 }, '.. checking autogenerated accessor existence';
57 ok $val, '... $foo.can("bar") should have returned true';
08f1960 @moritz start to remove .notdef
moritz authored
58 nok($foo.bar().defined, '.. autogenerated accessor works');
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
59 lives_ok {
60 $foo.bar = "baz";
61 }, '.. autogenerated mutator as lvalue works';
62 is($foo.bar, "baz", '.. autogenerated mutator as lvalue set the value correctly');
63 }
64
65 # L<S12/Attributes/Private attributes use an exclamation to indicate that no public accessor is>
66
67
68 {
69 class Foo4 { has $!bar; };
70 my $foo = Foo4.new();
71 ok($foo ~~ Foo4, '... our Foo4 instance was created');
72 #?pugs eval 'todo'
73 ok(!$foo.can("bar"), '.. checking autogenerated accessor existence', );
74 }
75
76
77 {
78 class Foo4a { has $!bar = "baz"; };
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
79 my $foo = Foo4a.new();
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
80 ok($foo ~~ Foo4a, '... our Foo4a instance was created');
81 #?pugs eval 'todo'
82 ok(!$foo.can("bar"), '.. checking autogenerated accessor existence');
83 }
84
85
86 # L<S12/Attributes>
87
88
89 {
90 class Foo5 {
91 has $.tail is rw;
92 has @.legs;
93 has $!brain;
94
95 method set_legs (*@legs) { @.legs = @legs }
96 method inc_brain () { $!brain++ }
97 method get_brain () { $!brain }
98 };
99 my $foo = Foo5.new();
100 ok($foo ~~ Foo5, '... our Foo5 instance was created');
101
102 lives_ok {
103 $foo.tail = "a";
104 }, "setting a public rw attribute";
105 is($foo.tail, "a", "getting a public rw attribute");
106
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored
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
97c23e5 @moritz refudge and correct S12-attributes/instance.t; add tests for attribut…
moritz authored
110 #?rakudo 2 todo 'ro on list attributes'
4bc3e06 @colomon Fudge for niecza.
colomon authored
111 #?niecza 2 todo 'ro on list attributes'
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
112 dies_ok {
113 $foo.legs = (4,5,6);
114 }, "setting a public ro attribute (2)";
115 is($foo.legs.[1], 2, "getting a public ro attribute (2)");
116
b3aadc7 [t/spec] fudge instance.t for rakudo
moritz authored
117 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
118 is($foo.get_brain, 1, "getting a private attribute (1)");
119 lives_ok {
120 $foo.inc_brain();
121 }, "modifiying a private attribute (2)";
122 is($foo.get_brain, 2, "getting a private attribute (2)");
123 }
124
8aaac76 @diakopter fix a bunch of smartlinks in S12, broken by me and TimToady++
diakopter authored
125 # L<S12/Semantics of C<bless>/If you name an attribute as a parameter, that attribute is initialized directly, so>
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
126
127 {
128 class Foo6 {
129 has $.bar is rw;
b02b1ec [t/spec] Unskip/untodo and correct some tests for attributive paramet…
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
97c23e5 @moritz refudge and correct S12-attributes/instance.t; add tests for attribut…
moritz 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 paramet…
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
97c23e5 @moritz refudge and correct S12-attributes/instance.t; add tests for attribut…
moritz authored
153 submethod BUILD (:$!hidden, :$!bar = 10, :$!baz?) {
6755bea @sorear [S12-attributes/instance,S06-signature/code] Unfudge for niecza
sorear authored
154 $!baz = 5;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
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 paramet…
jnthn authored
171 has $.baz is rw;
eb858da [t] clean up oo/attributes/instance.t and move it to spec
moritz authored
172
97c23e5 @moritz refudge and correct S12-attributes/instance.t; add tests for attribut…
moritz authored
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; t…
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 a…
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 a…
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 paramet…
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
97c23e5 @moritz refudge and correct S12-attributes/instance.t; add tests for attribut…
moritz authored
206 submethod BUILD (:$!bar = 5, :$!baz = 10 ) {
207 $!baz = 2 * $!baz;
4095b96 [t/spec]: Some unfudging and re-fudging for rakudo.
pmichaud authored
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) {
6755bea @sorear [S12-attributes/instance,S06-signature/code] Unfudge for niecza
sorear authored
251 $!a = $foo;
252 $!b = $bar;
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
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
97c23e5 @moritz refudge and correct S12-attributes/instance.t; add tests for attribut…
moritz authored
298 #?rakudo skip 'self closure'
4bc3e06 @colomon Fudge for niecza.
colomon authored
299 #?niecza skip 'self closure'
40a5eeb [t/spec] Add test for RT#64654 issue, now that Rakudo seems to handle…
jnthn authored
300 {
301 class ClosureWithself {
302 has $.cl = { self.foo }
303 method foo { 42 }
304 }
305 is ClosureWithself.new.cl().(), 42, 'use of self in closure on RHS of attr init works';
306 }
307
308
e47e77f [t/spec] Tests for .clone with parameters.
jnthn authored
309 # Tests for clone.
310 {
311 class CloneTest { has $.x is rw; has $.y is rw; }
312 my $a = CloneTest.new(x => 1, y => 2);
313 my $b = $a.clone();
314 is $b.x, 1, 'attribute cloned';
315 is $b.y, 2, 'attribute cloned';
316 $b.x = 3;
317 is $b.x, 3, 'changed attribute on clone...';
42626ba @coke niecza (auto)unfudge
coke authored
318 #?niecza todo "original not affected"
e47e77f [t/spec] Tests for .clone with parameters.
jnthn authored
319 is $a.x, 1, '...and original not affected';
320 my $c = $a.clone(x => 42);
321 is $c.x, 42, 'clone with parameters...';
42626ba @coke niecza (auto)unfudge
coke authored
322 #?niecza todo "original not affected"
e47e77f [t/spec] Tests for .clone with parameters.
jnthn authored
323 is $a.x, 1, '...leaves original intact...';
324 is $c.y, 2, '...and copies what we did not change.';
325 }
326
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
327 # tests for *-1 indexing on classes, RT #61766
328 {
329 class ArrayAttribTest {
330 has @.a is rw;
331 method init {
332 @.a = <a b c>;
333 }
334 method m0 { @.a[0] };
335 method m1 { @.a[*-2] };
336 method m2 { @.a[*-1] };
337 }
338 my $o = ArrayAttribTest.new;
339 $o.init;
340 is $o.m0, 'a', '@.a[0] works';
341 is $o.m1, 'b', '@.a[*-2] works';
29b569a [t/spec] unfudge @.a[*-1] tests for rakudo
moritz authored
342 is $o.m2, 'c', '@.a[*-1] works';
f1fac49 [t/spec] test for RT #75266, indexing array attributes with non-Ints
moritz authored
343
344 # RT #75266
345 is ArrayAttribTest.new(a => <x y z>).a[2.0], 'z',
346 'Can index array attributes with non-integers';
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
347 }
348
349 {
350 class AttribWriteTest {
351 has @.a;
352 has %.h;
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
353 method set_array1 {
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
354 @.a = <c b a>;
355 }
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
356 method set_array2 {
357 @!a = <c b a>;
358 }
359 method set_hash1 {
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
360 %.h = (a => 1, b => 2);
361 }
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
362 method set_hash2 {
363 %!h = (a => 1, b => 2);
364 }
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
365 }
366
367 my $x = AttribWriteTest.new;
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
368 # see Larry's reply to
369 # http://groups.google.com/group/perl.perl6.language/browse_thread/thread/2bc6dfd8492b87a4/9189d19e30198ebe?pli=1
370 # on why these should fail.
97c23e5 @moritz refudge and correct S12-attributes/instance.t; add tests for attribut…
moritz authored
371 #?rakudo 2 todo 'ro array/hash with accessor'
4bc3e06 @colomon Fudge for niecza.
colomon authored
372 #?niecza 2 todo 'ro array/hash with accessor'
6e5773d [t/spec] correct some OO tests that I broke before, masak++
moritz authored
373 dies_ok { $x.set_array1 }, 'can not assign to @.array attribute';
374 dies_ok { $x.set_hash1 }, 'can not assign to %.hash attribute';
375 lives_ok { $x.set_array2 }, 'can assign to @!array attribute';
376 lives_ok { $x.set_hash2 }, 'can assign to %!hash attribute';
2e82ab2 [t/spec] tests for RT #61914
moritz authored
377 }
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
378
2e82ab2 [t/spec] tests for RT #61914
moritz authored
379 # test that whitespaces after 'has (' are allowed.
380 # This used to be a Rakudo bug (RT #61914)
4bc3e06 @colomon Fudge for niecza.
colomon authored
381 #?niecza skip 'Unhandled parameter twigil .'
2e82ab2 [t/spec] tests for RT #61914
moritz authored
382 {
383 class AttribWsTest {
384 has ( $.this,
385 $.that,
386 );
387 }
388 my AttribWsTest $o .= new( this => 3, that => 4);
389 is $o.this, 3, 'could use whitespace after "has ("';
390 is $o.that, 4, '.. and a newline within the has() declarator';
17aba30 [t/spec] tests for writing to array and hash attributes
moritz authored
391 }
62eb139 [t/spec] Test for RT#62902.
jnthn authored
392
393 # test typed attributes and === (was Rakudo RT#62902).
394 {
395 class TA1 { }
396 class TA2 {
397 has TA1 $!a;
398 method foo { $!a === TA1 }
399 }
400 ok(TA2.new.foo, '=== works on typed attribute initialized with proto-object');
401 }
402
8cdbdfd [t/spec] merge attribute_of_return_value.t into instance.t
moritz authored
403 # used to be pugs regression
404 {
405 class C_Test { has $.a; }
406 sub f() { C_Test.new(:a(123)) }
407 sub g() { my C_Test $x .= new(:a(123)); $x }
408
409 is(C_Test.new(:a(123)).a, 123, 'C_Test.new().a worked');
410
411 my $o = f();
412 is($o.a, 123, 'my $o = f(); $o.a worked');
413
414 is((try { f().a }), 123, 'f().a worked (so the pugsbug is fixed (part 1))');
415
416 is((try { g().a }), 123, 'g().a worked (so the pugsbug is fixed (part 2))');
417 }
418
1e1543a [t] merge oo/attributes/attribute_list.t into instance.t
moritz authored
419 # was also a pugs regression:
420 # Modification of list attributes created with constructor fails
421
422 {
423 class D_Test {
424 has @.test is rw;
425 method get () { shift @.test }
426 }
427
428 my $test1 = D_Test.new();
429 $test1.test = [1];
430 is($test1.test, [1], "Initialized outside constructor");
431 is($test1.get , 1 , "Get appears to have worked");
432 is($test1.test, [], "Get Worked!");
433
434 my $test2 = D_Test.new( :test([1]) );
435 is($test2.test, [1], "Initialized inside constructor");
436 is($test2.get , 1 , "Get appears to have worked");
437 is($test2.test, [], "Get Worked!");
438 }
439
c077ab0 [t/spec] typed array and hash attributes
moritz authored
440 # test typed attributes
441 # TODO: same checks on private attributes
442 {
443 class TypedAttrib {
444 has Int @.a is rw;
445 has Int %.h is rw;
05c70f6 [t/spec] tests for RT #62838
moritz authored
446 has Int @!pa;
447 has Int %!ph;
448 method pac { @!pa.elems };
449 method phc { %!ph.elems };
c077ab0 [t/spec] typed array and hash attributes
moritz authored
450 }
451 my $o = try { TypedAttrib.new };
452 ok $o.defined, 'created object with typed attributes';
05c70f6 [t/spec] tests for RT #62838
moritz authored
453 is $o.a.elems, 0, 'typed public array attribute is empty';
454 is $o.h.elems, 0, 'typed public hash attribute is empty';
455 is $o.pac, 0, 'typed private array attribute is empty';
456 is $o.phc, 0, 'typed private hash attribute is empty';
71ef855 [t/spec] Some unfuding for Rakudo.
jnthn authored
457
4bc3e06 @colomon Fudge for niecza.
colomon authored
458 #?niecza skip "Unable to resolve method of in class Array"
c077ab0 [t/spec] typed array and hash attributes
moritz authored
459 ok $o.a.of === Int, 'array attribute is typed';
460 lives_ok { $o.a = (2, 3) }, 'Can assign to typed drw-array-attrib';
461 lives_ok { $o.a[2] = 4 }, 'Can insert into typed rw-array-attrib';
462 lives_ok { $o.a.push: 5 }, 'Can push onto typed rw-array-attrib';
463 is $o.a.join('|'), '2|3|4|5',
464 '... all of the above actually worked (not only lived)';
465
4bc3e06 @colomon Fudge for niecza.
colomon authored
466 #?niecza 4 todo 'typed arrays'
5df18e5 @jnthn Fudge updates for S12-attributes/instance.t.
jnthn authored
467 #?rakudo 1 todo 'typed array assignment'
c077ab0 [t/spec] typed array and hash attributes
moritz authored
468 dies_ok { $o.a = <foo bar> }, 'type enforced on array attrib (assignment)';
469 dies_ok { $o.a[2] = $*IN }, 'type enforced on array attrib (item assignment)';
5df18e5 @jnthn Fudge updates for S12-attributes/instance.t.
jnthn authored
470 #?rakudo 1 todo 'typed array push'
c077ab0 [t/spec] typed array and hash attributes
moritz authored
471 dies_ok { $o.a.push: [2, 3]}, 'type enforced on array attrib (push)';
472 dies_ok { $o.a[42]<foo> = 3}, 'no autovivification (typed array)';
473
71ef855 [t/spec] Some unfuding for Rakudo.
jnthn authored
474 #?rakudo todo 'over-eager auto-vivification bugs'
4bc3e06 @colomon Fudge for niecza.
colomon authored
475 #?niecza todo
c077ab0 [t/spec] typed array and hash attributes
moritz authored
476 is $o.a.join('|'), '2|3|4|5',
477 '... all of the above actually did nothing (not just died)';
478
4bc3e06 @colomon Fudge for niecza.
colomon authored
479 #?niecza skip "Unable to resolve method of in class Hash"
c077ab0 [t/spec] typed array and hash attributes
moritz authored
480 ok $o.h.of === Int, 'hash attribute is typed';
481 lives_ok {$o.h = { a => 1, b => 2 } }, 'assign to typed hash attrib';
482 lives_ok {$o.h<c> = 3}, 'insertion into typed hash attrib';
483 lives_ok {$o.h.push: (d => 4) }, 'pushing onto typed hash attrib';
9647099 [t/spec] some unfudges for rakudo
moritz authored
484
c077ab0 [t/spec] typed array and hash attributes
moritz authored
485 is_deeply $o.h<a b c d>, (1, 2, 3, 4), '... all of them worked';
486
0e33d81 @coke niecza fudge
coke authored
487 #?niecza 3 todo
c077ab0 [t/spec] typed array and hash attributes
moritz authored
488 dies_ok {$o.h = { :a<b> } }, 'Type enforced (hash, assignment)';
489 dies_ok {$o.h<a> = 'b' }, 'Type enforced (hash, insertion)';
490 dies_ok {$o.h.push: (g => 'f') }, 'Type enforced (hash, push)';
4bc3e06 @colomon Fudge for niecza.
colomon authored
491 #?niecza 2 todo
c077ab0 [t/spec] typed array and hash attributes
moritz authored
492 dies_ok {$o.h<blubb><bla> = 3 }, 'No autovivification (typed hash)';
5df18e5 @jnthn Fudge updates for S12-attributes/instance.t.
jnthn authored
493 #?rakudo skip 'is_deeply explodes'
c077ab0 [t/spec] typed array and hash attributes
moritz authored
494 is_deeply $o.h<a b c d>, (1, 2, 3, 4), 'hash still unchanged';
495 }
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for a…
jnthn authored
496
497 # attribute initialization based upon other attributes
4bc3e06 @colomon Fudge for niecza.
colomon authored
498 #?niecza skip 'Variable $.a used where no self is available'
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for a…
jnthn authored
499 {
500 class AttrInitTest {
501 has $.a = 1;
502 has $.b = 2;
a685a14 @moritz fix some uses of attributes where they cannot work
moritz authored
503 has $.c = $!a + $!b;
6b40e88 [t/spec] Some unfudging, test review/tweak and additional tests for a…
jnthn authored
504 }
505 is AttrInitTest.new.c, 3, 'Can initialize one attribute based on another (1)';
506 is AttrInitTest.new(a => 2).c, 4, 'Can initialize one attribute based on another (2)';
507 is AttrInitTest.new(c => 9).c, 9, 'Can initialize one attribute based on another (3)';
508 }
509
e9a791f [t/spec] Tests for attributes with the & sigil.
jnthn authored
510 # attributes with & sigil
511 {
512 class CodeAttr1 { has &!m = sub { "ok" }; method f { &!m() } }
513 is CodeAttr1.new.f, "ok", '&!m = sub { ... } works and an be called';
514
515 class CodeAttr2 { has &.a = { "woot" }; method foo { &!a() } }
516 is CodeAttr2.new.foo, "woot", '&.a = { ... } works and also declares &!a';
517 is CodeAttr2.new.a().(), "woot", '&.a has accessor returning closure';
518
519 class CodeAttr3 { has &!m = method { "OH HAI" }; method f { self.&!m() } }
520 is CodeAttr3.new.f, 'OH HAI', '&!m = method { ... } and self.&!m() work';
521 }
522
9c00c3e [t] merge oo/class_inclusion_with_inherited_class.t into spec/
moritz authored
523 {
524 # from t/oo/class_inclusion_with_inherited_class.t
525 # used to be a pugs regression
526
527 role A {
528 method t ( *@a ) {
529 [+] @a;
530 }
531 }
532
533 class B does A {}
534
535 class C does A {
536 has $.s is rw;
537 has B $.b is rw;
538 submethod BUILD {
6755bea @sorear [S12-attributes/instance,S06-signature/code] Unfudge for niecza
sorear authored
539 $!b = B.new;
540 $!s = $!b.t(1, 2, 3);
9c00c3e [t] merge oo/class_inclusion_with_inherited_class.t into spec/
moritz authored
541 }
542 }
543
544 is C.new.s, 6, "Test class include another class which inherited from same role";
545 }
546
58322aa [t/spec] Test for RT #68370
kyle authored
547 # RT #68370
548 {
549 class RT68370 {
550 has $!a;
551 method rt68370 { $!a = 68370 }
552 }
553
554 dies_ok { RT68370.rt68370() },
a89bb47 [t/spec] Unfudge a test Rakudo now passes, and tweak its explanation …
jnthn authored
555 'dies: trying to modify instance attribute when invocant is type object';
58322aa [t/spec] Test for RT #68370
kyle authored
556 }
557
a2787a7 [t/spec] Test for binding to an attribute.
jnthn authored
558 # Binding an attribute (was RT #64850)
559 {
560 class RT64850 {
561 has $.x;
562 method foo { $!x := 42 }
563 }
564 my $a = RT64850.new;
565 $a.foo;
566 is $a.x, 42, 'binding to an attribute works';
567 }
568
5df18e5 @jnthn Fudge updates for S12-attributes/instance.t.
jnthn authored
569 #?rakudo skip 'dubious test - the initializer becomes a submethod here, implying a scope'
9e0e58a [t/spec] test that lexicals escape thunks
moritz authored
570 {
571 class InitializationThunk {
572 has $.foo = my $x = 5;
573 method bar { $x };
574 }
575
576 is InitializationThunk.new.bar, 5, 'a lexical is not tied to a thunk';
577 }
578
a134bd1 [t/spec] test that you can call a method all(), and access attributes…
moritz authored
579 # http://rt.perl.org/rt3/Ticket/Display.html?id=69202
580 {
581 class TestMethodAll {
582 has $.a;
8dc7d4b [t/spec] mark various tests that intentionally declare things that ar…
lwall authored
583 method x(Str $x) {}; #OK not used
a134bd1 [t/spec] test that you can call a method all(), and access attributes…
moritz authored
584 method all() { $!a }
585 }
586 is TestMethodAll.new(a => 5).all, 5, 'Can call a method all()';
587 }
588
f1fac49 [t/spec] test for RT #75266, indexing array attributes with non-Ints
moritz authored
589
d706990 [t/spec] tests for RT #74186, unfudge two similar tests, and switch t…
moritz authored
590 # RT #74186
591 {
592 sub outer { 42 };
593 class AttribLex {
594 sub inner { 23 };
595 has $.outer = outer();
596 has $.inner = inner();
597 }
598 is AttribLex.new.outer, 42, 'Can use outer lexicals in attribut initialization';
599 is AttribLex.new.inner, 23, 'Can use lexicals in attribut initialization';
97c23e5 @moritz refudge and correct S12-attributes/instance.t; add tests for attribut…
moritz authored
600 }
601
602 # RT #85502
603 {
604 class AttribListAssign {
605 has $.a;
606 has $.b;
607 method doit {
608 ($!a, $!b) = <post office>;
609 }
610 }
611 my $x = AttribListAssign.new;
612 $x.doit;
613 is $x.a, 'post', 'list assignment to attributes (1)';
614 isa_ok $x.a, Str, 'list assignment to attributes (type)';
615 is $x.b, 'office', 'list assignment to attributes (2)';
d706990 [t/spec] tests for RT #74186, unfudge two similar tests, and switch t…
moritz authored
616
617 }
618
0dc03f4 @moritz BUILD should not prevent initialization of attributes it did not mention
moritz authored
619 # RT #68498
620 {
621 class Foo { has $.bar = "baz"; submethod BUILD {} }
622 is Foo.new.bar, 'baz',
623 'presence of BUILD does not prevent assignment of default values';
624
625 }
626
15c94ed @moritz RT #108670, accessor name clash
moritz authored
627 # RT #108670
628 eval_dies_ok 'my class AccessorClash { has @.a; has &.a }',
629 'cannot have two attributes with same accessor name';
bddc607 @moritz RT #74274, sneaking in access to private attributes through the back …
moritz authored
630 # RT #74274
631 eval_dies_ok q[class A { has $!a }; my $a = A.new(a => 42);
632 my $method = method { return $!a }; $a.$method()],
633 'cannot sneak in access to private attribute through the backdoor';
15c94ed @moritz RT #108670, accessor name clash
moritz authored
634
e76dd4c @moritz s/done_testing/done/ as per recent S24 changes
moritz authored
635 done();
d706990 [t/spec] tests for RT #74186, unfudge two similar tests, and switch t…
moritz authored
636
b8116e0 [t/spec] more cleanup of instance.t
moritz authored
637 # vim: ft=perl6
Something went wrong with that request. Please try again.