use XSAccessor if available
[gitmo/Role-Tiny.git] / t / role-tiny.t
1 use strictures 1;
2 use Test::More;
3 use Test::Fatal;
4
5 BEGIN {
6   package MyRole;
7
8   use Role::Tiny;
9
10   requires qw(req1 req2);
11
12   around foo => sub { my $orig = shift; join ' ', 'role foo', $orig->(@_) };
13
14   sub bar { 'role bar' }
15
16   sub baz { 'role baz' }
17 }
18
19 BEGIN {
20   package MyClass;
21
22   sub req1 { }
23   sub req2 { }
24   sub foo { 'class foo' }
25   sub baz { 'class baz' }
26
27 }
28
29 BEGIN {
30   package NoMethods;
31
32   package OneMethod;
33
34   sub req1 { }
35 }
36
37 sub try_apply_to {
38   my $to = shift;
39   exception { Role::Tiny->apply_role_to_package('MyRole', $to) }
40 }
41
42 is(try_apply_to('MyClass'), undef, 'role applies cleanly');
43 is(MyClass->foo, 'role foo class foo', 'method modifier');
44 is(MyClass->bar, 'role bar', 'method from role');
45 is(MyClass->baz, 'class baz', 'method from class');
46 ok(MyClass->does('MyRole'), 'class does role');
47 ok(!MyClass->does('Random'), 'class does not do non-role');
48
49 like(try_apply_to('NoMethods'), qr/req1, req2/, 'error for both methods');
50 like(try_apply_to('OneMethod'), qr/req2/, 'error for one method');
51
52 done_testing;