8 use Moose::Util::TypeConstraints;
10 my $Str = find_type_constraint('Str');
11 isa_ok( $Str, 'Moose::Meta::TypeConstraint' );
13 my $Undef = find_type_constraint('Undef');
14 isa_ok( $Undef, 'Moose::Meta::TypeConstraint' );
16 ok( !$Str->check(undef), '... Str cannot accept an Undef value' );
17 ok( $Str->check('String'), '... Str can accept an String value' );
18 ok( !$Undef->check('String'), '... Undef cannot accept an Str value' );
19 ok( $Undef->check(undef), '... Undef can accept an Undef value' );
21 my $Str_or_Undef = Moose::Meta::TypeConstraint::Union->new(
22 type_constraints => [ $Str, $Undef ] );
23 isa_ok( $Str_or_Undef, 'Moose::Meta::TypeConstraint::Union' );
26 $Str_or_Undef->check(undef),
27 '... (Str | Undef) can accept an Undef value'
30 $Str_or_Undef->check('String'),
31 '... (Str | Undef) can accept a String value'
34 ok( !$Str_or_Undef->is_a_type_of($Str), "not a subtype of Str" );
35 ok( !$Str_or_Undef->is_a_type_of($Undef), "not a subtype of Undef" );
38 $Str_or_Undef->find_type_for('String'), 'eq', 'Str',
42 $Str_or_Undef->find_type_for(undef), 'eq', 'Undef',
46 !defined( $Str_or_Undef->find_type_for( sub { } ) ),
47 'no find_type_for CodeRef'
50 ok( !$Str_or_Undef->equals($Str), "not equal to Str" );
51 ok( $Str_or_Undef->equals($Str_or_Undef), "equal to self" );
53 $Str_or_Undef->equals(
54 Moose::Meta::TypeConstraint::Union->new(
55 type_constraints => [ $Str, $Undef ]
61 $Str_or_Undef->equals(
62 Moose::Meta::TypeConstraint::Union->new(
63 type_constraints => [ $Undef, $Str ]
66 "equal to reversed clone"
70 !$Str_or_Undef->is_a_type_of("ThisTypeDoesNotExist"),
71 "not type of non existent type"
74 !$Str_or_Undef->is_subtype_of("ThisTypeDoesNotExist"),
75 "not subtype of non existent type"
79 $Str_or_Undef->parent,
80 find_type_constraint('Item'),
81 'parent of Str|Undef is Item'
85 [$Str_or_Undef->parents],
86 [find_type_constraint('Item')],
87 'parents of Str|Undef is Item'
92 my $ArrayRef = find_type_constraint('ArrayRef');
93 isa_ok( $ArrayRef, 'Moose::Meta::TypeConstraint' );
95 my $HashRef = find_type_constraint('HashRef');
96 isa_ok( $HashRef, 'Moose::Meta::TypeConstraint' );
98 ok( $ArrayRef->check( [] ), '... ArrayRef can accept an [] value' );
99 ok( !$ArrayRef->check( {} ), '... ArrayRef cannot accept an {} value' );
100 ok( $HashRef->check( {} ), '... HashRef can accept an {} value' );
101 ok( !$HashRef->check( [] ), '... HashRef cannot accept an [] value' );
103 my $ArrayRef_or_HashRef = Moose::Meta::TypeConstraint::Union->new(
104 type_constraints => [ $ArrayRef, $HashRef ] );
105 isa_ok( $ArrayRef_or_HashRef, 'Moose::Meta::TypeConstraint::Union' );
107 ok( $ArrayRef_or_HashRef->check( [] ),
108 '... (ArrayRef | HashRef) can accept []' );
109 ok( $ArrayRef_or_HashRef->check( {} ),
110 '... (ArrayRef | HashRef) can accept {}' );
113 !$ArrayRef_or_HashRef->check( \( my $var1 ) ),
114 '... (ArrayRef | HashRef) cannot accept scalar refs'
117 !$ArrayRef_or_HashRef->check( sub { } ),
118 '... (ArrayRef | HashRef) cannot accept code refs'
121 !$ArrayRef_or_HashRef->check(50),
122 '... (ArrayRef | HashRef) cannot accept Numbers'
125 diag $ArrayRef_or_HashRef->validate( [] );
128 !defined( $ArrayRef_or_HashRef->validate( [] ) ),
129 '... (ArrayRef | HashRef) can accept []'
132 !defined( $ArrayRef_or_HashRef->validate( {} ) ),
133 '... (ArrayRef | HashRef) can accept {}'
137 $ArrayRef_or_HashRef->validate( \( my $var2 ) ),
138 qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/,
139 '... (ArrayRef | HashRef) cannot accept scalar refs'
143 $ArrayRef_or_HashRef->validate( sub { } ),
144 qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/,
145 '... (ArrayRef | HashRef) cannot accept code refs'
149 $ArrayRef_or_HashRef->validate(50),
150 'Validation failed for \'ArrayRef\' with value 50 and Validation failed for \'HashRef\' with value 50 in (ArrayRef|HashRef)',
151 '... (ArrayRef | HashRef) cannot accept Numbers'
155 $ArrayRef_or_HashRef->parent,
156 find_type_constraint('Ref'),
157 'parent of ArrayRef|HashRef is Ref'
160 my $double_union = Moose::Meta::TypeConstraint::Union->new(
161 type_constraints => [ $Str_or_Undef, $ArrayRef_or_HashRef ] );
164 $double_union->parent,
165 find_type_constraint('Item'),
166 'parent of (Str|Undef)|(ArrayRef|HashRef) is Item'
170 $double_union->is_subtype_of('Item'),
171 '(Str|Undef)|(ArrayRef|HashRef) is a subtype of Item'
175 $double_union->is_a_type_of('Item'),
176 '(Str|Undef)|(ArrayRef|HashRef) is a type of Item'
180 !$double_union->is_a_type_of('Str'),
181 '(Str|Undef)|(ArrayRef|HashRef) is not a type of Str'
184 type 'SomeType', where { 1 };
185 type 'OtherType', where { 1 };
187 my $parentless_union = Moose::Meta::TypeConstraint::Union->new(
188 type_constraints => [
189 find_type_constraint('SomeType'),
190 find_type_constraint('OtherType'),
194 is($parentless_union->parent, undef, "no common ancestor gives undef parent");