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