summaryrefslogtreecommitdiff
path: root/t/exceptions/moose-meta-attribute-native-traits.t
blob: 64ba08525008f99a40867b4525de6a99d5699648 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use strict;
use warnings;

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

use Moose;

{
    my $exception = exception {
        {
            package TestClass;
            use Moose;

            has 'foo' => (
                traits => ['Array'],
                is     => 'ro',
                isa    => 'Int'
            );
        }
    };

    like(
        $exception,
        qr/The type constraint for foo must be a subtype of ArrayRef but it's a Int/,
        "isa is given as Int, but it should be ArrayRef");

    isa_ok(
        $exception,
        'Moose::Exception::WrongTypeConstraintGiven',
        "isa is given as Int, but it should be ArrayRef");

    is(
        $exception->required_type,
        "ArrayRef",
        "isa is given as Int, but it should be ArrayRef");

    is(
        $exception->given_type,
        "Int",
        "isa is given as Int, but it should be ArrayRef");

    is(
        $exception->attribute_name,
        "foo",
        "isa is given as Int, but it should be ArrayRef");
}

{
    my $exception = exception {
        {
            package TestClass2;
            use Moose;

            has 'foo' => (
                traits  => ['Array'],
                is      => 'ro',
                isa     => 'ArrayRef',
                handles => 'bar'
            );
        }
    };

    like(
        $exception,
        qr/The 'handles' option must be a HASH reference, not bar/,
        "'bar' is given as handles");

    isa_ok(
        $exception,
        'Moose::Exception::HandlesMustBeAHashRef',
        "'bar' is given as handles");

    is(
        $exception->given_handles,
        "bar",
        "'bar' is given as handles");
}

{
    my $exception = exception {
        {
            package TraitTest;
            use Moose::Role;
            with 'Moose::Meta::Attribute::Native::Trait';

            sub _helper_type { "ArrayRef" }
        }

        {
            package TestClass3;
            use Moose;

            has 'foo' => (
                traits  => ['TraitTest'],
                is      => 'ro',
                isa     => 'ArrayRef',
                handles => { get_count => 'count' }
            );
        }
    };

    like(
        $exception,
        qr/\QCannot calculate native type for Moose::Meta::Class::__ANON__::SERIAL::/,
        "cannot calculate native type for the given trait");

    isa_ok(
        $exception,
        'Moose::Exception::CannotCalculateNativeType',
        "cannot calculate native type for the given trait");
}

{
    my $regex = qr/bar/;
    my $exception = exception {
        {
            package TestClass4;
            use Moose;

            has 'foo' => (
                traits  => ['Array'],
                is      => 'ro',
                isa     => 'ArrayRef',
                handles => { get_count => $regex }
            );
        }
    };

    like(
        $exception,
        qr/\QAll values passed to handles must be strings or ARRAY references, not $regex/,
        "a Regexp is given to handles");
        #All values passed to handles must be strings or ARRAY references, not (?^:bar)

    isa_ok(
        $exception,
        'Moose::Exception::InvalidHandleValue',
        "a Regexp is given to handles");

    is(
        $exception->handle_value,
        $regex,
        "a Regexp is given to handles");
}

done_testing;