Commit | Line | Data |
451c8248 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
451c8248 |
7 | |
28fdde7f |
8 | use Moose::Util::TypeConstraints; |
451c8248 |
9 | |
10 | my $Str = find_type_constraint('Str'); |
b53f21ca |
11 | isa_ok( $Str, 'Moose::Meta::TypeConstraint' ); |
451c8248 |
12 | |
13 | my $Undef = find_type_constraint('Undef'); |
b53f21ca |
14 | isa_ok( $Undef, 'Moose::Meta::TypeConstraint' ); |
15 | |
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' ); |
20 | |
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' ); |
24 | |
25 | ok( |
26 | $Str_or_Undef->check(undef), |
27 | '... (Str | Undef) can accept an Undef value' |
28 | ); |
29 | ok( |
30 | $Str_or_Undef->check('String'), |
31 | '... (Str | Undef) can accept a String value' |
32 | ); |
33 | |
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" ); |
36 | |
37 | cmp_ok( |
38 | $Str_or_Undef->find_type_for('String'), 'eq', 'Str', |
39 | 'find_type_for Str' |
40 | ); |
41 | cmp_ok( |
42 | $Str_or_Undef->find_type_for(undef), 'eq', 'Undef', |
43 | 'find_type_for Undef' |
44 | ); |
45 | ok( |
46 | !defined( $Str_or_Undef->find_type_for( sub { } ) ), |
47 | 'no find_type_for CodeRef' |
48 | ); |
49 | |
50 | ok( !$Str_or_Undef->equals($Str), "not equal to Str" ); |
dabed765 |
51 | ok( $Str_or_Undef->equals($Str_or_Undef), "equal to self" ); |
b53f21ca |
52 | ok( |
53 | $Str_or_Undef->equals( |
54 | Moose::Meta::TypeConstraint::Union->new( |
55 | type_constraints => [ $Str, $Undef ] |
56 | ) |
57 | ), |
58 | "equal to clone" |
59 | ); |
60 | ok( |
61 | $Str_or_Undef->equals( |
62 | Moose::Meta::TypeConstraint::Union->new( |
63 | type_constraints => [ $Undef, $Str ] |
64 | ) |
65 | ), |
66 | "equal to reversed clone" |
67 | ); |
68 | |
69 | ok( |
70 | !$Str_or_Undef->is_a_type_of("ThisTypeDoesNotExist"), |
71 | "not type of non existent type" |
72 | ); |
73 | ok( |
74 | !$Str_or_Undef->is_subtype_of("ThisTypeDoesNotExist"), |
75 | "not subtype of non existent type" |
76 | ); |
4c015454 |
77 | |
451c8248 |
78 | # another .... |
79 | |
80 | my $ArrayRef = find_type_constraint('ArrayRef'); |
b53f21ca |
81 | isa_ok( $ArrayRef, 'Moose::Meta::TypeConstraint' ); |
451c8248 |
82 | |
83 | my $HashRef = find_type_constraint('HashRef'); |
b53f21ca |
84 | isa_ok( $HashRef, 'Moose::Meta::TypeConstraint' ); |
85 | |
86 | ok( $ArrayRef->check( [] ), '... ArrayRef can accept an [] value' ); |
87 | ok( !$ArrayRef->check( {} ), '... ArrayRef cannot accept an {} value' ); |
88 | ok( $HashRef->check( {} ), '... HashRef can accept an {} value' ); |
89 | ok( !$HashRef->check( [] ), '... HashRef cannot accept an [] value' ); |
90 | |
91 | my $HashOrArray = Moose::Meta::TypeConstraint::Union->new( |
92 | type_constraints => [ $ArrayRef, $HashRef ] ); |
93 | isa_ok( $HashOrArray, 'Moose::Meta::TypeConstraint::Union' ); |
94 | |
95 | ok( $HashOrArray->check( [] ), '... (ArrayRef | HashRef) can accept []' ); |
96 | ok( $HashOrArray->check( {} ), '... (ArrayRef | HashRef) can accept {}' ); |
97 | |
98 | ok( |
99 | !$HashOrArray->check( \( my $var1 ) ), |
100 | '... (ArrayRef | HashRef) cannot accept scalar refs' |
101 | ); |
102 | ok( |
103 | !$HashOrArray->check( sub { } ), |
104 | '... (ArrayRef | HashRef) cannot accept code refs' |
105 | ); |
106 | ok( |
107 | !$HashOrArray->check(50), |
108 | '... (ArrayRef | HashRef) cannot accept Numbers' |
109 | ); |
110 | |
111 | diag $HashOrArray->validate( [] ); |
112 | |
113 | ok( |
114 | !defined( $HashOrArray->validate( [] ) ), |
115 | '... (ArrayRef | HashRef) can accept []' |
116 | ); |
117 | ok( |
118 | !defined( $HashOrArray->validate( {} ) ), |
119 | '... (ArrayRef | HashRef) can accept {}' |
120 | ); |
121 | |
122 | like( |
123 | $HashOrArray->validate( \( my $var2 ) ), |
124 | qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/, |
125 | '... (ArrayRef | HashRef) cannot accept scalar refs' |
126 | ); |
127 | |
128 | like( |
129 | $HashOrArray->validate( sub { } ), |
130 | qr/Validation failed for \'ArrayRef\' with value .+ and Validation failed for \'HashRef\' with value .+ in \(ArrayRef\|HashRef\)/, |
131 | '... (ArrayRef | HashRef) cannot accept code refs' |
132 | ); |
133 | |
134 | is( |
135 | $HashOrArray->validate(50), |
136 | 'Validation failed for \'ArrayRef\' with value 50 and Validation failed for \'HashRef\' with value 50 in (ArrayRef|HashRef)', |
137 | '... (ArrayRef | HashRef) cannot accept Numbers' |
138 | ); |
451c8248 |
139 | |
a28e50e4 |
140 | done_testing; |