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