Commit | Line | Data |
3bb22459 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
3bb22459 |
7 | use Test::Moose; |
8 | |
7ff56534 |
9 | |
3bb22459 |
10 | { |
11 | package My::Attribute::Trait; |
12 | use Moose::Role; |
3c693def |
13 | |
3bb22459 |
14 | has 'alias_to' => (is => 'ro', isa => 'Str'); |
3c693def |
15 | |
16 | has foo => ( is => "ro", default => "blah" ); |
17 | |
3bb22459 |
18 | after 'install_accessors' => sub { |
19 | my $self = shift; |
20 | $self->associated_class->add_method( |
3c693def |
21 | $self->alias_to, |
3bb22459 |
22 | $self->get_read_method_ref |
23 | ); |
24 | }; |
3c693def |
25 | |
3bb22459 |
26 | package Moose::Meta::Attribute::Custom::Trait::Aliased; |
27 | sub register_implementation { 'My::Attribute::Trait' } |
28 | } |
29 | |
30 | { |
39d37838 |
31 | package My::Other::Attribute::Trait; |
32 | use Moose::Role; |
3c693def |
33 | |
39d37838 |
34 | my $method = sub { |
35 | 42; |
3c693def |
36 | }; |
37 | |
c32c2c61 |
38 | has the_other_attr => ( isa => "Str", is => "rw", default => "oink" ); |
3c693def |
39 | |
39d37838 |
40 | after 'install_accessors' => sub { |
41 | my $self = shift; |
42 | $self->associated_class->add_method( |
3c693def |
43 | 'additional_method', |
39d37838 |
44 | $method |
45 | ); |
46 | }; |
3c693def |
47 | |
39d37838 |
48 | package Moose::Meta::Attribute::Custom::Trait::Other; |
49 | sub register_implementation { 'My::Other::Attribute::Trait' } |
50 | } |
51 | |
52 | { |
3bb22459 |
53 | package My::Class; |
54 | use Moose; |
3c693def |
55 | |
3bb22459 |
56 | has 'bar' => ( |
57 | traits => [qw/Aliased/], |
58 | is => 'ro', |
59 | isa => 'Int', |
60 | alias_to => 'baz', |
61 | ); |
62 | } |
63 | |
3c693def |
64 | { |
39d37838 |
65 | package My::Derived::Class; |
66 | use Moose; |
67 | |
68 | extends 'My::Class'; |
69 | |
70 | has '+bar' => ( |
71 | traits => [qw/Other/], |
72 | ); |
73 | } |
74 | |
3bb22459 |
75 | my $c = My::Class->new(bar => 100); |
76 | isa_ok($c, 'My::Class'); |
77 | |
78 | is($c->bar, 100, '... got the right value for bar'); |
79 | |
39d37838 |
80 | can_ok($c, 'baz') and |
3bb22459 |
81 | is($c->baz, 100, '... got the right value for baz'); |
82 | |
3c693def |
83 | my $bar_attr = $c->meta->get_attribute('bar'); |
84 | does_ok($bar_attr, 'My::Attribute::Trait'); |
85 | is($bar_attr->foo, "blah", "attr initialized"); |
39d37838 |
86 | |
0db4f1d7 |
87 | ok(!$bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity"); |
88 | ok($bar_attr->does('Aliased'), "attr->does uses aliases"); |
89 | ok(!$bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles"); |
90 | ok(!$bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles"); |
91 | |
39d37838 |
92 | my $quux = My::Derived::Class->new(bar => 1000); |
93 | |
94 | is($quux->bar, 1000, '... got the right value for bar'); |
95 | |
96 | can_ok($quux, 'baz'); |
97 | is($quux->baz, 1000, '... got the right value for baz'); |
3c693def |
98 | |
99 | my $derived_bar_attr = $quux->meta->get_attribute("bar"); |
100 | does_ok($derived_bar_attr, 'My::Attribute::Trait' ); |
101 | |
102 | is( $derived_bar_attr->foo, "blah", "attr initialized" ); |
39d37838 |
103 | |
c32c2c61 |
104 | does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' ); |
3c693def |
105 | |
c32c2c61 |
106 | is($derived_bar_attr->the_other_attr, "oink", "attr initialized" ); |
107 | |
0db4f1d7 |
108 | ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity"); |
109 | ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases"); |
110 | ok(!$derived_bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles"); |
111 | ok(!$derived_bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles"); |
112 | |
c32c2c61 |
113 | can_ok($quux, 'additional_method'); |
114 | is(eval { $quux->additional_method }, 42, '... got the right value for additional_method'); |
3c693def |
115 | |
a28e50e4 |
116 | done_testing; |