8 use Test::More tests => 23;
15 package My::Attribute::Trait;
18 has 'alias_to' => (is => 'ro', isa => 'Str');
20 has foo => ( is => "ro", default => "blah" );
22 after 'install_accessors' => sub {
24 $self->associated_class->add_method(
26 $self->get_read_method_ref
30 package Mouse::Meta::Attribute::Custom::Trait::Aliased;
31 sub register_implementation { 'My::Attribute::Trait' }
35 package My::Other::Attribute::Trait;
42 has the_other_attr => ( isa => "Str", is => "rw", default => "oink" );
44 after 'install_accessors' => sub {
46 $self->associated_class->add_method(
52 package Mouse::Meta::Attribute::Custom::Trait::Other;
53 sub register_implementation { 'My::Other::Attribute::Trait' }
61 traits => [qw/Aliased/],
69 package My::Derived::Class;
75 traits => [qw/Other/],
79 my $c = My::Class->new(bar => 100);
80 isa_ok($c, 'My::Class');
82 is($c->bar, 100, '... got the right value for bar');
85 is($c->baz, 100, '... got the right value for baz');
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");
91 ok(!$bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
93 local $TODO = 'aliased name is not supported';
94 ok($bar_attr->does('Aliased'), "attr->does uses aliases");
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");
99 my $quux = My::Derived::Class->new(bar => 1000);
101 is($quux->bar, 1000, '... got the right value for bar');
103 can_ok($quux, 'baz');
104 is($quux->baz, 1000, '... got the right value for baz');
106 my $derived_bar_attr = $quux->meta->get_attribute("bar");
107 does_ok($derived_bar_attr, 'My::Attribute::Trait' );
109 is( $derived_bar_attr->foo, "blah", "attr initialized" );
111 does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' );
113 is($derived_bar_attr->the_other_attr, "oink", "attr initialized" );
115 ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
117 local $TODO = 'aliased name is not supported';
118 ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases");
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");
123 can_ok($quux, 'additional_method');
124 is(eval { $quux->additional_method }, 42, '... got the right value for additional_method');