12 package My::Attribute::Trait;
15 has 'alias_to' => (is => 'ro', isa => 'Str');
17 has foo => ( is => "ro", default => "blah" );
19 after 'install_accessors' => sub {
21 $self->associated_class->add_method(
23 $self->get_read_method_ref
27 package Moose::Meta::Attribute::Custom::Trait::Aliased;
28 sub register_implementation { 'My::Attribute::Trait' }
32 package My::Other::Attribute::Trait;
39 has the_other_attr => ( isa => "Str", is => "rw", default => "oink" );
41 after 'install_accessors' => sub {
43 $self->associated_class->add_method(
49 package Moose::Meta::Attribute::Custom::Trait::Other;
50 sub register_implementation { 'My::Other::Attribute::Trait' }
58 traits => [qw/Aliased/],
66 package My::Derived::Class;
72 traits => [qw/Other/],
76 my $c = My::Class->new(bar => 100);
77 isa_ok($c, 'My::Class');
79 is($c->bar, 100, '... got the right value for bar');
82 is($c->baz, 100, '... got the right value for baz');
84 my $bar_attr = $c->meta->get_attribute('bar');
85 does_ok($bar_attr, 'My::Attribute::Trait');
86 is($bar_attr->foo, "blah", "attr initialized");
88 ok(!$bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
89 ok($bar_attr->does('Aliased'), "attr->does uses aliases");
90 ok(!$bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
91 ok(!$bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
93 my $quux = My::Derived::Class->new(bar => 1000);
95 is($quux->bar, 1000, '... got the right value for bar');
98 is($quux->baz, 1000, '... got the right value for baz');
100 my $derived_bar_attr = $quux->meta->get_attribute("bar");
101 does_ok($derived_bar_attr, 'My::Attribute::Trait' );
103 is( $derived_bar_attr->foo, "blah", "attr initialized" );
105 does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' );
107 is($derived_bar_attr->the_other_attr, "oink", "attr initialized" );
109 ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
110 ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases");
111 ok(!$derived_bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
112 ok(!$derived_bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
114 can_ok($quux, 'additional_method');
115 is(eval { $quux->additional_method }, 42, '... got the right value for additional_method');