summaryrefslogtreecommitdiff
path: root/t/exceptions/stringify.t
blob: 7a7f0c43ac2c0b3a8132dc68ab103e5102f71a34 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use strict;
use warnings;

use Test::More;
use Try::Tiny;

{
    my $e;
    {
        package Foo;
        use Moose;
        use Try::Tiny;

        try {
            has '+foo' => ( is => 'ro' );
        }
        catch {
            $e = $_;
        };
    }

    ok( $e, q{got an exception from a bad has '+foo' declaration} );
    like(
        $e->as_string,
        qr/\QCould not find an attribute by the name of 'foo' to inherit from in Foo/,
        'stringification includes the error message'
    );
    like(
        $e->as_string,
        qr/\s+Moose::has/,
        'stringification includes the call to Moose::has'
    );
    unlike(
        $e->as_string,
        qr/Moose::Meta/,
        'stringification does not include internal calls to Moose meta classes'
    );

    try {
        Foo->meta->clone_object( [] );
    }
    catch {
        $e = $_;
    };

    like(
        $e->as_string,
        qr/Class::MOP::Class::clone_object/,
        'exception include first Class::MOP::Class frame'
    );
    unlike(
        $e->as_string,
        qr/Class::MOP::Mixin::_throw_exception/,
        'exception does not include internal calls toClass::MOP::Class meta classes'
    );
}

local $ENV{MOOSE_FULL_EXCEPTION} = 1;
{
    my $e;
    {
        package Bar;
        use Moose;
        use Try::Tiny;

        try {
            has '+foo' => ( is => 'ro' );
        }
        catch {
            $e = $_;
        };
    }

    ok( $e, q{got an exception from a bad has '+foo' declaration} );
    like(
        $e->as_string,
        qr/\QCould not find an attribute by the name of 'foo' to inherit from in Bar/,
        'stringification includes the error message'
    );
    like(
        $e->as_string,
        qr/\s+Moose::has/,
        'stringification includes the call to Moose::has'
    );
    like(
        $e->as_string,
        qr/Moose::Meta/,
        'stringification includes internal calls to Moose meta classes when MOOSE_FULL_EXCEPTION env var is true'
    );


    try {
        Foo->meta->clone_object( [] );
    }
    catch {
        $e = $_;
    };

    like(
        $e->as_string,
        qr/Class::MOP::Class::clone_object/,
        'exception include first Class::MOP::Class frame'
    );
    like(
        $e->as_string,
        qr/Class::MOP::Mixin::_throw_exception/,
        'exception includes internal calls toClass::MOP::Class meta classes when MOOSE_FULL_EXCEPTION env var is true'
    );
}

done_testing;