Renamed file so we don't have two files with the same number
[gitmo/Moose.git] / t / 020_attributes / 015_attribute_traits.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use Test::Moose;
9
10
11 {
12     package My::Attribute::Trait;
13     use Moose::Role;
14
15     has 'alias_to' => (is => 'ro', isa => 'Str');
16
17     has foo => ( is => "ro", default => "blah" );
18
19     after 'install_accessors' => sub {
20         my $self = shift;
21         $self->associated_class->add_method(
22             $self->alias_to,
23             $self->get_read_method_ref
24         );
25     };
26 }
27
28 {
29     package My::Class;
30     use Moose;
31
32     has 'bar' => (
33         traits   => [qw/My::Attribute::Trait/],
34         is       => 'ro',
35         isa      => 'Int',
36         alias_to => 'baz',
37     );
38
39     has 'gorch' => (
40         is      => 'ro',
41         isa     => 'Int',
42         default => sub { 10 }
43     );
44 }
45
46 my $c = My::Class->new(bar => 100);
47 isa_ok($c, 'My::Class');
48
49 is($c->bar, 100, '... got the right value for bar');
50 is($c->gorch, 10, '... got the right value for gorch');
51
52 can_ok($c, 'baz');
53 is($c->baz, 100, '... got the right value for baz');
54
55 my $bar_attr = $c->meta->get_attribute('bar');
56 does_ok($bar_attr, 'My::Attribute::Trait');
57 ok($bar_attr->has_applied_traits, '... got the applied traits');
58 is_deeply($bar_attr->applied_traits, [qw/My::Attribute::Trait/], '... got the applied traits');
59 is($bar_attr->foo, "blah", "attr initialized");
60
61 my $gorch_attr = $c->meta->get_attribute('gorch');
62 ok(!$gorch_attr->does('My::Attribute::Trait'), '... gorch doesnt do the trait');
63 ok(!$gorch_attr->has_applied_traits, '... no traits applied');
64 is($gorch_attr->applied_traits, undef, '... no traits applied');
65
66 done_testing;