6 use lib 't/lib', 'lib';
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;
172 Moose::Util::TypeConstraints->import(
173 -traits => 'My::SimpleTrait' );
175 qr/does not have an init_meta/,
176 'cannot provide -traits to an exporting module that does not init_meta'
181 package Foo::Subclass;
183 use Moose -traits => [ 'My::SimpleTrait3' ];
188 can_ok( Foo::Subclass->meta(), 'simple' );
189 is( Foo::Subclass->meta()->simple(), 5,
190 'Foo::Subclass->meta()->simple() returns expected value' );
191 is( Foo::Subclass->meta()->simple2(), 55,
192 'Foo::Subclass->meta()->simple2() returns expected value' );
193 can_ok( Foo::Subclass->meta(), 'attr2' );
194 is( Foo::Subclass->meta()->attr2(), 'something',
195 'Foo::Subclass->meta()->attr2() returns expected value' );
199 package Class::WithAlreadyPresentTrait;
200 use Moose -traits => 'My::SimpleTrait';
202 has an_attr => ( is => 'ro' );
206 my $instance = Class::WithAlreadyPresentTrait->new( an_attr => 'value' );
207 is( $instance->an_attr, 'value', 'Can get value' );
208 }, undef, 'Can create instance and access attributes' );
212 package Class::WhichLoadsATraitFromDisk;
214 # Any role you like here, the only important bit is that it gets
215 # loaded from disk and has not already been defined.
216 use Moose -traits => 'Role::Parent';
218 has an_attr => ( is => 'ro' );
222 my $instance = Class::WhichLoadsATraitFromDisk->new( an_attr => 'value' );
223 is( $instance->an_attr, 'value', 'Can get value' );
224 }, undef, 'Can create instance and access attributes' );