Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 109 lines (88 sloc) 3.017 kb
33d23c3 [spectest] Some tests for mixing in roles with 'does'.
jnthn authored
1 use v6;
2 use Test;
fd874aa [t/spec] test for RT #69654 - mixing in method with "does" should make t...
moritz authored
3 plan 23;
33d23c3 [spectest] Some tests for mixing in roles with 'does'.
jnthn authored
4
25e6fd9 [t/spec] fix roles smartlinks
moritz authored
5 # L<S14/Run-time Mixins/>
c2f8fec [t] and [t/spec] (two train travels worth of changes):
moritz authored
6
33d23c3 [spectest] Some tests for mixing in roles with 'does'.
jnthn authored
7 role R1 { method test { 42 } }
8 class C1 { }
9
10 my $x = C1.new();
11 $x does R1;
12 is $x.test, 42, 'method from a role can be mixed in';
e3bb48e Tests for calling mixed in methods with .?, .+ and .*.
jnthn authored
13 is $x.?test, 42, '.? form of call works on a mixed-in role';
14 is $x.+test, 42, '.+ form of call works on a mixed-in role';
15 is $x.*test, 42, '.* form of call works on a mixed-in role';
33d23c3 [spectest] Some tests for mixing in roles with 'does'.
jnthn authored
16
17
18 role R2 { method test { 42 } }
19 class C2 { has $.x }
20 my $y = C2.new(x => 100);
21 is $y.x, 100, 'initialization sanity check';
22 $y does R2;
23 is $y.test, 42, 'method from role was mixed in';
24 is $y.x, 100, 'mixing in did not destroy old value';
25
26
27 role R3 { has $.answer is rw }
28 class C3 { has $.x }
720c254 [spectest] A couple more mixin tests, for does (R1, R2) and does Answer(...
jnthn authored
29 $y = C3.new(x => 100);
33d23c3 [spectest] Some tests for mixing in roles with 'does'.
jnthn authored
30 $y does R3;
31 $y.answer = 42;
32 is $y.x, 100, 'mixing in with attributes did not destroy existing ones';
33 is $y.answer, 42, 'mixed in new attributes';
34
35
720c254 [spectest] A couple more mixin tests, for does (R1, R2) and does Answer(...
jnthn authored
36 $y = C3.new(x => 100);
37 $y does (R2, R3);
38 $y.answer = 13;
39 is $y.x, 100, 'multi-role mixin preserved existing values';
40 is $y.answer, 13, 'attribute from multi-role mixing OK';
41 is $y.test, 42, 'method from other role was OK too';
42
43
7767ea6 [t/spec] fudge mixin.t for rakudo
moritz authored
44 #?rakudo skip 'sub form of mixins'
45 {
46 role Answer { has $.answer is rw }
47 my $x = 0;
48 $x does Answer(42);
49 is $x.answer, 42, 'role mix-in with initialization value worked';
50 is $x, 0, 'mixing into Int still makes it function as an Int';
51 }
4612e1b [spectest] A couple more tests for mixins, to cover a bug fixed today in...
jnthn authored
52
7767ea6 [t/spec] fudge mixin.t for rakudo
moritz authored
53 #?rakudo skip 'sub form of mixins'
54 {
55 my $x = 0;
56 role A { has $.a is rw }
57 role B { has $.b is rw }
58 $x does A(1);
59 $x does B(2);
60 is $x.a, 1, 'mixining in two roles one after the other';
61 is $x.b, 2, 'mixining in two roles one after the other';
62 }
d900b6b [t/spec] Test for does at the point of declaration.
jnthn authored
63
f77aead [mixin.t] a better fix is to move the block boundary
lwall authored
64
d900b6b [t/spec] Test for does at the point of declaration.
jnthn authored
65 #?rakudo skip 'mixin at the point of declaration is compile time'
66 {
f77aead [mixin.t] a better fix is to move the block boundary
lwall authored
67 my @array does R1;
68 is @array.test, 42, 'mixing in a role at the point of declaration works';
69
d900b6b [t/spec] Test for does at the point of declaration.
jnthn authored
70 my $x;
71 BEGIN { $x = @array.test }
72 is $x, 42, 'mixing in at point of declaration at compile time';
73 }
720c254 [spectest] A couple more mixin tests, for does (R1, R2) and does Answer(...
jnthn authored
74
25e6fd9 [t/spec] fix roles smartlinks
moritz authored
75 # L<S14/Run-time Mixins/"but only if the role supplies exactly one attribute">
9657e08 [t/spec] test for parameterized mixin
moritz authored
76
7767ea6 [t/spec] fudge mixin.t for rakudo
moritz authored
77 #?rakudo skip 'sub form of mixin'
9657e08 [t/spec] test for parameterized mixin
moritz authored
78 {
79 role R4a {
80 # no attribute here
81 }
82 role R4b {
83 has $.x is rw;
84 }
85 role R4c {
86 has $.x;
87 has $.y;
88 }
89
90 dies_ok { my $x = 4; $x does R4a(3) },
91 '"does role(param)" does not work without attribute';
92 lives_ok { my $x = 4; $x does R4b(3) },
93 '"does role(param)" does work with one attribute';
94 dies_ok { my $x = 4; $x does R4c(3) },
95 '"does role(param)" does not work with two attributes';
96 is ([] does R4b("foo")).x, 'foo',
97 'can mix R4b into an Array, and access the attribute';
98 }
99
fd874aa [t/spec] test for RT #69654 - mixing in method with "does" should make t...
moritz authored
100 # RT #69654
101 {
102 role ProvidesFoo { method foo { } }
103 class NoFoo { };
104 is (NoFoo.new does ProvidesFoo).^methods(:local)>>.name, 'foo',
105 'mixin with "does" lists method during introspection';
106 }
9657e08 [t/spec] test for parameterized mixin
moritz authored
107
33d23c3 [spectest] Some tests for mixing in roles with 'does'.
jnthn authored
108 # vim: syn=perl6
Something went wrong with that request. Please try again.