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