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