summaryrefslogtreecommitdiff
path: root/t/exceptions/moose-util-metarole.t
blob: 11e30afba21b58743bcda7fa0adcecea24eb6333 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use strict;
use warnings;

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

{
    {
        package Foo;
        use Moose;
    }

    my $foo = Foo->new;
    my $blessed_foo = blessed $foo;
    my %args = ( "for" => $foo );

    my $exception = exception {
        Moose::Util::MetaRole::apply_metaroles( %args );
    };

    my $message = "When using Moose::Util::MetaRole, "
        ."you must pass a Moose class name, role name, metaclass object, or metarole object."
        ." You passed $foo, and we resolved this to a $blessed_foo object.";

    like(
        $exception,
        qr/\Q$message/,
        "$foo is an object, not a class");
        #When using Moose::Util::MetaRole, you must pass a Moose class name, role name, metaclass object, or metarole object. You passed Foo=HASH(0x16adb58), and we resolved this to a Foo object.

    isa_ok(
        $exception,
        'Moose::Exception::InvalidArgPassedToMooseUtilMetaRole',
        "$foo is an object, not a class");

    is(
        $exception->argument,
        $foo,
        "$foo is an object, not a class");
}

{
    my $array_ref = [1, 2, 3];
    my %args = ( "for" => $array_ref );

    my $exception = exception {
        Moose::Util::MetaRole::apply_metaroles( %args );
    };

    my $message = "When using Moose::Util::MetaRole, "
        ."you must pass a Moose class name, role name, metaclass object, or metarole object."
        ." You passed $array_ref, and this did not resolve to a metaclass or metarole."
        ." Maybe you need to call Moose->init_meta to initialize the metaclass first?";

    like(
        $exception,
        qr/\Q$message/,
        "an Array ref is passed to apply_metaroles");
        #When using Moose::Util::MetaRole, you must pass a Moose class name, role name, metaclass object, or metarole object. You passed ARRAY(0x21eb868), and this did not resolve to a metaclass or metarole. Maybe you need to call Moose->init_meta to initialize the metaclass first?

    isa_ok(
        $exception,
        'Moose::Exception::InvalidArgPassedToMooseUtilMetaRole',
        "an Array ref is passed to apply_metaroles");

    is(
        $exception->argument,
        $array_ref,
        "an Array ref is passed to apply_metaroles");
}

{
    my %args = ( "for" => undef );

    my $exception = exception {
        Moose::Util::MetaRole::apply_metaroles( %args );
    };

    my $message = "When using Moose::Util::MetaRole, "
        ."you must pass a Moose class name, role name, metaclass object, or metarole object."
        ." You passed undef, and this did not resolve to a metaclass or metarole."
        ." Maybe you need to call Moose->init_meta to initialize the metaclass first?";

    like(
        $exception,
        qr/\Q$message/,
        "undef passed to apply_metaroles");
        #When using Moose::Util::MetaRole, you must pass a Moose class name, role name, metaclass object, or metarole object. You passed undef, and this did not resolve to a metaclass or metarole. Maybe you need to call Moose->init_meta to initialize the metaclass first?

    isa_ok(
        $exception,
        'Moose::Exception::InvalidArgPassedToMooseUtilMetaRole',
        "undef passed to apply_metaroles");

    is(
        $exception->argument,
        undef,
        "undef passed to apply_metaroles");
}

{
    {
        package Foo::Role;
        use Moose::Role;
    }

    my %args = ('for' => "Foo::Role" );

    my $exception = exception {
        Moose::Util::MetaRole::apply_base_class_roles( %args );
    };

    like(
        $exception,
        qr/\QYou can only apply base class roles to a Moose class, not a role./,
        "Moose::Util::MetaRole::apply_base_class_roles expects a class for 'for'");

    isa_ok(
        $exception,
        'Moose::Exception::CannotApplyBaseClassRolesToRole',
        "Moose::Util::MetaRole::apply_base_class_roles expects a class for 'for'");

    is(
        $exception->role_name,
        'Foo::Role',
        "Moose::Util::MetaRole::apply_base_class_roles expects a class for 'for'");
}

done_testing;