1Test::Type(3) User Contributed Perl Documentation Test::Type(3)
2
3
4
6 Test::Type - Functions to validate data types in test files.
7
9 Version 1.3.0
10
12 use Test::Type;
13
14 # Test strings.
15 ok_string( $variable );
16 ok_string(
17 $variable,
18 name => 'My variable',
19 );
20
21 # Test arrayrefs.
22 ok_arrayref( $variable );
23 ok_arrayref(
24 $variable,
25 name => 'My variable',
26 );
27
28 # Test hashrefs.
29 ok_hashref( $variable );
30 ok_hashref(
31 $variable,
32 name => 'Test variable',
33 );
34
35 # Test coderefs.
36 ok_coderef( $variable );
37 ok_coderef(
38 $variable,
39 name => 'Test variable',
40 );
41
42 # Test numbers.
43 ok_number( $variable );
44 ok_number(
45 $variable,
46 name => 'Test variable',
47 );
48
49 # Test instances.
50 ok_instance(
51 $variable,
52 class => $class,
53 );
54 ok_instance(
55 $variable,
56 name => 'Test variable',
57 class => $class,
58 );
59
60 # Test regular expressions.
61 ok_regex( $variable );
62 ok_regex(
63 $variable,
64 name => 'Test regular expression',
65 );
66
68 ok_string()
69 Test if the variable passed is a string.
70
71 ok_string(
72 $variable,
73 );
74
75 ok_string(
76 $variable,
77 name => 'My variable',
78 );
79
80 ok_string(
81 $variable,
82 name => 'My variable',
83 allow_empty => 1,
84 );
85
86 Parameters:
87
88 • name
89
90 Optional, the name of the variable being tested.
91
92 • allow_empty
93
94 Boolean, default 1. Allow the string to be empty or not.
95
96 ok_arrayref()
97 Test if the variable passed is an arrayref that can be dereferenced
98 into an array.
99
100 ok_arrayref( $variable );
101
102 ok_arrayref(
103 $variable,
104 name => 'My variable',
105 );
106
107 ok_arrayref(
108 $variable,
109 allow_empty => 1,
110 no_blessing => 0,
111 );
112
113 # Check if the variable is an arrayref of hashrefs.
114 ok_arrayref(
115 $variable,
116 allow_empty => 1,
117 no_blessing => 0,
118 element_validate_type =>
119 sub
120 {
121 return Data::Validate::Type::is_hashref( $_[0] );
122 },
123 );
124
125 Parameters:
126
127 • name
128
129 Optional, the name of the variable being tested.
130
131 • allow_empty
132
133 Boolean, default 1. Allow the array to be empty or not.
134
135 • no_blessing
136
137 Boolean, default 0. Require that the variable is not blessed.
138
139 • element_validate_type
140
141 None by default. Set it to a coderef to validate the elements in
142 the array. The coderef will be passed the element to validate as
143 first parameter, and it must return a boolean indicating whether
144 the element was valid or not.
145
146 ok_hashref()
147 Test if the variable passed is a hashref that can be dereferenced into
148 a hash.
149
150 ok_hashref( $variable );
151
152 ok_hashref(
153 $variable,
154 name => 'Test variable',
155 );
156
157 ok_hashref(
158 $variable,
159 allow_empty => 1,
160 no_blessing => 0,
161 );
162
163 Parameters:
164
165 • name
166
167 Optional, the name of the variable being tested.
168
169 • allow_empty
170
171 Boolean, default 1. Allow the array to be empty or not.
172
173 • no_blessing
174
175 Boolean, default 0. Require that the variable is not blessed.
176
177 ok_coderef()
178 Test if the variable passed is an coderef that can be dereferenced into
179 a block of code.
180
181 ok_coderef( $variable );
182
183 ok_coderef(
184 $variable,
185 name => 'Test variable',
186 );
187
188 Parameters:
189
190 • name
191
192 Optional, the name of the variable being tested.
193
194 ok_number()
195 Test if the variable passed is a number.
196
197 ok_number( $variable );
198
199 ok_number(
200 $variable,
201 name => 'Test variable',
202 );
203
204 ok_number(
205 $variable,
206 positive => 1,
207 );
208
209 ok_number(
210 $variable,
211 strictly_positive => 1,
212 );
213
214 Parameters:
215
216 • name
217
218 Optional, the name of the variable being tested.
219
220 • strictly_positive
221
222 Boolean, default 0. Set to 1 to check for a strictly positive
223 number.
224
225 • positive
226
227 Boolean, default 0. Set to 1 to check for a positive number.
228
229 ok_instance()
230 Test if the variable is an instance of the given class.
231
232 Note that this handles inheritance properly, so it will succeed if the
233 variable is an instance of a subclass of the class given.
234
235 ok_instance(
236 $variable,
237 class => $class,
238 );
239
240 ok_instance(
241 $variable,
242 name => 'Test variable',
243 class => $class,
244 );
245
246 Parameters:
247
248 • name
249
250 Optional, the name of the variable being tested.
251
252 • class
253
254 Required, the name of the class to check the variable against.
255
256 ok_regex()
257 Test if the variable is a regular expression.
258
259 ok_regex( $variable );
260
262 Please report any bugs or feature requests to
263 "bug-test-dist-versionsync at rt.cpan.org", or through the web
264 interface at
265 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=test-type>. I will be
266 notified, and then you'll automatically be notified of progress on your
267 bug as I make changes.
268
270 You can find documentation for this module with the perldoc command.
271
272 perldoc Test::Type
273
274 You can also look for information at:
275
276 • GitHub (report bugs there)
277
278 <https://github.com/guillaumeaubert/Test-Type/issues>
279
280 • AnnoCPAN: Annotated CPAN documentation
281
282 <http://annocpan.org/dist/test-type>
283
284 • CPAN Ratings
285
286 <http://cpanratings.perl.org/d/test-type>
287
288 • Search CPAN
289
290 <https://metacpan.org/release/Test-Type>
291
293 Guillaume Aubert <https://metacpan.org/author/AUBERTG>, "<aubertg at
294 cpan.org>".
295
297 Copyright 2012-2017 Guillaume Aubert.
298
299 This code is free software; you can redistribute it and/or modify it
300 under the same terms as Perl 5 itself.
301
302 This program is distributed in the hope that it will be useful, but
303 WITHOUT ANY WARRANTY; without even the implied warranty of
304 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE
305 file for more details.
306
307
308
309perl v5.32.1 2021-01-27 Test::Type(3)