6 use lib 't/lib', 'lib';
8 use Test::More tests => 32;
12 package My::SimpleTrait;
16 sub simple { return 5 }
22 use Moose -traits => [ 'My::SimpleTrait' ];
25 can_ok( Foo->meta(), 'simple' );
26 is( Foo->meta()->simple(), 5,
27 'Foo->meta()->simple() returns expected value' );
32 use Moose -traits => 'My::SimpleTrait';
35 can_ok( Bar->meta(), 'simple' );
36 is( Bar->meta()->simple(), 5,
37 'Foo->meta()->simple() returns expected value' );
40 package My::SimpleTrait2;
44 # This needs to happen at compile time so it happens before we
49 default => 'something',
53 sub simple { return 5 }
59 use Moose -traits => [ 'My::SimpleTrait2' ];
62 can_ok( Bar->meta(), 'simple' );
63 is( Bar->meta()->simple(), 5,
64 'Bar->meta()->simple() returns expected value' );
65 can_ok( Bar->meta(), 'attr' );
66 is( Bar->meta()->attr(), 'something',
67 'Bar->meta()->attr() returns expected value' );
70 package My::SimpleTrait3;
77 default => 'something',
81 sub simple2 { return 55 }
87 use Moose -traits => [ 'My::SimpleTrait2', 'My::SimpleTrait3' ];
90 can_ok( Baz->meta(), 'simple' );
91 is( Baz->meta()->simple(), 5,
92 'Baz->meta()->simple() returns expected value' );
93 can_ok( Baz->meta(), 'attr' );
94 is( Baz->meta()->attr(), 'something',
95 'Baz->meta()->attr() returns expected value' );
96 can_ok( Baz->meta(), 'simple2' );
97 is( Baz->meta()->simple2(), 55,
98 'Baz->meta()->simple2() returns expected value' );
99 can_ok( Baz->meta(), 'attr2' );
100 is( Baz->meta()->attr2(), 'something',
101 'Baz->meta()->attr2() returns expected value' );
104 package My::Trait::AlwaysRO;
108 around '_process_new_attribute', '_process_inherited_attribute' =>
111 my ( $self, $name, %args ) = @_;
115 return $self->$orig( $name, %args );
122 use Moose -traits => [ 'My::Trait::AlwaysRO' ];
130 ok( Quux->meta()->has_attribute('size'),
131 'Quux has size attribute' );
132 ok( ! Quux->meta()->get_attribute('size')->writer(),
133 'size attribute does not have a writer' );
136 package My::Class::Whatever;
142 package Moose::Meta::Class::Custom::Trait::Whatever;
144 sub register_implementation {
145 return 'My::Class::Whatever';
150 package RanOutOfNames;
152 use Moose -traits => [ 'Whatever' ];
155 ok( RanOutOfNames->meta()->meta()->has_method('whatever'),
156 'RanOutOfNames->meta() has whatever method' );
161 use Moose::Role -traits => [ 'My::SimpleTrait' ];
164 can_ok( Role::Foo->meta(), 'simple' );
165 is( Role::Foo->meta()->simple(), 5,
166 'Role::Foo->meta()->simple() returns expected value' );
169 require Moose::Util::TypeConstraints;
170 dies_ok( sub { Moose::Util::TypeConstraints->import( -traits => 'My::SimpleTrait' ) },
171 'cannot provide -traits to an exporting module that does not init_meta' );
172 like( $@, qr/does not have an init_meta/,
173 'and error provides a useful explanation' );
177 package Foo::Subclass;
179 use Moose -traits => [ 'My::SimpleTrait3' ];
184 can_ok( Foo::Subclass->meta(), 'simple' );
185 is( Foo::Subclass->meta()->simple(), 5,
186 'Foo::Subclass->meta()->simple() returns expected value' );
187 is( Foo::Subclass->meta()->simple2(), 55,
188 'Foo::Subclass->meta()->simple2() returns expected value' );
189 can_ok( Foo::Subclass->meta(), 'attr2' );
190 is( Foo::Subclass->meta()->attr2(), 'something',
191 'Foo::Subclass->meta()->attr2() returns expected value' );
195 package Class::WithAlreadyPresentTrait;
196 use Moose -traits => 'My::SimpleTrait';
198 has an_attr => ( is => 'ro' );
202 my $instance = Class::WithAlreadyPresentTrait->new( an_attr => 'value' );
203 is( $instance->an_attr, 'value', 'Can get value' );
205 'Can create instance and access attributes';
209 package Class::WhichLoadsATraitFromDisk;
211 # Any role you like here, the only important bit is that it gets
212 # loaded from disk and has not already been defined.
213 use Moose -traits => 'Role::Parent';
215 has an_attr => ( is => 'ro' );
219 my $instance = Class::WhichLoadsATraitFromDisk->new( an_attr => 'value' );
220 is( $instance->an_attr, 'value', 'Can get value' );
222 'Can create instance and access attributes';