Updated test to properly handle a missing dependency
[gitmo/MooseX-AlwaysCoerce.git] / t / 01-basic.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 7;
6 use Test::Exception;
7
8 {
9     package MyClass;
10     use Moose;
11     use MooseX::AlwaysCoerce;
12     use Moose::Util::TypeConstraints;
13
14     subtype 'MyType', as 'Int';
15     coerce 'MyType', from 'Str', via { length $_ };
16
17     subtype 'Uncoerced', as 'Int';
18
19     has foo => (is => 'rw', isa => 'MyType');
20
21     class_has bar => (is => 'rw', isa => 'MyType');
22
23     class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
24
25     has quux => (is => 'rw', isa => 'MyType', coerce => 0);
26
27     has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
28
29     class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
30 }
31
32 ok( (my $instance = MyClass->new), 'instance' );
33
34 lives_ok { $instance->foo('bar') } 'attribute coercion ran';
35
36 lives_ok { $instance->bar('baz') } 'class attribute coercion ran';
37
38 dies_ok { $instance->baz('quux') }
39     'class attribute coercion did not run with coerce => 0';
40
41 dies_ok { $instance->quux('mtfnpy') }
42     'attribute coercion did not run with coerce => 0';
43
44 lives_ok { $instance->uncoerced_attr(10) }
45     'set attribute having type with no coercion and no coerce=0';
46
47 lives_ok { $instance->uncoerced_class_attr(10) }
48     'set class attribute having type with no coercion and no coerce=0';