Skip to content

Commit c39ed26

Browse files
committed
Test using the metamodel at runtime more. Expand test 65 and test 72 which tests using the metamodel for inspecting and applying roles at runtime.
1 parent 83e79a3 commit c39ed26

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

t/nqp/65-how.t

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# check subs
44

5-
plan(7);
5+
plan(10);
66

77
class Foo {
88
has $!foo_attr;
@@ -30,3 +30,12 @@ ok($baz.HOW.attributes($baz) == 1,"the right numer of attributes after adding");
3030
ok($baz.HOW.attributes($baz)[0].name == '$!baz_attr',"we can add an attribute");
3131

3232

33+
class Descendant is Bar {
34+
}
35+
36+
my $d := Descendant.new();
37+
# Descendant being in parents seems suspicious
38+
ok(nqp::elems($d.HOW.parents($d)) >= 3); # Descendant, Foo, Bar, NQPMu
39+
ok(nqp::elems($d.HOW.parents($d,:local(1))) == 1,'right number of local parents');
40+
my $local_parents := $d.HOW.parents($d,:local(1));
41+
ok($local_parents[0].HOW.name($local_parents[0]) eq 'Bar','we can get the name of a parent');

t/nqp/72-rolehow.t

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
plan(12);
2+
role Foo {
3+
method additional() {
4+
"hey new method!"
5+
}
6+
}
7+
role BarOverride {
8+
method bar() {
9+
200;
10+
}
11+
}
12+
class Bar {
13+
method bar() {
14+
100;
15+
}
16+
}
17+
18+
my $methods := Foo.HOW.methods(Foo);
19+
ok(nqp::elems($methods) == 1,'Foo has only one method');
20+
my $method := $methods[0];
21+
my $name := $method.name;#nqp::can($method, 'name') ?? $method.name !! nqp::getcodename($method);
22+
23+
my $a := Bar.new();
24+
ok($a.bar == 100);
25+
$a.HOW.mixin($a,BarOverride);
26+
ok($a.HOW.name($a) eq 'Bar+{BarOverride}','the role name is part of the generated class name');
27+
28+
my $parents := $a.HOW.parents($a,:local(1));
29+
ok(nqp::elems($parents) == 1,"the generated class has only one parent" );
30+
ok($parents[0].HOW.name($parents[0] eq 'Bar'),"...and it's the correct one");
31+
32+
my $roles := $a.HOW.roles($a,:local(1));
33+
ok(nqp::elems($roles) == 1,"the generated class does only one role" );
34+
ok($roles[0].HOW.name($roles[0]) eq 'BarOverride',"...and it's the correct one");
35+
36+
ok($a.bar == 200);
37+
38+
my $b := Bar.new();
39+
$b.HOW.mixin($b,Foo);
40+
ok($b.bar == 100, 'we can use non overriden methods');
41+
ok($b.additional eq 'hey new method!','new methods are inserted');
42+
43+
$b.HOW.mixin($b,BarOverride);
44+
ok($b.bar == 200, 'we can apply two roles to one object');
45+
ok($b.HOW.name($b) eq 'Bar+{Foo}+{BarOverride}','both role names are part of the generated class name');

0 commit comments

Comments
 (0)