01e97414f30ca91f54262e17f06d0bdf8062c169
[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 tests => 12;
8
9 use Test::Exception;
10 use Test::Mouse;
11
12
13
14 {
15     package My::Attribute::Trait;
16     use Mouse::Role;
17
18     has 'alias_to' => (is => 'ro', isa => 'Str');
19
20     has foo => ( is => "ro", default => "blah" );
21
22     after 'install_accessors' => sub {
23         my $self = shift;
24         my $reader = $self->get_read_method_ref;
25
26         $self->associated_class->add_method(
27             $self->alias_to,
28             $reader,
29         );
30     };
31 }
32
33 {
34     package My::Class;
35     use Mouse;
36
37     has 'bar' => (
38         traits   => [qw/My::Attribute::Trait/],
39         is       => 'ro',
40         isa      => 'Int',
41         alias_to => 'baz',
42     );
43
44     has 'gorch' => (
45         is      => 'ro',
46         isa     => 'Int',
47         default => sub { 10 }
48     );
49 }
50
51 my $c = My::Class->new(bar => 100);
52 isa_ok($c, 'My::Class');
53
54 is($c->bar, 100, '... got the right value for bar');
55 is($c->gorch, 10, '... got the right value for gorch');
56
57 can_ok($c, 'baz');
58 is($c->baz, 100, '... got the right value for baz');
59
60 my $bar_attr = $c->meta->get_attribute('bar');
61
62 does_ok($bar_attr, 'My::Attribute::Trait');
63 ok($bar_attr->has_applied_traits, '... got the applied traits');
64 is_deeply($bar_attr->applied_traits, [qw/My::Attribute::Trait/], '... got the applied traits');
65 is($bar_attr->foo, "blah", "attr initialized");
66
67 my $gorch_attr = $c->meta->get_attribute('gorch');
68 ok(!$gorch_attr->does('My::Attribute::Trait'), '... gorch doesnt do the trait');
69 ok(!$gorch_attr->has_applied_traits, '... no traits applied');
70 is($gorch_attr->applied_traits, undef, '... no traits applied');
71
72
73