potential fixes for role problems.. doesnt solve everything though; will have to...
[gitmo/MooseX-AlwaysCoerce.git] / t / 04-parameterized-roles.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use Test::More;
6
7 BEGIN {
8     if (eval { require MooseX::Role::Parameterized }) {
9         plan tests => 8;
10     } else {
11         plan skip_all => 'This test needs MooseX::Role::Parameterized';
12     }
13 }
14
15 {
16     package Role;
17     use MooseX::Role::Parameterized;
18     use MooseX::AlwaysCoerce;
19     use Moose::Util::TypeConstraints;
20
21     # I do nothing!
22     role {};
23     use Moose::Util::TypeConstraints;
24
25     subtype 'MyType', as 'Int';
26     coerce 'MyType', from 'Str', via { length $_ };
27
28     subtype 'Uncoerced', as 'Int';
29
30     has foo => (is => 'rw', isa => 'MyType');
31
32     class_has bar => (is => 'rw', isa => 'MyType');
33
34     class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
35
36     has quux => (is => 'rw', isa => 'MyType', coerce => 0);
37
38     has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
39
40     class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
41 }
42
43 {
44     package Foo;
45     use Moose;
46     with 'Role';
47 }
48
49 package main;
50 use Test::Exception;
51 use Test::NoWarnings;
52
53 ok( (my $instance = MyClass->new), 'instance' );
54
55 lives_ok { $instance->foo('bar') } 'attribute coercion ran';
56
57 lives_ok { $instance->bar('baz') } 'class attribute coercion ran';
58
59 dies_ok { $instance->baz('quux') }
60     'class attribute coercion did not run with coerce => 0';
61
62 dies_ok { $instance->quux('mtfnpy') }
63     'attribute coercion did not run with coerce => 0';
64
65 lives_ok { $instance->uncoerced_attr(10) }
66     'set attribute having type with no coercion and no coerce=0';
67
68 lives_ok { $instance->uncoerced_class_attr(10) }
69     'set class attribute having type with no coercion and no coerce=0';
70
71