6 use Test::More tests => 28;
10 package My::SimpleTrait;
14 sub simple { return 5 }
20 use Moose -traits => [ 'My::SimpleTrait' ];
23 can_ok( Foo->meta(), 'simple' );
24 is( Foo->meta()->simple(), 5,
25 'Foo->meta()->simple() returns expected value' );
30 use Moose -traits => 'My::SimpleTrait';
33 can_ok( Bar->meta(), 'simple' );
34 is( Bar->meta()->simple(), 5,
35 'Foo->meta()->simple() returns expected value' );
38 package My::SimpleTrait2;
42 # This needs to happen at compile time so it happens before we
47 default => 'something',
51 sub simple { return 5 }
57 use Moose -traits => [ 'My::SimpleTrait2' ];
60 can_ok( Bar->meta(), 'simple' );
61 is( Bar->meta()->simple(), 5,
62 'Bar->meta()->simple() returns expected value' );
63 can_ok( Bar->meta(), 'attr' );
64 is( Bar->meta()->attr(), 'something',
65 'Bar->meta()->attr() returns expected value' );
68 package My::SimpleTrait3;
75 default => 'something',
79 sub simple2 { return 55 }
85 use Moose -traits => [ 'My::SimpleTrait2', 'My::SimpleTrait3' ];
88 can_ok( Baz->meta(), 'simple' );
89 is( Baz->meta()->simple(), 5,
90 'Baz->meta()->simple() returns expected value' );
91 can_ok( Baz->meta(), 'attr' );
92 is( Baz->meta()->attr(), 'something',
93 'Baz->meta()->attr() returns expected value' );
94 can_ok( Baz->meta(), 'simple2' );
95 is( Baz->meta()->simple2(), 55,
96 'Baz->meta()->simple2() returns expected value' );
97 can_ok( Baz->meta(), 'attr2' );
98 is( Baz->meta()->attr2(), 'something',
99 'Baz->meta()->attr2() returns expected value' );
102 package My::Trait::AlwaysRO;
106 around '_process_new_attribute', '_process_inherited_attribute' =>
109 my ( $self, $name, %args ) = @_;
113 return $self->$orig( $name, %args );
120 use Moose -traits => [ 'My::Trait::AlwaysRO' ];
128 ok( Quux->meta()->has_attribute('size'),
129 'Quux has size attribute' );
130 ok( ! Quux->meta()->get_attribute('size')->writer(),
131 'size attribute does not have a writer' );
134 package My::Class::Whatever;
140 package Moose::Meta::Class::Custom::Trait::Whatever;
142 sub register_implementation {
143 return 'My::Class::Whatever';
148 package RanOutOfNames;
150 use Moose -traits => [ 'Whatever' ];
153 ok( RanOutOfNames->meta()->meta()->has_method('whatever'),
154 'RanOutOfNames->meta() has whatever method' );
159 use Moose::Role -traits => [ 'My::SimpleTrait' ];
162 can_ok( Role::Foo->meta(), 'simple' );
163 is( Role::Foo->meta()->simple(), 5,
164 'Role::Foo->meta()->simple() returns expected value' );
167 require Moose::Util::TypeConstraints;
168 dies_ok( sub { Moose::Util::TypeConstraints->import( -traits => 'My::SimpleTrait' ) },
169 'cannot provide -traits to an exporting module that does not init_meta' );
170 like( $@, qr/does not have an init_meta/,
171 '... and error provides a useful explanation' );
176 skip 'This will blow up until Moose::Meta::Class->_fix_metaclass_incompatibility understands roles', 5;
178 package Foo::Subclass;
180 use Moose -traits => [ 'My::SimpleTrait3' ];
185 can_ok( Foo::Subclass->meta(), 'simple' );
186 is( Foo::Subclass->meta()->simple(), 5,
187 'Foo::Subclass->meta()->simple() returns expected value' );
188 is( Foo::Subclass->meta()->simple2(), 55,
189 'Foo::Subclass->meta()->simple2() returns expected value' );
190 can_ok( Foo::Subclass->meta(), 'attr2' );
191 is( Foo::Subclass->meta()->attr2(), 'something',
192 'Foo::Subclass->meta()->attr2() returns expected value' );