e15c758d0c9df3598cf36596dcc9e4fd870bdd85
[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 => 4;
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
23 ok( (my $instance = MyClass->new), 'instance' );
24
25 eval { $instance->foo('bar') };
26 ok( (!$@), 'attribute coercion ran' );
27
28 eval { $instance->bar('baz') };
29 ok( (!$@), 'class attribute coercion ran' );
30
31 eval { $instance->baz('quux') };
32 ok( $@, 'class attribute coercion did not run with coerce => 0' );