summaryrefslogtreecommitdiffstats
path: root/t/51-buggy-constructors.t
blob: bbe51270dee2f30318081b4143b4bc0ec8a2b0a2 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Test::Moose;

{
    package Foo;

    sub new { bless {}, shift }
}

{
    package Foo::Sub;
    use Moose;
    use MooseX::NonMoose;

    extends 'Foo';
}

with_immutable {
    my $foo;
    is(exception { $foo = Foo::Sub->new }, undef,
       "subclassing nonmoose classes with correct constructors works");
    isa_ok($foo, 'Foo');
    isa_ok($foo, 'Foo::Sub');
} 'Foo::Sub';

{
    package BadFoo;

    sub new { bless {} }
}

{
    package BadFoo::Sub;
    use Moose;
    use MooseX::NonMoose;

    extends 'BadFoo';
}

with_immutable {
    my $foo;
    is(exception { $foo = BadFoo::Sub->new }, undef,
       "subclassing nonmoose classes with incorrect constructors works");
    isa_ok($foo, 'BadFoo');
    isa_ok($foo, 'BadFoo::Sub');
} 'BadFoo::Sub';

{
    package BadFoo2;

    sub new { {} }
}

{
    package BadFoo2::Sub;
    use Moose;
    use MooseX::NonMoose;

    extends 'BadFoo2';
}

with_immutable {
    my $foo;
    like(exception { $foo = BadFoo2::Sub->new; },
         qr/\QThe constructor for BadFoo2 did not return a blessed instance/,
         "subclassing nonmoose classes with incorrect constructors dies properly");
} 'BadFoo2::Sub';

{
    package BadFoo3;

    sub new { bless {}, 'Something::Else::Entirely' }
}

{
    package BadFoo3::Sub;
    use Moose;
    use MooseX::NonMoose;

    extends 'BadFoo3';
}

with_immutable {
    my $foo;
    like(exception { $foo = BadFoo3::Sub->new },
         qr/\QThe constructor for BadFoo3 returned an object whose class is not a parent of BadFoo3::Sub/,
         "subclassing nonmoose classes with incorrect constructors dies properly");
} 'BadFoo3::Sub';

done_testing;