forked from Raku/roast
/
new.t
151 lines (124 loc) · 3.65 KB
/
new.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use v6;
use Test;
plan 26;
class Parent {
has $.x;
}
class Child is Parent {
has $.y;
}
my $o;
lives-ok { $o = Child.new(:x(2), :y(3)) },
'can instantiate class with parent attributes';
is $o.y, 3, '... worked for the child';
is $o.x, 2, '... worked for the parent';
# RT #76490
#?rakudo 3 todo 'parent attributes in initialization RT #76490'
#?niecza 3 todo
lives-ok { $o = Child.new( :y(4), Parent{ :x<5> }) },
'can instantiate class with explicit specification of parent attrib';
is $o.y, 4, '... worked for the child';
is $o.x, 5, '... worked for the parent';
class GrandChild is Child {
}
#?rakudo 6 todo 'parent attributes in initialization RT #76490'
#?niecza 6 todo
lives-ok { $o = GrandChild.new( Child{ :y(4) }, Parent{ :x<5> }) },
'can instantiate class with explicit specification of parent attrib (many parents)';
is $o.y, 4, '... worked for the class Child';
is $o.x, 5, '... worked for the class Parent';
lives-ok { $o = GrandChild.new( Parent{ :x<5> }, Child{ :y(4) }) },
'can instantiate class with explicit specification of parent attrib (many parents, other order)';
is $o.y, 4, '... worked for the class Child (other order)';
is $o.x, 5, '... worked for the class Parent (other order)';
# RT #66204
{
class RT66204 {}
ok ! RT66204.defined, 'NewClass is not .defined';
dies-ok { RT66204 .= new }, 'class asked to build itself refuses';
ok ! RT66204.defined, 'NewClass is still not .defined';
}
# RT 71706
{
class RT71706 {
class RT71706::Artie {}
}
# TODO: check the error message, not just the timing.
#?rakudo todo "nested package handling does't quite get this one right"
#?niecza todo
dies-ok { RT71706::Artie.new }, 'die trying to instantiate missing class';
}
# RT #69676
{
class NewFromMu {
has $.x;
has $.y;
method new($a, $b) {
self.Mu::new(:x($a), :y($b));
}
}
my $x;
lives-ok { $x = NewFromMu.new('j', 'k') }, 'can delegate to self.Mu::new';
is $x.x, 'j', '... got the right attribute (1)';
is $x.y, 'k', '... got the right attribute (2)';
}
#?niecza skip "Cannot call new; none of these signatures match"
{
my class MultiNewFromMu {
has $.x;
multi method new($x) {
self.new(:$x);
}
}
is MultiNewFromMu.new('wirklich!').x, 'wirklich!',
'Mu.new is a multi method';
}
# RT #68756
{
class RT68756 {
has $.a1;
has $.a2;
multi method new(Int $number, Str $color) {
self.bless(:a1($number), :a2($color));
}
}
my RT68756 $foo .= new(2, "geegaw");
is-deeply [ $foo.a1, $foo.a2 ],
[2, "geegaw"],
'multi-constructor class alternate (positional) constructor';
#?niecza emit # fails
my RT68756 $bar .= new(:a1(3), :a2<yoohoo>);
#?niecza skip 'Without previous line, this is a disaster'
is-deeply [ $bar.a1, $bar.a2 ],
[3, "yoohoo"],
'multi-constructor class alternate default named constructor';
}
# RT #68558
{
class RT68558 {
has $.foo;
method new($foo) { nextwith(:$foo) }
}
is RT68558.new('x').foo, 'x', 'Can call nextwith in .new';
}
# RT #100780
#?niecza skip 'dies more thoroughly than okay'
{
dies-ok { X.new }, 'RT #100780'
}
# RT #74300
#?niecza skip 'No candidates for dispatch to new'
{
class RT74300 {
has $.foo;
multi method new($) {}
}
is RT74300.new(:foo<bar>).foo, 'bar', 'multi method($) does not break attribute initialization';
}
# RT #77200
{
my class RT77200 { }
lives-ok { my RT77200 $lex .= new },
"Can call .=new on a variable of a lexical type";
}
# vim: ft=perl6