add test for has with coerce => 0
[gitmo/MooseX-AlwaysCoerce.git] / t / 01-basic.t
CommitLineData
ad1917d7 1#!/usr/bin/env perl
2use strict;
3use warnings;
4
f327aa7a 5use Test::More tests => 5;
ad1917d7 6
7{
8 package MyClass;
9 use Moose;
ad1917d7 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');
44b44091 19
20 class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
f327aa7a 21
22 has quux => (is => 'rw', isa => 'MyType', coerce => 0);
ad1917d7 23}
24
25ok( (my $instance = MyClass->new), 'instance' );
26
27eval { $instance->foo('bar') };
28ok( (!$@), 'attribute coercion ran' );
29
30eval { $instance->bar('baz') };
31ok( (!$@), 'class attribute coercion ran' );
44b44091 32
33eval { $instance->baz('quux') };
34ok( $@, 'class attribute coercion did not run with coerce => 0' );
f327aa7a 35
36undef $@;
37
38eval { $instance->quux('mtfnpy') };
39ok( $@, 'attribute coercion did not run with coerce => 0' );