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;
9 use lib 't/lib', 'lib';
15 package My::SimpleTrait;
19 sub simple { return 5 }
25 use Mouse -traits => [ 'My::SimpleTrait' ];
28 can_ok( Foo->meta(), 'simple' );
29 is( Foo->meta()->simple(), 5,
30 'Foo->meta()->simple() returns expected value' );
35 use Mouse -traits => 'My::SimpleTrait';
38 can_ok( Bar->meta(), 'simple' );
39 is( Bar->meta()->simple(), 5,
40 'Foo->meta()->simple() returns expected value' );
43 package My::SimpleTrait2;
47 # This needs to happen at compile time so it happens before we
52 default => 'something',
56 sub simple { return 5 }
62 use Mouse -traits => [ 'My::SimpleTrait2' ];
65 can_ok( Bar->meta(), 'simple' );
66 is( Bar->meta()->simple(), 5,
67 'Bar->meta()->simple() returns expected value' );
68 can_ok( Bar->meta(), 'attr' );
69 is( Bar->meta()->attr(), 'something',
70 'Bar->meta()->attr() returns expected value' );
73 package My::SimpleTrait3;
80 default => 'something',
84 sub simple2 { return 55 }
90 use Mouse -traits => [ 'My::SimpleTrait2', 'My::SimpleTrait3' ];
93 can_ok( Baz->meta(), 'simple' );
94 is( Baz->meta()->simple(), 5,
95 'Baz->meta()->simple() returns expected value' );
96 can_ok( Baz->meta(), 'attr' );
97 is( Baz->meta()->attr(), 'something',
98 'Baz->meta()->attr() returns expected value' );
99 can_ok( Baz->meta(), 'simple2' );
100 is( Baz->meta()->simple2(), 55,
101 'Baz->meta()->simple2() returns expected value' );
102 can_ok( Baz->meta(), 'attr2' );
103 is( Baz->meta()->attr2(), 'something',
104 'Baz->meta()->attr2() returns expected value' );
107 package My::Trait::AlwaysRO;
111 around '_process_new_attribute', '_process_inherited_attribute' =>
114 my ( $self, $name, %args ) = @_;
118 return $self->$orig( $name, %args );
125 use Mouse -traits => [ 'My::Trait::AlwaysRO' ];
133 ok( Quux->meta()->has_attribute('size'),
134 'Quux has size attribute' );
135 ok( ! Quux->meta()->get_attribute('size')->writer(),
136 'size attribute does not have a writer' );
139 package My::Class::Whatever;
145 package Mouse::Meta::Class::Custom::Trait::Whatever;
147 sub register_implementation {
148 return 'My::Class::Whatever';
153 package RanOutOfNames;
155 use Mouse -traits => [ 'Whatever' ];
158 ok( RanOutOfNames->meta()->meta()->has_method('whatever'),
159 'RanOutOfNames->meta() has whatever method' );
164 use Mouse::Role -traits => [ 'My::SimpleTrait' ];
167 can_ok( Role::Foo->meta(), 'simple' );
168 is( Role::Foo->meta()->simple(), 5,
169 'Role::Foo->meta()->simple() returns expected value' );
172 require Mouse::Util::TypeConstraints;
173 dies_ok( sub { Mouse::Util::TypeConstraints->import( -traits => 'My::SimpleTrait' ) },
174 'cannot provide -traits to an exporting module that does not init_meta' );
175 like( $@, qr/does not have an init_meta/,
176 '... and error provides a useful explanation' );
180 package Foo::Subclass;
182 use Mouse -traits => [ 'My::SimpleTrait3' ];
187 can_ok( Foo::Subclass->meta(), 'simple' );
188 is( Foo::Subclass->meta()->simple(), 5,
189 'Foo::Subclass->meta()->simple() returns expected value' );
190 is( Foo::Subclass->meta()->simple2(), 55,
191 'Foo::Subclass->meta()->simple2() returns expected value' );
192 can_ok( Foo::Subclass->meta(), 'attr2' );
193 is( Foo::Subclass->meta()->attr2(), 'something',
194 'Foo::Subclass->meta()->attr2() returns expected value' );
198 package Class::WithAlreadyPresentTrait;
199 use Mouse -traits => 'My::SimpleTrait';
201 has an_attr => ( is => 'ro' );
205 my $instance = Class::WithAlreadyPresentTrait->new( an_attr => 'value' );
206 is( $instance->an_attr, 'value', 'Can get value' );
208 '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 Mouse -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' );
225 'Can create instance and access attributes';