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