add failing tests for types without coercion, add Schwern to CONTRIBUTORS
[gitmo/MooseX-AlwaysCoerce.git] / t / 01-basic.t
CommitLineData
ad1917d7 1#!/usr/bin/env perl
2use strict;
3use warnings;
4
6b46d35c 5use Test::More tests => 7;
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
6b46d35c 16 subtype 'Uncoerced', as 'Int';
17
ad1917d7 18 has foo => (is => 'rw', isa => 'MyType');
19
20 class_has bar => (is => 'rw', isa => 'MyType');
44b44091 21
22 class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
f327aa7a 23
24 has quux => (is => 'rw', isa => 'MyType', coerce => 0);
6b46d35c 25
26 has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
27
28 class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
ad1917d7 29}
30
31ok( (my $instance = MyClass->new), 'instance' );
32
33eval { $instance->foo('bar') };
e11e714d 34is $@, "", 'attribute coercion ran';
ad1917d7 35
36eval { $instance->bar('baz') };
e11e714d 37is $@, "", 'class attribute coercion ran';
44b44091 38
39eval { $instance->baz('quux') };
40ok( $@, 'class attribute coercion did not run with coerce => 0' );
f327aa7a 41
42undef $@;
43
44eval { $instance->quux('mtfnpy') };
45ok( $@, 'attribute coercion did not run with coerce => 0' );
6b46d35c 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';