test under various mutablation scenarios
[gitmo/Moose.git] / t / 300_immutable / 006_immutable_nonmoose_subclass.t
CommitLineData
0aac92c5 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 10;
7use Test::Exception;
8use Scalar::Util 'blessed';
9
10BEGIN {
11 use_ok('Moose');
12 use_ok('Moose::Meta::Role');
13}
14
61c0fc9f 15=pod
16
17This test it kind of odd, it tests
18to see if make_immutable will DWIM
19when pressented with a class that
20inherits from a non-Moose base class.
21
22Since immutable only affects the local
23class, and if it doesnt find a constructor
24it will always create one, it won't
25discover this issue, and it will ignore
26the inherited constructor.
27
28This is not really the desired way, but
29detecting this opens a big can of worms
30which we are not going to deal with just
31yet (eventually yes, but most likely it
32will be when we have MooseX::Compile
33available and working).
34
35In the meantime, just know that when you
36call make_immutable on a class which
37inherits from a non-Moose class, you
38should add (inline_constructor => 0).
39
40Sorry Sartak.
41
42=cut
43
0aac92c5 44{
45 package Grandparent;
46
47 sub new {
48 my $class = shift;
49 my %args = (
50 grandparent => 'gramma',
51 @_,
52 );
53
54 bless \%args => $class;
55 }
56
57 sub grandparent { 1 }
58}
59
60{
61 package Parent;
62 use Moose;
63 extends 'Grandparent';
64
65 around new => sub {
66 my $orig = shift;
67 my $class = shift;
68
69 $class->$orig(
70 parent => 'mama',
71 @_,
72 );
73 };
74
75 sub parent { 1 }
76}
77
78{
79 package Child;
80 use Moose;
81 extends 'Parent';
82
83 sub child { 1 }
84
5a3217de 85 __PACKAGE__->meta->make_immutable;
0aac92c5 86}
87
88is(blessed(Grandparent->new), "Grandparent", "got a Grandparent object out of Grandparent->new");
89is(blessed(Parent->new), "Parent", "got a Parent object out of Parent->new");
90is(blessed(Child->new), "Child", "got a Child object out of Child->new");
91
92is(Child->new->grandparent, 1, "Child responds to grandparent");
93is(Child->new->parent, 1, "Child responds to parent");
94is(Child->new->child, 1, "Child responds to child");
95
61c0fc9f 96is(Child->new->{grandparent}, undef, "didnt create a value, cause immutable overode the constructor");
97is(Child->new->{parent}, undef, "didnt create a value, cause immutable overode the constructor");
98
0aac92c5 99