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