summaryrefslogtreecommitdiff
path: root/t/exceptions/moose-meta-typecoercion-union.t
blob: 371216582d029fce21aa79e30f4781fd40dee248 (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
use strict;
use warnings;

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

use Moose;
use Moose::Util::TypeConstraints;

{
    my $exception = exception {
        Moose::Meta::TypeCoercion::Union->new( type_constraint => find_type_constraint("Str") );
    };

    like(
        $exception,
        qr/\QYou can only create a Moose::Meta::TypeCoercion::Union for a Moose::Meta::TypeConstraint::Union, not a Str/,
        "'Str' is not a Moose::Meta::TypeConstraint::Union");

    isa_ok(
        $exception,
        "Moose::Exception::NeedsTypeConstraintUnionForTypeCoercionUnion",
        "'Str' is not a Moose::Meta::TypeConstraint::Union");

    is(
        $exception->type_name,
        "Str",
        "'Str' is not a Moose::Meta::TypeConstraint::Union");
}

{
    union 'StringOrInt', [qw( Str Int )];
    my $type = find_type_constraint("StringOrInt");
    my $tt = Moose::Meta::TypeCoercion::Union->new( type_constraint => $type );

    my $exception = exception {
        $tt->add_type_coercions("ArrayRef");
    };

    like(
        $exception,
        qr/Cannot add additional type coercions to Union types/,
        "trying to add ArrayRef to a Moose::Meta::TypeCoercion::Union object");

    isa_ok(
        $exception,
        "Moose::Exception::CannotAddAdditionalTypeCoercionsToUnion",
        "trying to add ArrayRef to a Moose::Meta::TypeCoercion::Union object");

    is(
        $exception->type_coercion_union_object,
        $tt,
        "trying to add ArrayRef to a Moose::Meta::TypeCoercion::Union object");
}

done_testing;