Failing tests for narrowing a consumed attribute's type constraint
[gitmo/Moose.git] / t / 030_roles / 017_extending_role_attrs.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 10;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');
11 }
12
13 =pod
14
15 This basically just makes sure that using +name 
16 on role attributes works right.
17
18 =cut
19
20 {
21     package Foo::Role;
22     use Moose::Role;
23     
24     has 'bar' => (
25         is      => 'rw',
26         isa     => 'Int',   
27         default => sub { 10 },
28     );
29     
30     package Foo;
31     use Moose;
32     
33     with 'Foo::Role';
34     
35     ::lives_ok {
36         has '+bar' => (default => sub { 100 });
37     } '... extended the attribute successfully';  
38 }
39
40 my $foo = Foo->new;
41 isa_ok($foo, 'Foo');
42
43 is($foo->bar, 100, '... got the extended attribute');
44
45 {
46     package Bar::Role;
47     use Moose::Role;
48
49     has 'foo' => (
50         is      => 'rw',
51         isa     => 'Str | Int',
52     );
53
54     package Bar;
55     use Moose;
56
57     with 'Bar::Role';
58
59     ::lives_ok {
60         has '+foo' => (
61             isa => 'Int',
62         )
63     } "... narrowed the role's type constraint successfully";
64 }
65
66
67 my $bar = Bar->new(foo => 42);
68 isa_ok($bar, 'Bar');
69 is($bar->foo, 42, '... got the extended attribute');
70 $bar->foo(100);
71 is($bar->foo, 100, "... can change the attribute's value to an Int");
72
73 throws_ok { $bar->foo("baz") } qr/^Attribute \(foo\) does not pass the type constraint because: Validation failed for 'Int' failed with value baz at /;
74 is($bar->foo, 100, "... still has the old Int value");
75