summaryrefslogtreecommitdiff
path: root/t/type_constraints/subtyping_union_types.t
blob: d2a514f47f056f066616af530d56ff018a8b1574 (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
use strict;
use warnings;

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

use Moose::Util::TypeConstraints;

is( exception {
    subtype 'MyCollections' => as 'ArrayRef | HashRef';
}, undef, '... created the subtype special okay' );

{
    my $t = find_type_constraint('MyCollections');
    isa_ok($t, 'Moose::Meta::TypeConstraint');

    is($t->name, 'MyCollections', '... name is correct');

    my $p = $t->parent;
    isa_ok($p, 'Moose::Meta::TypeConstraint::Union');
    isa_ok($p, 'Moose::Meta::TypeConstraint');

    is($p->name, 'ArrayRef|HashRef', '... parent name is correct');

    ok($t->check([]), '... validated it correctly');
    ok($t->check({}), '... validated it correctly');
    ok(!$t->check(1), '... validated it correctly');
}

is( exception {
    subtype 'MyCollectionsExtended'
        => as 'ArrayRef|HashRef'
        => where {
            if (ref($_) eq 'ARRAY') {
                return if scalar(@$_) < 2;
            }
            elsif (ref($_) eq 'HASH') {
                return if scalar(keys(%$_)) < 2;
            }
            1;
        };
}, undef, '... created the subtype special okay' );

{
    my $t = find_type_constraint('MyCollectionsExtended');
    isa_ok($t, 'Moose::Meta::TypeConstraint');

    is($t->name, 'MyCollectionsExtended', '... name is correct');

    my $p = $t->parent;
    isa_ok($p, 'Moose::Meta::TypeConstraint::Union');
    isa_ok($p, 'Moose::Meta::TypeConstraint');

    is($p->name, 'ArrayRef|HashRef', '... parent name is correct');

    ok(!$t->check([]), '... validated it correctly');
    ok($t->check([1, 2]), '... validated it correctly');

    ok(!$t->check({}), '... validated it correctly');
    ok($t->check({ one => 1, two => 2 }), '... validated it correctly');

    ok(!$t->check(1), '... validated it correctly');
}

{
    my $union = Moose::Util::TypeConstraints::find_or_create_type_constraint('Int|ArrayRef[Int]');
    subtype 'UnionSub', as 'Int|ArrayRef[Int]';

    my $subtype = find_type_constraint('UnionSub');

    ok(
        !$union->is_a_type_of('Ref'),
        'Int|ArrayRef[Int] is not a type of Ref'
    );
    ok(
        !$subtype->is_a_type_of('Ref'),
        'subtype of Int|ArrayRef[Int] is not a type of Ref'
    );

    ok(
        $union->is_a_type_of('Defined'),
        'Int|ArrayRef[Int] is a type of Defined'
    );
    ok(
        $subtype->is_a_type_of('Defined'),
        'subtype of Int|ArrayRef[Int] is a type of Defined'
    );

    ok(
        !$union->is_subtype_of('Ref'),
        'Int|ArrayRef[Int] is not a subtype of Ref'
    );
    ok(
        !$subtype->is_subtype_of('Ref'),
        'subtype of Int|ArrayRef[Int] is not a subtype of Ref'
    );

    ok(
        $union->is_subtype_of('Defined'),
        'Int|ArrayRef[Int] is a subtype of Defined'
    );
    ok(
        $subtype->is_subtype_of('Defined'),
        'subtype of Int|ArrayRef[Int] is a subtype of Defined'
    );
}

done_testing;