b3643e59e0ce31d32cd0c8c9d874b54b9af6aebb
[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 => 5;
6
7 {
8     package MyClass;
9     use Moose;
10     use MooseX::AlwaysCoerce;
11     use Moose::Util::TypeConstraints;
12
13     subtype 'MyType', as 'Int';
14     coerce 'MyType', from 'Str', via { length $_ };
15
16     has foo => (is => 'rw', isa => 'MyType');
17
18     class_has bar => (is => 'rw', isa => 'MyType');
19
20     class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
21
22     has quux => (is => 'rw', isa => 'MyType', coerce => 0);
23 }
24
25 ok( (my $instance = MyClass->new), 'instance' );
26
27 eval { $instance->foo('bar') };
28 ok( (!$@), 'attribute coercion ran' );
29
30 eval { $instance->bar('baz') };
31 ok( (!$@), 'class attribute coercion ran' );
32
33 eval { $instance->baz('quux') };
34 ok( $@, 'class attribute coercion did not run with coerce => 0' );
35
36 undef $@;
37
38 eval { $instance->quux('mtfnpy') };
39 ok( $@, 'attribute coercion did not run with coerce => 0' );