Commit | Line | Data |
73d6ccc2 |
1 | use strict; |
2 | use warnings; |
3 | |
eebc1a2f |
4 | use Test::More tests => 4; |
73d6ccc2 |
5 | use Test::Exception; |
6 | |
7 | use Class::MOP; |
8 | |
9 | { |
10 | |
11 | package My::Meta; |
12 | |
13 | use strict; |
14 | use warnings; |
15 | use metaclass; |
16 | |
17 | use base 'Class::MOP::Class'; |
18 | |
19 | sub initialize { |
20 | shift->SUPER::initialize( |
21 | @_, |
22 | immutable_trait => 'My::Meta::Class::Immutable::Trait', |
23 | ); |
24 | } |
25 | } |
26 | |
27 | { |
28 | package My::Meta::Class::Immutable::Trait; |
29 | |
30 | use MRO::Compat; |
31 | use base 'Class::MOP::Class::Immutable::Trait'; |
32 | |
33 | sub another_method { 42 } |
34 | |
35 | sub superclasses { |
36 | my $orig = shift; |
37 | my $self = shift; |
38 | $self->$orig(@_); |
39 | } |
40 | } |
41 | |
42 | { |
43 | package Foo; |
44 | |
45 | use strict; |
46 | use warnings; |
47 | use metaclass; |
48 | |
49 | __PACKAGE__->meta->add_attribute('foo'); |
50 | |
51 | __PACKAGE__->meta->make_immutable; |
52 | } |
53 | |
54 | { |
55 | package Bar; |
56 | |
57 | use strict; |
58 | use warnings; |
59 | use metaclass 'My::Meta'; |
60 | |
61 | use base 'Foo'; |
62 | |
63 | __PACKAGE__->meta->add_attribute('bar'); |
64 | |
65 | ::lives_ok { __PACKAGE__->meta->make_immutable } |
66 | 'can safely make a class immutable when it has a custom metaclass and immutable trait'; |
67 | } |
68 | |
69 | { |
70 | can_ok( Bar->meta, 'another_method' ); |
71 | is( Bar->meta->another_method, 42, 'another_method returns expected value' ); |
72 | is_deeply( |
73 | [ Bar->meta->superclasses ], ['Foo'], |
74 | 'Bar->meta->superclasses returns expected value after immutabilization' |
75 | ); |
76 | } |