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