In the case where a child type constraint's parent constraint fails, the exception...
[gitmo/Moose.git] / t / 020_attributes / 016_attribute_traits_registered.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 24;
7 use Test::Exception;
8 use Test::Moose;
9
10 BEGIN {
11     use_ok('Moose');
12 }
13
14 {
15     package My::Attribute::Trait;
16     use Moose::Role;
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
30     package Moose::Meta::Attribute::Custom::Trait::Aliased;
31     sub register_implementation { 'My::Attribute::Trait' }
32 }
33
34 {
35     package My::Other::Attribute::Trait;
36     use Moose::Role;
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
52     package Moose::Meta::Attribute::Custom::Trait::Other;
53     sub register_implementation { 'My::Other::Attribute::Trait' }
54 }
55
56 {
57     package My::Class;
58     use Moose;
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;
70     use Moose;
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");
92 ok($bar_attr->does('Aliased'), "attr->does uses aliases");
93 ok(!$bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
94 ok(!$bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
95
96 my $quux = My::Derived::Class->new(bar => 1000);
97
98 is($quux->bar, 1000, '... got the right value for bar');
99
100 can_ok($quux, 'baz');
101 is($quux->baz, 1000, '... got the right value for baz');
102
103 my $derived_bar_attr = $quux->meta->get_attribute("bar");
104 does_ok($derived_bar_attr, 'My::Attribute::Trait' );
105
106 is( $derived_bar_attr->foo, "blah", "attr initialized" );
107
108 does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' );
109
110 is($derived_bar_attr->the_other_attr, "oink", "attr initialized" );
111
112 ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity");
113 ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases");
114 ok(!$derived_bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles");
115 ok(!$derived_bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles");
116
117 can_ok($quux, 'additional_method');
118 is(eval { $quux->additional_method }, 42, '... got the right value for additional_method');
119