Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 87 lines (66 sloc) 2.67 kb
297e783 [t] and [t/spec]
moritz authored
1 use v6;
2
3 use Test;
4
5 # L<"http://use.perl.org/~autrijus/journal/25351">
6 # Roles are also classes! They can be instantiated just fine if they are
7 # concrete enough. Basically they mean composable classes or mixin-able
8 # classes. Hence, RoleName.new() instantiates an object that will probably fail
9 # on all stubs.
10
31f28a8 [t/spec] Add a test for RT#75822 and correct another test so we can un-t...
jnthn authored
11 plan 19;
297e783 [t] and [t/spec]
moritz authored
12
13 role SampleRole {
14 method sample_method () { 42 }
15 }
16
17 {
18 my $obj = SampleRole.new;
19 ok $obj.defined, "roles can be instantiated";
20
21 ok $obj ~~ SampleRole, "our instantiated role object smartmatches against our role";
22
23 is $obj.sample_method, 42, "calling a method on our instantiated role object worked";
d40ec46 [t/spec] More tests for punning a role into a class.
jnthn authored
24
25 my $obj2 = SampleRole.new;
26 ok $obj.WHAT === $obj2.WHAT, "Punned role classes have the same .WHAT";
c413af6 [t/spec] Test that .WHAT on an instance of a class punned from a role st...
jnthn authored
27
f85b8bf Fix a bunch of typeobject stringifications.
kboga authored
28 is $obj.WHAT.gist, 'SampleRole()', '.WHAT as a string gives the name of the role';
d40ec46 [t/spec] More tests for punning a role into a class.
jnthn authored
29 }
30
31 role WithAttr {
32 has $.x;
33 has $.y;
34 }
35 {
36 my $obj = WithAttr.new(x => 'abc', y => 123);
37 ok $obj ~~ WithAttr, "our instantiated role object smartmatches against our role";
38 is $obj.x, 'abc', "role attributes initialized in constructor";
39 is $obj.y, 123, "role attributes initialized in constructor";
40 }
41
42 {
8c884c7 @colomon Fudge for niecza.
colomon authored
43 role ParaRole[$x] {
44 method get_x { $x }
45 }
46
d40ec46 [t/spec] More tests for punning a role into a class.
jnthn authored
47 my $obj = ParaRole[42].new;
48 my $obj2 = ParaRole[100].new;
49
50 ok $obj ~~ ParaRole, "instantiated object smartmatches against parameterized role";
51 ok $obj ~~ ParaRole[42], "instantiated object smartmatches against parameterized role (with param)";
52 ok $obj2 ~~ ParaRole, "instantiated object smartmatches against parameterized role";
53 ok $obj2 ~~ ParaRole[100], "instantiated object smartmatches against parameterized role (with param)";
54
55 is $obj.get_x, 42, "instantiated object has method with correct associated role parameter";
56 is $obj2.get_x, 100, "instantiated object has method with correct associated role parameter";
297e783 [t] and [t/spec]
moritz authored
57 }
720a722 [t/spec] Tests for punning of roles into classes used with inheritance.
jnthn authored
58
8c884c7 @colomon Fudge for niecza.
colomon authored
59 {
60 role ParaRole2Args[$x, $y] {
61 method x { $x + $y }
62 }
31f28a8 [t/spec] Add a test for RT#75822 and correct another test so we can un-t...
jnthn authored
63
8c884c7 @colomon Fudge for niecza.
colomon authored
64 is ParaRole2Args[4, 5].new.x, 9, 'instantiating a parametric role with two arguments works';
65 }
31f28a8 [t/spec] Add a test for RT#75822 and correct another test so we can un-t...
jnthn authored
66
720a722 [t/spec] Tests for punning of roles into classes used with inheritance.
jnthn authored
67 # Can also pun a role and inherit from the punned class.
68 {
69 class TestA is SampleRole { }
70 is(TestA.new.sample_method, 42, "can call method from punned class of inherited role");
71
72 class TestB is WithAttr { }
73 my $obj = TestB.new(x => 1, y => 2);
74 is($obj.x, 1, "can access attribute from punned class of inherited role");
75 is($obj.y, 2, "can access attribute from punned class of inherited role");
76 }
b012afc [t/spec] Test for role punning with a method other than .new being punne...
jnthn authored
77
78 # It isn't just .new that works - any method can be punned.
79 {
80 role NotNewTest {
81 method x { 69 }
82 }
83 is(NotNewTest.x, 69, "it's not just .new that causes a pun, but any method");
84 }
820912f [t/spec] more smartlinks; less than 100 remain without smartlinks
moritz authored
85
86 # vim: ft=perl6
Something went wrong with that request. Please try again.