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