- Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathanonymous.t
77 lines (63 loc) · 2.31 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
76
77
useTest;
# L<S12/Classes/"Raku supports multiple inheritance, anonymous classes">
plan18;
# 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 (?)'
is($c1.WHAT().gist, '()', '.WHAT.gist stringifies to ()');
# Anonymous classes with methods.
my$c2= class { methodfoo { 42 }; methodbar { 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 { methodmeth() { return42 } }} ,
"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 { methodmeth() { return42 } }).new.meth, 42,
"calling a method on an instance of an anonymous class (2)";
}
# Anonymous classes can inherit from named classes.
{
classTestParent { methodfoo { 42 } }
my$x= class :: is TestParent { }
ok($x~~ TestParent, 'anonymous class isa TestParent');
is($x.foo, 42, 'inherited method from TestParent');
}
# https://github.com/Raku/old-issue-tracker/issues/924
{
subrt64888 {
(
class {
methodStringy() { 'RT #64888' }
methodNumeric() { 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';
is~$i1, 'RT #64888', 'anonymous class stringified works';
is+$i1, 64888, 'anonymous class numified works';
}
# https://github.com/Raku/old-issue-tracker/issues/2282
throws-likeq[anon class C { }; C.WHAT; ], X::Undeclared::Symbols,
'anon class is actually anon';
# https://github.com/Raku/old-issue-tracker/issues/2884
{
ok (class {}) !=== (class {}), 'anonymous classes are distinct';
}
# vim: expandtab shiftwidth=4