summaryrefslogtreecommitdiff
path: root/t/cmop/immutable_w_custom_metaclass.t
blob: c0b722dfbe97b6adcd7bc924cb5e689d66cf469d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use strict;
use warnings;

use Test::More;
use Test::Fatal;
use Scalar::Util;

use Class::MOP;

use lib 't/cmop/lib';

{

    package Foo;

    use strict;
    use warnings;
    use metaclass;

    __PACKAGE__->meta->make_immutable;

    package Bar;

    use strict;
    use warnings;
    use metaclass;

    __PACKAGE__->meta->make_immutable;

    package Baz;

    use strict;
    use warnings;
    use metaclass 'MyMetaClass';

    sub mymetaclass_attributes {
        shift->meta->mymetaclass_attributes;
    }

    ::is( ::exception { Baz->meta->superclasses('Bar') }, undef, '... we survive the metaclass incompatibility test' );
}

{
    my $meta = Baz->meta;
    ok( $meta->is_mutable, '... Baz is mutable' );
    is(
        Scalar::Util::blessed( Foo->meta ),
        Scalar::Util::blessed( Bar->meta ),
        'Foo and Bar immutable metaclasses match'
    );
    is( Scalar::Util::blessed($meta), 'MyMetaClass',
        'Baz->meta blessed as MyMetaClass' );
    ok( Baz->can('mymetaclass_attributes'),
        '... Baz can do method before immutable' );
    ok( $meta->can('mymetaclass_attributes'),
        '... meta can do method before immutable' );
    is( exception { $meta->make_immutable }, undef, "Baz is now immutable" );
    ok( $meta->is_immutable, '... Baz is immutable' );
    isa_ok( $meta, 'MyMetaClass', 'Baz->meta' );
    ok( Baz->can('mymetaclass_attributes'),
        '... Baz can do method after imutable' );
    ok( $meta->can('mymetaclass_attributes'),
        '... meta can do method after immutable' );
    isnt( Scalar::Util::blessed( Baz->meta ),
        Scalar::Util::blessed( Bar->meta ),
        'Baz and Bar immutable metaclasses are different' );
    is( exception { $meta->make_mutable }, undef, "Baz is now mutable" );
    ok( $meta->is_mutable, '... Baz is mutable again' );
}

done_testing;