File: union_types.t

package info (click to toggle)
libmoose-perl 2.2207-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,416 kB
  • sloc: perl: 21,345; ansic: 291; makefile: 10
file content (195 lines) | stat: -rw-r--r-- 5,422 bytes parent folder | download | duplicates (7)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use strict;
use warnings;

use Test::More;

use Moose::Util::TypeConstraints;

my $Str = find_type_constraint('Str');
isa_ok( $Str, 'Moose::Meta::TypeConstraint' );

my $Undef = find_type_constraint('Undef');
isa_ok( $Undef, 'Moose::Meta::TypeConstraint' );

ok( !$Str->check(undef),      '... Str cannot accept an Undef value' );
ok( $Str->check('String'),    '... Str can accept an String value' );
ok( !$Undef->check('String'), '... Undef cannot accept an Str value' );
ok( $Undef->check(undef),     '... Undef can accept an Undef value' );

my $Str_or_Undef = Moose::Meta::TypeConstraint::Union->new(
    type_constraints => [ $Str, $Undef ] );
isa_ok( $Str_or_Undef, 'Moose::Meta::TypeConstraint::Union' );

ok(
    $Str_or_Undef->check(undef),
    '... (Str | Undef) can accept an Undef value'
);
ok(
    $Str_or_Undef->check('String'),
    '... (Str | Undef) can accept a String value'
);

ok( !$Str_or_Undef->is_a_type_of($Str),   "not a subtype of Str" );
ok( !$Str_or_Undef->is_a_type_of($Undef), "not a subtype of Undef" );

cmp_ok(
    $Str_or_Undef->find_type_for('String'), 'eq', 'Str',
    'find_type_for Str'
);
cmp_ok(
    $Str_or_Undef->find_type_for(undef), 'eq', 'Undef',
    'find_type_for Undef'
);
ok(
    !defined( $Str_or_Undef->find_type_for( sub { } ) ),
    'no find_type_for CodeRef'
);

ok( !$Str_or_Undef->equals($Str),         "not equal to Str" );
ok( $Str_or_Undef->equals($Str_or_Undef), "equal to self" );
ok(
    $Str_or_Undef->equals(
        Moose::Meta::TypeConstraint::Union->new(
            type_constraints => [ $Str, $Undef ]
        )
    ),
    "equal to clone"
);
ok(
    $Str_or_Undef->equals(
        Moose::Meta::TypeConstraint::Union->new(
            type_constraints => [ $Undef, $Str ]
        )
    ),
    "equal to reversed clone"
);

ok(
    !$Str_or_Undef->is_a_type_of("ThisTypeDoesNotExist"),
    "not type of non existent type"
);
ok(
    !$Str_or_Undef->is_subtype_of("ThisTypeDoesNotExist"),
    "not subtype of non existent type"
);

is(
    $Str_or_Undef->parent,
    find_type_constraint('Item'),
    'parent of Str|Undef is Item'
);

is_deeply(
    [$Str_or_Undef->parents],
    [find_type_constraint('Item')],
    'parents of Str|Undef is Item'
);

# another ....

my $ArrayRef = find_type_constraint('ArrayRef');
isa_ok( $ArrayRef, 'Moose::Meta::TypeConstraint' );

my $HashRef = find_type_constraint('HashRef');
isa_ok( $HashRef, 'Moose::Meta::TypeConstraint' );

ok( $ArrayRef->check( [] ), '... ArrayRef can accept an [] value' );
ok( !$ArrayRef->check( {} ), '... ArrayRef cannot accept an {} value' );
ok( $HashRef->check(   {} ), '... HashRef can accept an {} value' );
ok( !$HashRef->check( [] ), '... HashRef cannot accept an [] value' );

my $ArrayRef_or_HashRef = Moose::Meta::TypeConstraint::Union->new(
    type_constraints => [ $ArrayRef, $HashRef ] );
isa_ok( $ArrayRef_or_HashRef, 'Moose::Meta::TypeConstraint::Union' );

ok( $ArrayRef_or_HashRef->check( [] ),
    '... (ArrayRef | HashRef) can accept []' );
ok( $ArrayRef_or_HashRef->check( {} ),
    '... (ArrayRef | HashRef) can accept {}' );

ok(
    !$ArrayRef_or_HashRef->check( \( my $var1 ) ),
    '... (ArrayRef | HashRef) cannot accept scalar refs'
);
ok(
    !$ArrayRef_or_HashRef->check( sub { } ),
    '... (ArrayRef | HashRef) cannot accept code refs'
);
ok(
    !$ArrayRef_or_HashRef->check(50),
    '... (ArrayRef | HashRef) cannot accept Numbers'
);

diag $ArrayRef_or_HashRef->validate( [] );

ok(
    !defined( $ArrayRef_or_HashRef->validate( [] ) ),
    '... (ArrayRef | HashRef) can accept []'
);
ok(
    !defined( $ArrayRef_or_HashRef->validate( {} ) ),
    '... (ArrayRef | HashRef) can accept {}'
);

like(
    $ArrayRef_or_HashRef->validate( \( my $var2 ) ),
    qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/,
    '... (ArrayRef | HashRef) cannot accept scalar refs'
);

like(
    $ArrayRef_or_HashRef->validate( sub { } ),
    qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/,
    '... (ArrayRef | HashRef) cannot accept code refs'
);

is(
    $ArrayRef_or_HashRef->validate(50),
    'Validation failed for \'ArrayRef\' with value 50 and Validation failed for \'HashRef\' with value 50 in (ArrayRef|HashRef)',
    '... (ArrayRef | HashRef) cannot accept Numbers'
);

is(
    $ArrayRef_or_HashRef->parent,
    find_type_constraint('Ref'),
    'parent of ArrayRef|HashRef is Ref'
);

my $double_union = Moose::Meta::TypeConstraint::Union->new(
    type_constraints => [ $Str_or_Undef, $ArrayRef_or_HashRef ] );

is(
    $double_union->parent,
    find_type_constraint('Item'),
    'parent of (Str|Undef)|(ArrayRef|HashRef) is Item'
);

ok(
    $double_union->is_subtype_of('Item'),
    '(Str|Undef)|(ArrayRef|HashRef) is a subtype of Item'
);

ok(
    $double_union->is_a_type_of('Item'),
    '(Str|Undef)|(ArrayRef|HashRef) is a type of Item'
);

ok(
    !$double_union->is_a_type_of('Str'),
    '(Str|Undef)|(ArrayRef|HashRef) is not a type of Str'
);

type 'SomeType', where { 1 };
type 'OtherType', where { 1 };

my $parentless_union = Moose::Meta::TypeConstraint::Union->new(
    type_constraints => [
        find_type_constraint('SomeType'),
        find_type_constraint('OtherType'),
    ],
);

is($parentless_union->parent, undef, "no common ancestor gives undef parent");


done_testing;