Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / cmop / immutable_custom_trait.t
CommitLineData
38bf2a25 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Fatal;
6
7use Class::MOP;
8
9{
10
11 package My::Meta;
12
13 use strict;
14 use warnings;
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 ::is( ::exception { __PACKAGE__->meta->make_immutable }, undef, 'can safely make a class immutable when it has a custom metaclass and immutable trait' );
65}
66
67{
68 can_ok( Bar->meta, 'another_method' );
69 is( Bar->meta->another_method, 42, 'another_method returns expected value' );
70 is_deeply(
71 [ Bar->meta->superclasses ], ['Foo'],
72 'Bar->meta->superclasses returns expected value after immutabilization'
73 );
74}
75
76done_testing;