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