added default {} keyword
[gitmo/Moose.git] / t / 052_util_std_type_constraints.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 194;
7 use Test::Exception;
8
9 use Scalar::Util ();
10
11 BEGIN {
12     use_ok('Moose::Util::TypeConstraints');           
13 }
14
15 my $SCALAR_REF = \(my $var);
16
17 Moose::Util::TypeConstraints->export_type_contstraints_as_functions();
18
19 ok(defined Any(0),               '... Any accepts anything');
20 ok(defined Any(100),             '... Any accepts anything');
21 ok(defined Any(''),              '... Any accepts anything');
22 ok(defined Any('Foo'),           '... Any accepts anything');
23 ok(defined Any([]),              '... Any accepts anything');
24 ok(defined Any({}),              '... Any accepts anything');
25 ok(defined Any(sub {}),          '... Any accepts anything');
26 ok(defined Any($SCALAR_REF),     '... Any accepts anything');
27 ok(defined Any(qr/../),          '... Any accepts anything');
28 ok(defined Any(bless {}, 'Foo'), '... Any accepts anything');
29 ok(defined Any(undef),           '... Any accepts anything');
30
31 ok(defined Item(0),               '... Item is the base type, so accepts anything');
32 ok(defined Item(100),             '... Item is the base type, so accepts anything');
33 ok(defined Item(''),              '... Item is the base type, so accepts anything');
34 ok(defined Item('Foo'),           '... Item is the base type, so accepts anything');
35 ok(defined Item([]),              '... Item is the base type, so accepts anything');
36 ok(defined Item({}),              '... Item is the base type, so accepts anything');
37 ok(defined Item(sub {}),          '... Item is the base type, so accepts anything');
38 ok(defined Item($SCALAR_REF),     '... Item is the base type, so accepts anything');
39 ok(defined Item(qr/../),          '... Item is the base type, so accepts anything');
40 ok(defined Item(bless {}, 'Foo'), '... Item is the base type, so accepts anything');
41 ok(defined Item(undef),           '... Item is the base type, so accepts anything');
42
43 ok(defined Defined(0),               '... Defined accepts anything which is defined');
44 ok(defined Defined(100),             '... Defined accepts anything which is defined');
45 ok(defined Defined(''),              '... Defined accepts anything which is defined');
46 ok(defined Defined('Foo'),           '... Defined accepts anything which is defined');
47 ok(defined Defined([]),              '... Defined accepts anything which is defined');
48 ok(defined Defined({}),              '... Defined accepts anything which is defined');
49 ok(defined Defined(sub {}),          '... Defined accepts anything which is defined');
50 ok(defined Defined($SCALAR_REF),     '... Defined accepts anything which is defined');
51 ok(defined Defined(qr/../),          '... Defined accepts anything which is defined');
52 ok(defined Defined(bless {}, 'Foo'), '... Defined accepts anything which is defined');
53 ok(!defined Defined(undef),          '... Defined accepts anything which is defined');
54
55 ok(!defined Undef(0),               '... Undef accepts anything which is not defined');
56 ok(!defined Undef(100),             '... Undef accepts anything which is not defined');
57 ok(!defined Undef(''),              '... Undef accepts anything which is not defined');
58 ok(!defined Undef('Foo'),           '... Undef accepts anything which is not defined');
59 ok(!defined Undef([]),              '... Undef accepts anything which is not defined');
60 ok(!defined Undef({}),              '... Undef accepts anything which is not defined');
61 ok(!defined Undef(sub {}),          '... Undef accepts anything which is not defined');
62 ok(!defined Undef($SCALAR_REF),     '... Undef accepts anything which is not defined');
63 ok(!defined Undef(qr/../),          '... Undef accepts anything which is not defined');
64 ok(!defined Undef(bless {}, 'Foo'), '... Undef accepts anything which is not defined');
65 ok(defined Undef(undef),            '... Undef accepts anything which is not defined');
66
67 ok(defined Bool(0),                 '... Bool rejects anything which is not a 1 or 0 or "" or undef');
68 ok(defined Bool(1),                 '... Bool rejects anything which is not a 1 or 0 or "" or undef');
69 ok(!defined Bool(100),              '... Bool rejects anything which is not a 1 or 0 or "" or undef');
70 ok(defined Bool(''),                '... Bool rejects anything which is not a 1 or 0 or "" or undef');
71 ok(!defined Bool('Foo'),            '... Bool rejects anything which is not a 1 or 0 or "" or undef');
72 ok(!defined Bool([]),               '... Bool rejects anything which is not a 1 or 0 or "" or undef');
73 ok(!defined Bool({}),               '... Bool rejects anything which is not a 1 or 0 or "" or undef');
74 ok(!defined Bool(sub {}),           '... Bool rejects anything which is not a 1 or 0 or "" or undef');
75 ok(!defined Bool($SCALAR_REF),      '... Bool rejects anything which is not a 1 or 0 or "" or undef');
76 ok(!defined Bool(qr/../),           '... Bool rejects anything which is not a 1 or 0 or "" or undef');
77 ok(!defined Bool(bless {}, 'Foo'),  '... Bool rejects anything which is not a 1 or 0 or "" or undef');
78 ok(defined Bool(undef),             '... Bool rejects anything which is not a 1 or 0 or "" or undef');
79
80 ok(defined Value(0),                 '... Value accepts anything which is not a Ref');
81 ok(defined Value(100),               '... Value accepts anything which is not a Ref');
82 ok(defined Value(''),                '... Value accepts anything which is not a Ref');
83 ok(defined Value('Foo'),             '... Value accepts anything which is not a Ref');
84 ok(!defined Value([]),               '... Value rejects anything which is not a Value');
85 ok(!defined Value({}),               '... Value rejects anything which is not a Value');
86 ok(!defined Value(sub {}),           '... Value rejects anything which is not a Value');
87 ok(!defined Value($SCALAR_REF),      '... Value rejects anything which is not a Value');
88 ok(!defined Value(qr/../),           '... Value rejects anything which is not a Value');
89 ok(!defined Value(bless {}, 'Foo'),  '... Value rejects anything which is not a Value');
90 ok(!defined Value(undef),            '... Value rejects anything which is not a Value');
91
92 ok(!defined Ref(0),               '... Ref accepts anything which is not a Value');
93 ok(!defined Ref(100),             '... Ref accepts anything which is not a Value');
94 ok(!defined Ref(''),              '... Ref accepts anything which is not a Value');
95 ok(!defined Ref('Foo'),           '... Ref accepts anything which is not a Value');
96 ok(defined Ref([]),               '... Ref rejects anything which is not a Ref');
97 ok(defined Ref({}),               '... Ref rejects anything which is not a Ref');
98 ok(defined Ref(sub {}),           '... Ref rejects anything which is not a Ref');
99 ok(defined Ref($SCALAR_REF),      '... Ref rejects anything which is not a Ref');
100 ok(defined Ref(qr/../),           '... Ref rejects anything which is not a Ref');
101 ok(defined Ref(bless {}, 'Foo'),  '... Ref rejects anything which is not a Ref');
102 ok(!defined Ref(undef),           '... Ref rejects anything which is not a Ref');
103
104 ok(defined Int(0),                 '... Int accepts anything which is an Int');
105 ok(defined Int(100),               '... Int accepts anything which is an Int');
106 ok(!defined Int(0.5),              '... Int accepts anything which is not a Int');
107 ok(!defined Int(100.01),           '... Int accepts anything which is not a Int');
108 ok(!defined Int(''),               '... Int rejects anything which is not a Int');
109 ok(!defined Int('Foo'),            '... Int rejects anything which is not a Int');
110 ok(!defined Int([]),               '... Int rejects anything which is not a Int');
111 ok(!defined Int({}),               '... Int rejects anything which is not a Int');
112 ok(!defined Int(sub {}),           '... Int rejects anything which is not a Int');
113 ok(!defined Int($SCALAR_REF),      '... Int rejects anything which is not a Int');
114 ok(!defined Int(qr/../),           '... Int rejects anything which is not a Int');
115 ok(!defined Int(bless {}, 'Foo'),  '... Int rejects anything which is not a Int');
116 ok(!defined Int(undef),            '... Int rejects anything which is not a Int');
117
118 ok(defined Num(0),                 '... Num accepts anything which is an Num');
119 ok(defined Num(100),               '... Num accepts anything which is an Num');
120 ok(defined Num(0.5),               '... Num accepts anything which is an Num');
121 ok(defined Num(100.01),            '... Num accepts anything which is an Num');
122 ok(!defined Num(''),               '... Num rejects anything which is not a Num');
123 ok(!defined Num('Foo'),            '... Num rejects anything which is not a Num');
124 ok(!defined Num([]),               '... Num rejects anything which is not a Num');
125 ok(!defined Num({}),               '... Num rejects anything which is not a Num');
126 ok(!defined Num(sub {}),           '... Num rejects anything which is not a Num');
127 ok(!defined Num($SCALAR_REF),      '... Num rejects anything which is not a Num');
128 ok(!defined Num(qr/../),           '... Num rejects anything which is not a Num');
129 ok(!defined Num(bless {}, 'Foo'),  '... Num rejects anything which is not a Num');
130 ok(!defined Num(undef),            '... Num rejects anything which is not a Num');
131
132 ok(defined Str(0),                 '... Str accepts anything which is a Str');
133 ok(defined Str(100),               '... Str accepts anything which is a Str');
134 ok(defined Str(''),                '... Str accepts anything which is a Str');
135 ok(defined Str('Foo'),             '... Str accepts anything which is a Str');
136 ok(!defined Str([]),               '... Str rejects anything which is not a Str');
137 ok(!defined Str({}),               '... Str rejects anything which is not a Str');
138 ok(!defined Str(sub {}),           '... Str rejects anything which is not a Str');
139 ok(!defined Str($SCALAR_REF),      '... Str rejects anything which is not a Str');
140 ok(!defined Str(qr/../),           '... Str rejects anything which is not a Str');
141 ok(!defined Str(bless {}, 'Foo'),  '... Str rejects anything which is not a Str');
142 ok(!defined Str(undef),            '... Str rejects anything which is not a Str');
143
144 ok(!defined ScalarRef(0),                '... ScalarRef rejects anything which is not a ScalarRef');
145 ok(!defined ScalarRef(100),              '... ScalarRef rejects anything which is not a ScalarRef');
146 ok(!defined ScalarRef(''),               '... ScalarRef rejects anything which is not a ScalarRef');
147 ok(!defined ScalarRef('Foo'),            '... ScalarRef rejects anything which is not a ScalarRef');
148 ok(!defined ScalarRef([]),               '... ScalarRef rejects anything which is not a ScalarRef');
149 ok(!defined ScalarRef({}),               '... ScalarRef rejects anything which is not a ScalarRef');
150 ok(!defined ScalarRef(sub {}),           '... ScalarRef rejects anything which is not a ScalarRef');
151 ok(defined ScalarRef($SCALAR_REF),       '... ScalarRef accepts anything which is a ScalarRef');
152 ok(!defined ScalarRef(qr/../),           '... ScalarRef rejects anything which is not a ScalarRef');
153 ok(!defined ScalarRef(bless {}, 'Foo'),  '... ScalarRef rejects anything which is not a ScalarRef');
154 ok(!defined ScalarRef(undef),            '... ScalarRef rejects anything which is not a ScalarRef');
155
156 ok(!defined ArrayRef(0),                '... ArrayRef rejects anything which is not a ArrayRef');
157 ok(!defined ArrayRef(100),              '... ArrayRef rejects anything which is not a ArrayRef');
158 ok(!defined ArrayRef(''),               '... ArrayRef rejects anything which is not a ArrayRef');
159 ok(!defined ArrayRef('Foo'),            '... ArrayRef rejects anything which is not a ArrayRef');
160 ok(defined ArrayRef([]),                '... ArrayRef accepts anything which is a ArrayRef');
161 ok(!defined ArrayRef({}),               '... ArrayRef rejects anything which is not a ArrayRef');
162 ok(!defined ArrayRef(sub {}),           '... ArrayRef rejects anything which is not a ArrayRef');
163 ok(!defined ArrayRef($SCALAR_REF),      '... ArrayRef rejects anything which is not a ArrayRef');
164 ok(!defined ArrayRef(qr/../),           '... ArrayRef rejects anything which is not a ArrayRef');
165 ok(!defined ArrayRef(bless {}, 'Foo'),  '... ArrayRef rejects anything which is not a ArrayRef');
166 ok(!defined ArrayRef(undef),            '... ArrayRef rejects anything which is not a ArrayRef');
167
168 ok(!defined HashRef(0),                '... HashRef rejects anything which is not a HashRef');
169 ok(!defined HashRef(100),              '... HashRef rejects anything which is not a HashRef');
170 ok(!defined HashRef(''),               '... HashRef rejects anything which is not a HashRef');
171 ok(!defined HashRef('Foo'),            '... HashRef rejects anything which is not a HashRef');
172 ok(!defined HashRef([]),               '... HashRef rejects anything which is not a HashRef');
173 ok(defined HashRef({}),                '... HashRef accepts anything which is a HashRef');
174 ok(!defined HashRef(sub {}),           '... HashRef rejects anything which is not a HashRef');
175 ok(!defined HashRef($SCALAR_REF),      '... HashRef rejects anything which is not a HashRef');
176 ok(!defined HashRef(qr/../),           '... HashRef rejects anything which is not a HashRef');
177 ok(!defined HashRef(bless {}, 'Foo'),  '... HashRef rejects anything which is not a HashRef');
178 ok(!defined HashRef(undef),            '... HashRef rejects anything which is not a HashRef');
179
180 ok(!defined CodeRef(0),                '... CodeRef rejects anything which is not a CodeRef');
181 ok(!defined CodeRef(100),              '... CodeRef rejects anything which is not a CodeRef');
182 ok(!defined CodeRef(''),               '... CodeRef rejects anything which is not a CodeRef');
183 ok(!defined CodeRef('Foo'),            '... CodeRef rejects anything which is not a CodeRef');
184 ok(!defined CodeRef([]),               '... CodeRef rejects anything which is not a CodeRef');
185 ok(!defined CodeRef({}),               '... CodeRef rejects anything which is not a CodeRef');
186 ok(defined CodeRef(sub {}),            '... CodeRef accepts anything which is a CodeRef');
187 ok(!defined CodeRef($SCALAR_REF),      '... CodeRef rejects anything which is not a CodeRef');
188 ok(!defined CodeRef(qr/../),           '... CodeRef rejects anything which is not a CodeRef');
189 ok(!defined CodeRef(bless {}, 'Foo'),  '... CodeRef rejects anything which is not a CodeRef');
190 ok(!defined CodeRef(undef),            '... CodeRef rejects anything which is not a CodeRef');
191
192 ok(!defined RegexpRef(0),                '... RegexpRef rejects anything which is not a RegexpRef');
193 ok(!defined RegexpRef(100),              '... RegexpRef rejects anything which is not a RegexpRef');
194 ok(!defined RegexpRef(''),               '... RegexpRef rejects anything which is not a RegexpRef');
195 ok(!defined RegexpRef('Foo'),            '... RegexpRef rejects anything which is not a RegexpRef');
196 ok(!defined RegexpRef([]),               '... RegexpRef rejects anything which is not a RegexpRef');
197 ok(!defined RegexpRef({}),               '... RegexpRef rejects anything which is not a RegexpRef');
198 ok(!defined RegexpRef(sub {}),           '... RegexpRef rejects anything which is not a RegexpRef');
199 ok(!defined RegexpRef($SCALAR_REF),      '... RegexpRef rejects anything which is not a RegexpRef');
200 ok(defined RegexpRef(qr/../),            '... RegexpRef accepts anything which is a RegexpRef');
201 ok(!defined RegexpRef(bless {}, 'Foo'),  '... RegexpRef rejects anything which is not a RegexpRef');
202 ok(!defined RegexpRef(undef),            '... RegexpRef rejects anything which is not a RegexpRef');
203
204 ok(!defined Object(0),                '... Object rejects anything which is not blessed');
205 ok(!defined Object(100),              '... Object rejects anything which is not blessed');
206 ok(!defined Object(''),               '... Object rejects anything which is not blessed');
207 ok(!defined Object('Foo'),            '... Object rejects anything which is not blessed');
208 ok(!defined Object([]),               '... Object rejects anything which is not blessed');
209 ok(!defined Object({}),               '... Object rejects anything which is not blessed');
210 ok(!defined Object(sub {}),           '... Object rejects anything which is not blessed');
211 ok(!defined Object($SCALAR_REF),      '... Object rejects anything which is not blessed');
212 ok(!defined Object(qr/../),           '... Object rejects anything which is not blessed');
213 ok(defined Object(bless {}, 'Foo'),   '... Object accepts anything which is blessed');
214 ok(!defined Object(undef),             '... Object accepts anything which is blessed');
215
216 {
217     package My::Role;
218     sub does { 'fake' }
219 }
220
221 ok(!defined Role(0),                    '... Role rejects anything which is not a Role');
222 ok(!defined Role(100),                  '... Role rejects anything which is not a Role');
223 ok(!defined Role(''),                   '... Role rejects anything which is not a Role');
224 ok(!defined Role('Foo'),                '... Role rejects anything which is not a Role');
225 ok(!defined Role([]),                   '... Role rejects anything which is not a Role');
226 ok(!defined Role({}),                   '... Role rejects anything which is not a Role');
227 ok(!defined Role(sub {}),               '... Role rejects anything which is not a Role');
228 ok(!defined Role($SCALAR_REF),          '... Role rejects anything which is not a Role');
229 ok(!defined Role(qr/../),               '... Role rejects anything which is not a Role');
230 ok(!defined Role(bless {}, 'Foo'),      '... Role accepts anything which is not a Role');
231 ok(defined Role(bless {}, 'My::Role'),  '... Role accepts anything which is not a Role');
232 ok(!defined Role(undef),                 '... Role accepts anything which is not a Role');
233
234