forked from Raku/roast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anonymous.t
75 lines (61 loc) · 2.1 KB
/
anonymous.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
use v6;
use Test;
# L<S12/Classes/"Perl 6 supports multiple inheritance, anonymous classes">
plan 17;
# Create and instantiate empty class; check .WHAT works and stringifies to
# empty string.
my $c1 = class { };
my $t1 = $c1.new();
ok(defined($t1), 'instantiated the class');
ok($t1 ~~ $c1, 'isa check works');
#?rakudo todo 'Anonymous class stringification (?)'
#?niecza todo
is($c1.WHAT().gist, '()', '.WHAT.gist stringifies to ()');
# Anonymous classes with methods.
my $c2 = class { method foo { 42 }; method bar { 28 } };
my $t2 = $c2.new();
is($t2.foo, 42, 'can call methods on anonymous classes');
is($t2.bar, 28, 'can call methods on anonymous classes');
# Anonymous classes with attributes.
my $c3 = class { has $.x };
my $t3 = $c3.new(x => 42);
is($t3.x, 42, 'anonymous classes can have attributes');
{
my $class;
lives_ok { $class = class { method meth() { return 42 } }} ,
"anonymous class creation";
my $a;
ok ($a = $class.new), "instantiation of anonymous class";
is $a.meth, 42, "calling a method on an instance of an anonymous class (1)";
# And the same w/o using a $class variable:
is (class { method meth() { return 42 } }).new.meth, 42,
"calling a method on an instance of an anonymous class (2)";
}
# Anonymous classes can inherit from named classes.
{
class TestParent { method foo { 42 } }
my $x = class :: is TestParent { }
ok($x ~~ TestParent, 'anonymous class isa TestParent');
is($x.foo, 42, 'inherited method from TestParent');
}
# RT #64888
{
sub rt64888 {
(
class {
method Stringy() { 'RT #64888' }
method Numeric() { 64888 }
}
).new
}
my $i1;
my $i2;
lives_ok { $i1 = rt64888() }, 'can get anonymous class instance once';
lives_ok { $i2 = rt64888() }, 'can get anonymous class instance twice';
#?niecza todo
is ~$i1, 'RT #64888', 'anonymous class stringified works';
is +$i1, 64888, 'anonymous class numified works';
}
# RT #80024
eval_dies_ok q[anon class C { }; C.WHAT; ], 'anon class is actually anon';
# vim: ft=perl6