2c557ca166d3803eff5bcaf42f07d6b98832d392
[gitmo/Mouse.git] / t / 020_attributes / 015_attribute_traits.t
1 #!/usr/bin/perl
2 use lib 't/lib';
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8 BEGIN{
9     if(eval{ require Class::Method::Modifiers::Fast } || eval{ require Class::Method::Modifiers }){
10         plan tests => 12;
11     }
12     else{
13         plan skip_all => 'This test requires Class::Method::Modifiers(::Fast)?';
14     }
15 }
16 use Test::Exception;
17 use Test::Mouse;
18
19
20
21 {
22     package My::Attribute::Trait;
23     use Mouse::Role;
24
25     has 'alias_to' => (is => 'ro', isa => 'Str');
26
27     has foo => ( is => "ro", default => "blah" );
28
29     after 'install_accessors' => sub {
30         my $self = shift;
31         my $reader = $self->get_read_method;
32
33         $self->associated_class->add_method(
34             $self->alias_to,
35             sub { shift->$reader(@_) },
36         );
37     };
38 }
39
40 {
41     package My::Class;
42     use Mouse;
43
44     has 'bar' => (
45         traits   => [qw/My::Attribute::Trait/],
46         is       => 'ro',
47         isa      => 'Int',
48         alias_to => 'baz',
49     );
50
51     has 'gorch' => (
52         is      => 'ro',
53         isa     => 'Int',
54         default => sub { 10 }
55     );
56 }
57
58 my $c = My::Class->new(bar => 100);
59 isa_ok($c, 'My::Class');
60
61 is($c->bar, 100, '... got the right value for bar');
62 is($c->gorch, 10, '... got the right value for gorch');
63
64 can_ok($c, 'baz');
65 is($c->baz, 100, '... got the right value for baz');
66
67 my $bar_attr = $c->meta->get_attribute('bar');
68
69 does_ok($bar_attr, 'My::Attribute::Trait');
70 ok($bar_attr->has_applied_traits, '... got the applied traits');
71 is_deeply($bar_attr->applied_traits, [qw/My::Attribute::Trait/], '... got the applied traits');
72 is($bar_attr->foo, "blah", "attr initialized");
73
74 my $gorch_attr = $c->meta->get_attribute('gorch');
75 ok(!$gorch_attr->does('My::Attribute::Trait'), '... gorch doesnt do the trait');
76 ok(!$gorch_attr->has_applied_traits, '... no traits applied');
77 is($gorch_attr->applied_traits, undef, '... no traits applied');
78
79
80