Debian Codegraph @

t/types.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 51;
4 use Test::Fatal;
5 use Dancer2::Core::Types;
6
7
8 ok( exception { Str->(undef) }, 'Str does not accept undef value', );
9
10 is( exception { Str->('something') }, undef, 'Str', );
11
12 like(
13 exception { Str->( { foo => 'something' } ) },
14 qr{Reference.+foo.+something.+did not pass type constraint.+Str}, 'Str',
15 );
16
17 is( exception { Num->(34) }, undef, 'Num', );
18
19 ok( exception { Num->(undef) }, 'Num does not accept undef value', );
20
21 like(
22 exception { Num->('not a number') },
23 qr{not a number.+did not pass type constraint.+Num},
24 'Num fail',
25 );
26
27 is( exception { Bool->(1) }, undef, 'Bool true value', );
28
29 is( exception { Bool->(0) }, undef, 'Bool false value', );
30
31 is( exception { Bool->(undef) }, undef, 'Bool does accepts undef value', );
32
33 like(
34 exception { Bool->('2') },
35 qr{2.+did not pass type constraint.+Bool},
36 'Bool fail',
37 );
38
39 is( exception { RegexpRef->(qr{.*}) }, undef, 'Regexp', );
40
41 like(
42 exception { RegexpRef->('/.*/') },
43 qr{\Q/.*/\E.+did not pass type constraint.+RegexpRef},
44 'Regexp fail',
45 );
46
47 ok( exception { RegexpRef->(undef) }, 'Regexp does not accept undef value', );
48
49 is( exception { HashRef->( { goo => 'le' } ) }, undef, 'HashRef', );
50
51 like(
52 exception { HashRef->('/.*/') },
53 qr{\Q/.*/\E.+did not pass type constraint.+HashRef},
54 'HashRef fail',
55 );
56
57 ok( exception { HashRef->(undef) }, 'HashRef does not accept undef value', );
58
59 is( exception { ArrayRef->( [ 1, 2, 3, 4 ] ) }, undef, 'ArrayRef', );
60
61 like(
62 exception { ArrayRef->('/.*/') },
63 qr{\Q/.*/\E.+did not pass type constraint.+ArrayRef},
64 'ArrayRef fail',
65 );
66
67 ok( exception { ArrayRef->(undef) }, 'ArrayRef does not accept undef value', );
68
69 is( exception {
70 CodeRef->( sub {44} );
71 },
72 undef,
73 'CodeRef',
74 );
75
76 like(
77 exception { CodeRef->('/.*/') },
78 qr{\Q/.*/\E.+did not pass type constraint.+CodeRef},
79 'CodeRef fail',
80 );
81
82 ok( exception { CodeRef->(undef) }, 'CodeRef does not accept undef value', );
83
84 {
85
86 package InstanceChecker::zad7;
87 use Moo;
88 use Dancer2::Core::Types;
89 has foo => ( is => 'ro', isa => InstanceOf ['Foo'] );
90 }
91
92 is( exception { InstanceChecker::zad7->new( foo => bless {}, 'Foo' ) },
93 undef, 'InstanceOf',
94 );
95
96 like(
97 exception { InstanceChecker::zad7->new( foo => bless {}, 'Bar' ) },
98 qr{Reference bless.+Bar.+not isa Foo},
99 'InstanceOf fail',
100 );
101
102 ok( exception { InstanceOf('Foo')->(undef) },
103 'InstanceOf does not accept undef value',
104 );
105
106 is( exception { Dancer2Prefix->('/foo') }, undef, 'Dancer2Prefix', );
107
108 like(
109 exception { Dancer2Prefix->('bar/something') },
110 qr{bar/something.+did not pass type constraint.+Dancer2Prefix},
111 'Dancer2Prefix fail',
112 );
113
114 # see Dancer2Prefix definition, undef is a valid value
115 like(
116 exception { Dancer2Prefix->(undef) },
117 qr/Undef.+did not pass type constraint.+Dancer2Prefix/,
118 'Dancer2Prefix does not accept undef value',
119 );
120
121 is( exception { Dancer2AppName->('Foo') }, undef, 'Dancer2AppName', );
122
123 is( exception { Dancer2AppName->('Foo::Bar') }, undef, 'Dancer2AppName', );
124
125 is( exception { Dancer2AppName->('Foo::Bar::Baz') }, undef, 'Dancer2AppName',
126 );
127
128 like(
129 exception { Dancer2AppName->('Foo:Bar') },
130 qr{Foo:Bar is not a Dancer2AppName},
131 'Dancer2AppName fails with single colons',
132 );
133
134 like(
135 exception { Dancer2AppName->('Foo:::Bar') },
136 qr{Foo:::Bar is not a Dancer2AppName},
137 'Dancer2AppName fails with tripe colons',
138 );
139
140 like(
141 exception { Dancer2AppName->('7Foo') },
142 qr{7Foo is not a Dancer2AppName},
143 'Dancer2AppName fails with beginning number',
144 );
145
146 like(
147 exception { Dancer2AppName->('Foo::45Bar') },
148 qr{Foo::45Bar is not a Dancer2AppName},
149 'Dancer2AppName fails with beginning number',
150 );
151
152 like(
153 exception { Dancer2AppName->('-F') },
154 qr{-F is not a Dancer2AppName},
155 'Dancer2AppName fails with special character',
156 );
157
158 like(
159 exception { Dancer2AppName->('Foo::-') },
160 qr{Foo::- is not a Dancer2AppName},
161 'Dancer2AppName fails with special character',
162 );
163
164 like(
165 exception { Dancer2AppName->('Foo^') },
166 qr{\QFoo^\E is not a Dancer2AppName},
167 'Dancer2AppName fails with special character',
168 );
169
170 ok( exception { Dancer2AppName->(undef) },
171 'Dancer2AppName does not accept undef value',
172 );
173
174 like(
175 exception { Dancer2AppName->('') },
176 qr{Empty string is not a Dancer2AppName},
177 'Dancer2AppName fails an empty string value',
178 );
179
180 is( exception { Dancer2Method->('post') }, undef, 'Dancer2Method', );
181
182 like(
183 exception { Dancer2Method->('POST') },
184 qr{POST.+did not pass type constraint.+Dancer2Method},
185 'Dancer2Method fail',
186 );
187
188 ok( exception { Dancer2Method->(undef) },
189 'Dancer2Method does not accept undef value',
190 );
191
192 is( exception { Dancer2HTTPMethod->('POST') }, undef, 'Dancer2HTTPMethod', );
193
194 like(
195 exception { Dancer2HTTPMethod->('post') },
196 qr{post.+did not pass type constraint.+Dancer2HTTPMethod},
197 'Dancer2HTTPMethod fail',
198 );
199
200 ok( exception { Dancer2HTTPMethod->(undef) },
201 'Dancer2Method does not accept undef value',
202 );
203
204 use Dancer2::Core::Error;
205 use Dancer2::Core::Hook;
206
207 ok( exception { Hook->(undef) }, 'Hook does not accept undef value' );
208
209 ok(exception { Hook->(Dancer2::Core::Error->new) },
210 'Hook does not Core::Error as value');
211
212 is( exception {
213 Hook->(Dancer2::Core::Hook->new(name => 'test', code => sub { }))
214 },
215 undef,
216 'Hook',
217 );
218
219 is(exception { ReadableFilePath->('t') }, undef, 'ReadableFilePath');
220
221 like(
222 exception { ReadableFilePath->('nosuchdirectory') },
223 qr/Value "nosuchdirectory" did not pass type constraint "ReadableFilePath"/,
224 'ReadableFilePath'
225 );