Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (97 sloc) 4.006 kb
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
1 use v6;
2
3 use Test;
4
6956eb0 [t/spec] Test for RT 66178: "but" with nonexistent role
kyle authored
5 plan *;
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
6
7b86856 [spec] S12-role/basic.t: fixed POD
moritz authored
7 =begin description
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
8
605c16f [t/spec] fix some of the S12/Roles smartlinks
moritz authored
9 Basic role tests from L<S14/Roles>
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
10
7b86856 [spec] S12-role/basic.t: fixed POD
moritz authored
11 =end description
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
12
605c16f [t/spec] fix some of the S12/Roles smartlinks
moritz authored
13 # L<S14/Roles>
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
14 # Basic definition
15 role Foo {}
494a817 [t/spec] Some fudging changes (mostly removal) and corrections to a role...
jnthn authored
16 class Bar does Foo {};
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
17
18 # Smartmatch and .HOW.does and .^does
19 my $bar = Bar.new();
469e0ff [spec] S12-role/basic.t: use defined() to test for objects of empty clas...
moritz authored
20 ok ($bar ~~ Bar), '... smartmatch our $bar to the Bar class';
21 ok ($bar.HOW.does($bar, Foo)), '.HOW.does said our $bar does Foo';
22 ok ($bar.^does(Foo)), '.^does said our $bar does Foo';
23 ok ($bar ~~ Foo), 'smartmatch said our $bar does Foo';
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
24
9c04a3a [t/spec] Tests for does inside package.
jnthn authored
25 # Can also write does inside the class.
26 role Foo2 { method x { 42 } }
27 class Bar2 { does Foo2; }
28 my $bar2 = Bar2.new();
29 ok ($bar2 ~~ Foo2), 'smartmatch works when role is done inside class';
30 is $bar2.x, 42, 'method composed when role is done inside class';
31
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
32 # Mixing a Role into a Mu using imperative C<does>
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
33 my $baz = 3;
469e0ff [spec] S12-role/basic.t: use defined() to test for objects of empty clas...
moritz authored
34 ok defined($baz does Foo), 'mixing in our Foo role into $baz worked';
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
35 #?pugs skip 3 'feature'
469e0ff [spec] S12-role/basic.t: use defined() to test for objects of empty clas...
moritz authored
36 ok $baz.HOW.does($baz, Foo), '.HOW.does said our $baz now does Foo';
37 ok $baz.^does(Foo), '.^does said our $baz now does Foo';
5b8dc6b [t] warning suppression
lwall authored
38 eval_dies_ok q{ $baz ~~ Baz }, 'smartmatch against non-existent type dies';
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
39
605c16f [t/spec] fix some of the S12/Roles smartlinks
moritz authored
40 # L<S14/Roles/but with a role keyword:>
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
41 # Roles may have methods
42 #?pugs skip "todo"
43 {
44 role A { method say_hello(Str $to) { "Hello, $to" } }
494a817 [t/spec] Some fudging changes (mostly removal) and corrections to a role...
jnthn authored
45 my Bar $a .= new();
469e0ff [spec] S12-role/basic.t: use defined() to test for objects of empty clas...
moritz authored
46 ok(defined($a does A), 'mixing A into $a worked');
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
47 is $a.say_hello("Ingo"), "Hello, Ingo",
48 '$a "inherited" the .say_hello method of A';
49 }
50
605c16f [t/spec] fix some of the S12/Roles smartlinks
moritz authored
51 # L<S14/Roles/Roles may have attributes:>
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
52 {
05207e9 [basic.t] default expression must come after traits
lwall authored
53 role B { has $.attr is rw = 42 }
494a817 [t/spec] Some fudging changes (mostly removal) and corrections to a role...
jnthn authored
54 my Bar $b .= new();
55 $b does B;
469e0ff [spec] S12-role/basic.t: use defined() to test for objects of empty clas...
moritz authored
56 ok defined($b), 'mixing B into $b worked';
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
57 is $b.attr, 42, '$b "inherited" the $.attr attribute of B (1)';
58 is ($b.attr = 23), 23, '$b "inherited" the $.attr attribute of B (2)';
59
799a132 [t/spec] fix all but 3 smartlinks
moritz authored
60 # L<S14/Run-time Mixins/"but creates a copy">
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
61 # As usual, ok instead of todo_ok to avoid unexpected succeedings.
494a817 [t/spec] Some fudging changes (mostly removal) and corrections to a role...
jnthn authored
62 my Bar $c .= new(),
8c1e27c [spec] moved oo/roles/basic.t to spec/, heavily reworked and many eval()...
moritz authored
63 ok defined($c), 'creating a Foo worked';
64 ok !($c ~~ B), '$c does not B';
65 ok (my $d = $c but B), 'mixing in a Role via but worked';
66 ok !($c ~~ B), '$c still does not B...';
67 ok $d ~~ B, '...but $d does B';
68 }
469e0ff [spec] S12-role/basic.t: use defined() to test for objects of empty clas...
moritz authored
69
81691fe [t/spec] Tests for using roles as type constraints.
jnthn authored
70 # Using roles as type constraints.
71 role C { }
72 class DoesC does C { }
73 lives_ok { my C $x; }, 'can use role as a type constraint on a variable';
321b8af [t/spec] Fudge S14-roles/basic.t (some very disturbing errors here).
colomon authored
74 #?rakudo todo 'Cannot assign Mu to variable with role constraint -- bug or feature?'
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
75 lives_ok { my C $x = Mu }, 'can assign undefined';
81691fe [t/spec] Tests for using roles as type constraints.
jnthn authored
76 dies_ok { my C $x = 42 }, 'type-check enforced';
77 dies_ok { my C $x; $x = 42 }, 'type-check enforced in future assignments too';
78 lives_ok {my C $x = DoesC.new },'type-check passes for class doing role';
79 lives_ok { my C $x = 42 but C },'type-check passes when role mixed in';
80
81 class HasC {
82 has C $.x is rw;
83 }
84 lives_ok { HasC.new }, 'attributes typed as roles initialized OK';
85 lives_ok { HasC.new.x = DoesC.new },
86 'typed attribute accepts things it should';
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
87 lives_ok { HasC.new.x = Mu }, 'typed attribute accepts things it should';
321b8af [t/spec] Fudge S14-roles/basic.t (some very disturbing errors here).
colomon authored
88 #?rakudo todo "Type attribute accepts anything?"
81691fe [t/spec] Tests for using roles as type constraints.
jnthn authored
89 dies_ok { HasC.new.x = 42 }, 'typed attribute rejects things it should';
90
0093231 [t/spec] moved test from S12-role/ to S14-roles/
masak authored
91 # Checking if role does role
92 role D {
93 }
94
95 ok D ~~ Role, 'a role does the Role type';
96
0a8953b [t/spec] Test for RT #60176
kyle authored
97 # If these two tests are swapped, rakudo gives a stack trace on exit.
83778e9 [t/spec] slight re-fudges for Rakudo
moritz authored
98 #?rakudo skip 'class as role (RT #60176)'
0a8953b [t/spec] Test for RT #60176
kyle authored
99 {
100 eval_lives_ok 'class Plain {}; class DoesClass does Plain {}',
101 'a class is also a role';
102
103 eval_lives_ok 'class DoesInt does Int {}',
104 'can compile "class does Int"';
105 }
106
6956eb0 [t/spec] Test for RT 66178: "but" with nonexistent role
kyle authored
107 eval_dies_ok '0 but RT66178', '"but" with non-existent role dies';
108
965af15 [t/spec] class A does NonExistentRole should mention role name in the er...
moritz authored
109 {
110 my $x = eval 'class Animal does NonExistentRole { }; 1';
111 my $err = "$!";
112 ok !$x, 'a class dies when it does a non-existent role';
113 ok $err ~~ /NonExistentRole/,
114 '... and the error message mentions the role';
115 }
116
6956eb0 [t/spec] Test for RT 66178: "but" with nonexistent role
kyle authored
117 done_testing;
118
469e0ff [spec] S12-role/basic.t: use defined() to test for objects of empty clas...
moritz authored
119 # vim: ft=perl6
Something went wrong with that request. Please try again.