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