Skip to content
Newer
Older
100644 93 lines (72 sloc) 2.18 KB
297e783 [t] and [t/spec]
moritz authored
1 use v6;
2
3 use Test;
4
166331d [t/spec] test for RT #76476, monkey-typed method new is not called
moritz authored
5 plan 15;
297e783 [t] and [t/spec]
moritz authored
6
7 # L<S12/"Construction and Initialization">
8
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
9 class OwnConstr {
297e783 [t] and [t/spec]
moritz authored
10 has $.x = 13;
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
11 my $in_own = 0;
12 method own() {
297e783 [t] and [t/spec]
moritz authored
13 $in_own++;
ae4af99 [t/spec] Spec change means bless always needs a candidate now; update…
jnthn authored
14 return self.bless(self.CREATE(), :x(42));
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
15 }
16 method in_own {
17 $in_own;
297e783 [t] and [t/spec]
moritz authored
18 }
19 }
20 ok OwnConstr.new ~~ OwnConstr, "basic class instantiation";
21 is OwnConstr.new.x, 13, "basic attribute access";
22 # As usual, is instead of todo_is to suppress unexpected succeedings
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
23 is OwnConstr.in_own, 0, "own constructor was not called";
f70e129 [t/spec] Test for RT #64116
kyle authored
24
297e783 [t] and [t/spec]
moritz authored
25 ok OwnConstr.own ~~ OwnConstr, "own construction instantiated its class";
26 is OwnConstr.own.x, 42, "attribute was set from our constructor";
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
27 #?rakudo todo 'unknown'
28 is OwnConstr.in_own, 1, "own constructor was actually called";
297e783 [t] and [t/spec]
moritz authored
29
30
31 # L<"http://www.mail-archive.com/perl6-language@perl.org/msg20241.html">
32 # provide constructor for single positional argument
33
34 class Foo {
35 has $.a;
36
f200142 [t/spec] some improvements to construction.t, and fudged for rakudo
moritz authored
37 method new ($self: Str $string) {
f74df28 [t/spec] second attempt to fix constructor test, masak++
moritz authored
38 $self.bless(*, a => $string);
297e783 [t] and [t/spec]
moritz authored
39 }
40 }
41
42
43 ok Foo.new("a string") ~~ Foo, '... our Foo instance was created';
44
ba8e84a [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
45 #?pugs todo 'feature'
46 is Foo.new("a string").a, 'a string', "our own 'new' was called";
297e783 [t] and [t/spec]
moritz authored
47
48
49 # Using ".=" to create an object
50 {
51 class Bar { has $.attr }
52 my Bar $bar .= new(:attr(42));
53 is $bar.attr, 42, "instantiating an object using .= worked (1)";
54 }
55 # Using ".=()" to create an object
56 {
57 class Fooz { has $.x }
0634426 [t/spec] fix small error in construction.t
moritz authored
58 my Fooz $f .= new(:x(1));
297e783 [t] and [t/spec]
moritz authored
59 is $f.x, 1, "instantiating an object using .=() worked";
60 }
61
62 {
63 class Baz { has @.x is rw }
64 my Baz $foo .= new(:x(1,2,3));
65 lives_ok -> { $foo.x[0] = 3 }, "Array initialized in auto-constructor is not unwritable...";
66 is $foo.x[0], 3, "... and keeps its value properly."
67 }
f70e129 [t/spec] Test for RT #64116
kyle authored
68
69 # RT #64116
70 {
71 class RT64116 { has %.env is rw };
72
ea4d235 [t/spec] begin cleansing of eval and todos that should skip
kyle authored
73 my $a = RT64116.CREATE;
f70e129 [t/spec] Test for RT #64116
kyle authored
74
75 lives_ok { $a.env = { foo => "bar" } }, 'assign to attr of .CREATEd class';
76 is $a.env<foo>, 'bar', 'assignment works';
77 }
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
78
166331d [t/spec] test for RT #76476, monkey-typed method new is not called
moritz authored
79 # RT #76476
80 {
81 use MONKEY_TYPING;
82 class MonkeyNew { has $.x is rw };
83 augment class MonkeyNew {
84 method new() {
85 self.bless(*, :x('called'));
86 }
87 };
88 #?rakudo todo 'RT 76476'
89 is MonkeyNew.new().x, 'called', 'monkey-typed .new() method is called';
90 }
91
7f29bc5 [t/spec] Add vim: lines everywhere.
kyle authored
92 # vim: ft=perl6
Something went wrong with that request. Please try again.