find prereqs automatically
[gitmo/MooseX-AlwaysCoerce.git] / t / 03-roles.t
CommitLineData
0d42c8e8 1#!/usr/bin/env perl
2use strict;
3use warnings;
4
4fb8b2bf 5use Test::More tests => 16;
dfab7c8b 6use Test::Fatal;
0d42c8e8 7use Test::NoWarnings;
8
9{
10 package MyRole;
11 use Moose::Role;
12 use MooseX::AlwaysCoerce;
13 use Moose::Util::TypeConstraints;
14
15 subtype 'MyType', as 'Int';
16 coerce 'MyType', from 'Str', via { length $_ };
17
18 subtype 'Uncoerced', as 'Int';
19
20 has foo => (is => 'rw', isa => 'MyType');
21
22 class_has bar => (is => 'rw', isa => 'MyType');
23
24 class_has baz => (is => 'rw', isa => 'MyType', coerce => 0);
25
26 has quux => (is => 'rw', isa => 'MyType', coerce => 0);
27
28 has uncoerced_attr => (is => 'rw', isa => 'Uncoerced');
29
30 class_has uncoerced_class_attr => (is => 'rw', isa => 'Uncoerced');
bb7cca58 31
32 has untyped_attr => (is => 'rw');
33
34 class_has untyped_class_attr => (is => 'rw');
0d42c8e8 35}
36
37{
38 package MyClass;
39 use Moose;
40 with 'MyRole';
41}
42
43ok( (my $instance = MyClass->new), 'instance' );
44
13005566 45{
4fb8b2bf 46 local $TODO = (Moose->VERSION < 1.9900 ? 'waiting on Moose changes for role support' : undef);
13005566 47
dfab7c8b 48 is( exception {
bb7cca58 49 $instance->foo('bar');
dfab7c8b 50 }, undef, 'attribute coercion ran' );
4fb8b2bf 51 is($instance->foo, 3);
13005566 52}
0d42c8e8 53
dfab7c8b 54is( exception {
bb7cca58 55 $instance->bar('baz');
56 is $instance->bar, 3;
dfab7c8b 57}, undef, 'class attribute coercion ran' );
0d42c8e8 58
dfab7c8b 59isnt( exception { $instance->baz('quux') }, undef, 'class attribute coercion did not run with coerce => 0' );
0d42c8e8 60
dfab7c8b 61isnt( exception { $instance->quux('mtfnpy') }, undef, 'attribute coercion did not run with coerce => 0' );
0d42c8e8 62
dfab7c8b 63is( exception {
bb7cca58 64 $instance->uncoerced_attr(10);
65 is $instance->uncoerced_attr(10), 10;
dfab7c8b 66}, undef, 'set attribute having type with no coercion and no coerce=0' );
bb7cca58 67
dfab7c8b 68is( exception {
bb7cca58 69 $instance->uncoerced_class_attr(10);
70 is $instance->uncoerced_class_attr(10), 10;
dfab7c8b 71}, undef, 'set class attribute having type with no coercion and no coerce=0' );
bb7cca58 72
dfab7c8b 73is( exception {
bb7cca58 74 $instance->untyped_attr(10);
75 is $instance->untyped_attr, 10;
dfab7c8b 76}, undef, 'set untyped attribute' );
bb7cca58 77
dfab7c8b 78is( exception {
bb7cca58 79 $instance->untyped_class_attr(10);
80 is $instance->untyped_class_attr, 10;
dfab7c8b 81}, undef, 'set untyped class attribute' );