summaryrefslogtreecommitdiff
path: root/t/exceptions/class-mop-object.t
blob: b41f93a802c0ec11b1d4c45ab8f4aeaade057230 (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
use strict;
use warnings;

use Test::More;
use Test::Fatal;

{
    my $exception = exception {
        use Moose ();
        # XXX call cmop version of throw_error here instead!
        Moose->throw_error("Hello, I am an exception object");
    };

    like(
        $exception,
        qr/Hello, I am an exception object/,
        "throw_error stringifies to the message");

    isa_ok(
        $exception,
        'Moose::Exception::Legacy',
        "exception");
}

{
    my $exception = exception {
        use Moose ();
        Moose->throw_error("Hello, ", "I am an ", "exception object");
    };

    like(
        $exception,
        qr/Hello, I am an exception object/,
        "throw_error stringifies to the full message");

    isa_ok(
        $exception,
        'Moose::Exception::Legacy',
        "exception");
}

{
    BEGIN
    {
        {
            package FooRole;
            use Moose::Role;

            sub xyz {
                print "In xyz method";
            }
        }

        {
            package FooMetaclass;
            use Moose;
            with 'FooRole';
            extends 'Moose::Meta::Class';

            sub _inline_check_required_attr {
                my $self = shift;
                my ($attr) = @_;

                return unless defined $attr->init_arg;
                return unless $attr->can('is_required') && $attr->is_required;
                return if $attr->has_default || $attr->has_builder;

                return (
                    'if (!exists $params->{\'' . $attr->init_arg . '\'}) {',
                    $self->_inline_throw_error(
                        'Legacy => '.
                        'message        => "An inline error" '
                    ).';',
                    '}',
                );
            }
        }
    }
};

{
    {
        package Foo2;
        use Moose -metaclass => 'FooMetaclass';

        has 'baz' => (
            is       => 'ro',
            isa      => 'Int',
            required => 1,
        );
        __PACKAGE__->meta->make_immutable;
    }

    my $exception = exception {
        my $test1 = Foo2->new;
    };

    like(
        $exception,
        qr/An inline error/,
        "_inline_throw_error stringifies to the message");

    isa_ok(
        $exception,
        'Moose::Exception::Legacy',
        "_inline_throw_error stringifies to the message");
}

done_testing();