add failing tests for types without coercion, add Schwern to CONTRIBUTORS
[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
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     subtype 'Uncoerced', as 'Int';
17
18     has foo => (is => 'rw', isa => 'MyType');
19
20     class_has bar => (is => 'rw', isa => 'MyType');
21
22     class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
23
24     has quux => (is => 'rw', isa => 'MyType', coerce => 0);
25
26     has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
27
28     class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
29 }
30
31 ok( (my $instance = MyClass->new), 'instance' );
32
33 eval { $instance->foo('bar') };
34 is $@, "", 'attribute coercion ran';
35
36 eval { $instance->bar('baz') };
37 is $@, "", 'class attribute coercion ran';
38
39 eval { $instance->baz('quux') };
40 ok( $@, 'class attribute coercion did not run with coerce => 0' );
41
42 undef $@;
43
44 eval { $instance->quux('mtfnpy') };
45 ok( $@, 'attribute coercion did not run with coerce => 0' );
46
47 eval { $instance->uncoerced_attr(10) };
48 is $@, "", 'set attribute having type with no coercion and no coerce=0';
49
50 eval { $instance->uncoerced_class_attr(10) };
51 is $@, "", 'set class attribute having type with no coercion and no coerce=0';