253a34513d9149e845485502193395454480b8df
[gitmo/Mouse.git] / t / 020_attributes / 017_attribute_traits_n_meta.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 7;
7 use Test::Exception;
8
9 use lib 't/lib';
10 use Test::Mouse;
11
12 {
13     package My::Meta::Attribute::DefaultReadOnly;
14     use Mouse;
15
16     extends 'Mouse::Meta::Attribute';
17
18     around 'new' => sub {
19         my $next = shift;
20         my ($self, $name, %options) = @_;
21         $options{is} = 'ro'
22             unless exists $options{is};
23         $next->($self, $name, %options);
24     };
25 }
26
27 {
28     package My::Attribute::Trait;
29     use Mouse::Role;
30
31     has 'alias_to' => (is => 'ro', isa => 'Str');
32
33     after 'install_accessors' => sub {
34         my $self = shift;
35         $self->associated_class->add_method(
36             $self->alias_to,
37             $self->get_read_method_ref
38         );
39     };
40 }
41
42 {
43     package My::Class;
44     use Mouse;
45
46     has 'bar' => (
47         metaclass => 'My::Meta::Attribute::DefaultReadOnly',
48         traits    => [qw/My::Attribute::Trait/],
49         isa       => 'Int',
50         alias_to  => 'baz',
51     );
52 }
53
54 my $c = My::Class->new(bar => 100);
55 isa_ok($c, 'My::Class');
56
57 is($c->bar, 100, '... got the right value for bar');
58
59 can_ok($c, 'baz');
60 is($c->baz, 100, '... got the right value for baz');
61
62 isa_ok($c->meta->get_attribute('bar'), 'My::Meta::Attribute::DefaultReadOnly');
63 does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait');
64 is($c->meta->get_attribute('bar')->_is_metadata, 'ro', '... got the right metaclass customization');
65
66
67
68