forked from Raku/roast
/
augment-supersede.t
111 lines (90 loc) · 2.44 KB
/
augment-supersede.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
use v6;
use Test;
plan 14;
# L<S12/"Open vs Closed Classes"/"Otherwise you'll get a class redefinition error.">
use MONKEY-TYPING;
{
class Foo {
method a {'called Foo.a'}
}
augment class Foo {
method b {'called Foo.b'}
}
my $o = Foo.new;
is($o.a, 'called Foo.a', 'basic method call works');
is($o.b, 'called Foo.b', 'added method call works');
dies-ok { EVAL('augment class NonExistent { }') },
'augment on non-existent class dies';
}
# RT #74910
{
my class LexFoo { };
augment class LexFoo { method b { 'called LexFoo.b' } };
is LexFoo.b, 'called LexFoo.b', 'can augment lexical class';
}
# RT #76104
{
augment class Hash {
method foo() { self.keys };
}
is { a => 1 }.foo, 'a', 'can augment Hash';
}
# RT #66694
throws-like q[
class MethodClash { method foo() { 3 } };
augment class MethodClash { method foo() { 3 } };
], X::Syntax::Augment::WithoutMonkeyTyping, 'cannot override a method by monkey-typing';
# RT #76600
eval-lives-ok q[
use MONKEY-TYPING;
role Bar { has $.counter; }
class Pub does Bar { has $.saloon; }
augment class Pub { method snug() { } }
], 'augmenting a class which has a role composed works';
#?rakudo skip 'redeclaration of symbol Bar RT #125046'
{
use MONKEY-TYPING;
class Bar {
method c {'called Bar.c'}
}
supersede class Bar {
method d {'called Bar.d'}
}
my $o = Bar.new;
throws-like '$o.c', Exception, 'overridden method is gone completely';
is($o.d, 'called Bar.d', 'new method is present instead');
}
# RT #75432
{
lives-ok {
class A { multi method a() { }};
augment class A { multi method a() { } }
}, 'RT #75432'
}
# RT #71456
# some integers produces from ranges didn't have
# methods that augment added. Weird.
{
augment class Int {
method prime { True };
}
my $primes = 0;
lives-ok {
for 1..5 {
$primes++ if .prime;
}
}, 'integers produced from ranges have augmented methods';
}
# RT #117163
{
try EVAL 'class F { also is F; }';
ok ~$! ~~ / 'cannot inherit from itself' /, "used to crash rakudo";
}
# RT #117165
{
try EVAL 'class ::F { ... }; class F is ::F';
ok ~$! ~~ / 'cannot inherit from itself' /, "used to crash rakudo";
}
eval-lives-ok 'class A { class B {} }; use MONKEY; augment class A { augment class B { } }',
'Augmenting a nested package lives';
# vim: ft=perl6