Only coerce if the accessor's type can coerce.
[gitmo/MooseX-AlwaysCoerce.git] / t / 01-basic.t.orig
CommitLineData
e930b397 1#!/usr/bin/env perl
2use strict;
3use warnings;
4
5use 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
31ok( (my $instance = MyClass->new), 'instance' );
32
33eval { $instance->foo('bar') };
34ok( (!$@), 'attribute coercion ran' );
35
36eval { $instance->bar('baz') };
37ok( (!$@), 'class attribute coercion ran' );
38
39eval { $instance->baz('quux') };
40ok( $@, 'class attribute coercion did not run with coerce => 0' );
41
42undef $@;
43
44eval { $instance->quux('mtfnpy') };
45ok( $@, 'attribute coercion did not run with coerce => 0' );
46
47eval { $instance->uncoerced_attr(10) };
48is $@, "", 'set attribute having type with no coercion and no coerce=0';
49
50eval { $instance->uncoerced_class_attr(10) };
51is $@, "", 'set class attribute having type with no coercion and no coerce=0';