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