File Coverage

blib/lib/Test/Data/Scalar.pm
Criterion Covered Total %
statement 87 122 71.3
branch 29 52 55.7
condition 15 42 35.7
subroutine 17 22 77.2
pod 18 18 100.0
total 166 256 64.8


line stmt bran cond sub pod time code
1             package Test::Data::Scalar;
2 2     2   12 use strict;
  2         4  
  2         56  
3              
4 2     2   9 use Exporter qw(import);
  2         3  
  2         159  
5              
6             our @EXPORT = qw(
7             blessed_ok defined_ok dualvar_ok greater_than length_ok
8             less_than maxlength_ok minlength_ok number_ok
9             readonly_ok ref_ok ref_type_ok strong_ok tainted_ok
10             untainted_ok weak_ok undef_ok number_between_ok
11             string_between_ok
12             );
13              
14             our $VERSION = '1.242';
15              
16 2     2   11 use Scalar::Util;
  2         3  
  2         82  
17 2     2   9 use Test::Builder;
  2         3  
  2         2994  
18              
19             my $Test = Test::Builder->new();
20              
21             =encoding utf8
22              
23             =head1 NAME
24              
25             Test::Data::Scalar -- test functions for scalar variables
26              
27             =head1 SYNOPSIS
28              
29             use Test::Data qw(Scalar);
30              
31             =head1 DESCRIPTION
32              
33             This modules provides a collection of test utilities for
34             scalar variables. Load the module through Test::Data.
35              
36             =head2 Functions
37              
38             =over 4
39              
40             =item blessed_ok( SCALAR )
41              
42             Ok if the SCALAR is a blessed reference.
43              
44             =cut
45              
46             sub blessed_ok ($;$) {
47 9     9 1 26796 my $ref = ref $_[0];
48 9         31 my $ok = Scalar::Util::blessed($_[0]);
49 9   50     44 my $name = $_[1] || 'Scalar is blessed';
50              
51 9 100       45 $Test->diag("Expected a blessed value, but didn't get it\n\t" .
52             qq|Reference type is "$ref"\n| ) unless $ok;
53              
54 9         1942 $Test->ok( $ok, $name );
55             }
56              
57             =item defined_ok( SCALAR )
58              
59             Ok if the SCALAR is defined.
60              
61             =cut
62              
63             sub defined_ok ($;$) {
64 2     2 1 6868 my $ok = defined $_[0];
65 2   50     11 my $name = $_[1] || 'Scalar is defined';
66              
67 2 100       9 $Test->diag("Expected a defined value, got an undefined one\n", $name )
68             unless $ok;
69              
70 2         211 $Test->ok( $ok, $name );
71             }
72              
73             =item undef_ok( SCALAR )
74              
75             Ok if the SCALAR is undefined.
76              
77             =cut
78              
79             sub undef_ok ($;$) {
80 6   50 6 1 17390 my $name = $_[1] || 'Scalar is undefined';
81              
82 6 50       20 if( @_ > 0 ) {
83 6         9 my $ok = not defined $_[0];
84              
85 6 100       21 $Test->diag("Expected an undefined value, got a defined one\n")
86             unless $ok;
87              
88 6         891 $Test->ok( $ok, $name );
89             }
90             else {
91 0         0 $Test->diag("Expected an undefined value, but got no arguments\n");
92              
93 0         0 $Test->ok( 0, $name );
94             }
95             }
96              
97             =item dualvar_ok( SCALAR )
98              
99             Ok if the scalar is a dualvar.
100              
101             How do I test this?
102              
103             sub dualvar_ok ($;$)
104             {
105             my $ok = Scalar::Util::dualvar( $_[0] );
106             my $name = $_[1] || 'Scalar is a dualvar';
107              
108             $Test->ok( $ok, $name );
109              
110             $Test->diag("Expected a dualvar, didn't get it\n")
111             unless $ok;
112             }
113              
114             =cut
115              
116             =item greater_than( SCALAR, BOUND )
117              
118             Ok if the SCALAR is numerically greater than BOUND.
119              
120             =cut
121              
122             sub greater_than ($$;$) {
123 8     8 1 32598 my $value = shift;
124 8         21 my $bound = shift;
125 8   50     56 my $name = shift || 'Scalar is greater than bound';
126              
127 8         21 my $ok = $value > $bound;
128              
129 8 100       54 $Test->diag("Number is less than the bound.\n\t" .
130             "Expected a number greater than [$bound]\n\t" .
131             "Got [$value]\n") unless $ok;
132              
133 8         1460 $Test->ok( $ok, $name );
134             }
135              
136             =item length_ok( SCALAR, LENGTH )
137              
138             Ok if the length of SCALAR is LENGTH.
139              
140             =cut
141              
142             sub length_ok ($$;$) {
143 19     19 1 98991 my $string = shift;
144 19         59 my $length = shift;
145 19   50     120 my $name = shift || 'Scalar has right length';
146              
147 19         57 my $actual = length $string;
148 19         48 my $ok = $length == $actual;
149              
150 19 100       146 $Test->diag("Length of value not within bounds\n\t" .
151             "Expected length=[$length]\n\t" .
152             "Got [$actual]\n") unless $ok;
153              
154 19         5570 $Test->ok( $ok, $name );
155             }
156              
157             =item less_than( SCALAR, BOUND )
158              
159             Ok if the SCALAR is numerically less than BOUND.
160              
161             =cut
162              
163             sub less_than ($$;$) {
164 8     8 1 32736 my $value = shift;
165 8         18 my $bound = shift;
166 8   50     50 my $name = shift || 'Scalar is less than bound';
167              
168 8         17 my $ok = $value < $bound;
169              
170 8 100       50 $Test->diag("Number is greater than the bound.\n\t" .
171             "Expected a number less than [$bound]\n\t" .
172             "Got [$value]\n") unless $ok;
173              
174 8         1184 $Test->ok( $ok, $name );
175             }
176              
177             =item maxlength_ok( SCALAR, LENGTH )
178              
179             Ok is the length of SCALAR is less than or equal to LENGTH.
180              
181             =cut
182              
183             sub maxlength_ok($$;$) {
184 8     8 1 2982 my $string = shift;
185 8         23 my $length = shift;
186 8   50     42 my $name = shift || 'Scalar length is less than bound';
187              
188 8         23 my $actual = length $string;
189 8         19 my $ok = $actual <= $length;
190              
191 8 50       26 $Test->diag("Length of value longer than expected\n\t" .
192             "Expected max=[$length]\n\tGot [$actual]\n") unless $ok;
193              
194 8         34 $Test->ok( $ok, $name );
195             }
196              
197             =item minlength_ok( SCALAR, LENGTH )
198              
199             Ok is the length of SCALAR is greater than or equal to LENGTH.
200              
201             =cut
202              
203             sub minlength_ok($$;$) {
204 12     12 1 4007 my $string = shift;
205 12         23 my $length = shift;
206 12   50     79 my $name = shift || 'Scalar length is greater than bound';
207              
208 12         29 my $actual = length $string;
209 12         28 my $ok = $actual >= $length;
210              
211 12 50       51 $Test->diag("Length of value shorter than expected\n\t" .
212             "Expected min=[$length]\n\tGot [$actual]\n") unless $ok;
213              
214 12         46 $Test->ok( $ok, $name );
215             }
216              
217             =item number_ok( SCALAR )
218              
219             Ok if the SCALAR is a number ( or a string that represents a
220             number ).
221              
222             At the moment, a number is just a string of digits. This needs
223             work.
224              
225             =cut
226              
227             sub number_ok($;$) {
228 0     0 1 0 my $number = shift;
229 0   0     0 my $name = shift || 'Scalar is a number';
230              
231 0 0       0 $number =~ /\D/ ? $Test->ok( 0, $name ) : $Test->ok( 1, $name );
232             }
233              
234             =item number_between_ok( SCALAR, LOWER, UPPER )
235              
236             Ok if the number in SCALAR sorts between the number
237             in LOWER and the number in UPPER, numerically.
238              
239             If you put something that isn't a number into UPPER or
240             LOWER, Perl will try to make it into a number and you
241             may get unexpected results.
242              
243             =cut
244              
245             sub number_between_ok($$$;$) {
246 5     5 1 9622 my $number = shift;
247 5         13 my $lower = shift;
248 5         10 my $upper = shift;
249 5   50     28 my $name = shift || 'Scalar is in numerical range';
250              
251 5 100 33     65 unless( defined $lower and defined $upper ) {
    50          
    50          
252 0         0 $Test->diag("You need to define LOWER and UPPER bounds " .
253             "to use number_between_ok" );
254 0         0 $Test->ok( 0, $name );
255             }
256 0         0 elsif( $upper < $lower ) {
257 0         0 $Test->diag(
258             "Upper bound [$upper] is lower than lower bound [$lower]" );
259 0         0 $Test->ok( 0, $name );
260             }
261 0 100       0 elsif( $number >= $lower and $number <= $upper ) {
262 4         18 $Test->ok( 1, $name );
263             }
264             else {
265 1         12 $Test->diag( "Number [$number] was not within bounds\n",
266             "\tExpected lower bound [$lower]\n",
267             "\tExpected upper bound [$upper]\n" );
268 1         389 $Test->ok( 0, $name );
269             }
270             }
271              
272             =item string_between_ok( SCALAR, LOWER, UPPER )
273              
274             Ok if the string in SCALAR sorts between the string
275             in LOWER and the string in UPPER, ASCII-betically.
276              
277             =cut
278              
279             sub string_between_ok($$$;$) {
280 6     6 1 11492 my $string = shift;
281 6         16 my $lower = shift;
282 6         12 my $upper = shift;
283 6   50     36 my $name = shift || 'Scalar is in string range';
284              
285 6 100 33     59 unless( defined $lower and defined $upper ) {
    50          
    50          
286 0         0 $Test->diag("You need to define LOWER and UPPER bounds " .
287             "to use string_between_ok" );
288 0         0 $Test->ok( 0, $name );
289             }
290 0         0 elsif( $upper lt $lower ) {
291 0         0 $Test->diag(
292             "Upper bound [$upper] is lower than lower bound [$lower]" );
293 0         0 $Test->ok( 0, $name );
294             }
295 0 50       0 elsif( $string ge $lower and $string le $upper ) {
296 5         22 $Test->ok( 1, $name );
297             }
298             else {
299 1         12 $Test->diag( "String [$string] was not within bounds\n",
300             "\tExpected lower bound [$lower]\n",
301             "\tExpected upper bound [$upper]\n" );
302 1         391 $Test->ok( 0, $name );
303             }
304              
305             }
306              
307             =item readonly_ok( SCALAR )
308              
309             Ok is the SCALAR is read-only.
310              
311             =cut
312              
313             sub readonly_ok($;$) {
314 0     0 1 0 my $ok = not Scalar::Util::readonly( $_[0] );
315 0   0     0 my $name = $_[1] || 'Scalar is read-only';
316              
317 0 0       0 $Test->diag("Expected readonly reference, got writeable one\n")
318             unless $ok;
319              
320 0         0 $Test->ok( $ok, $name );
321             }
322              
323             =item ref_ok( SCALAR )
324              
325             Ok if the SCALAR is a reference.
326              
327             =cut
328              
329             sub ref_ok($;$) {
330 2     2 1 5620 my $ok = ref $_[0];
331 2   50     13 my $name = $_[1] || 'Scalar is a reference';
332              
333 2 50       10 $Test->diag("Expected reference, didn't get it\n")
334             unless $ok;
335              
336 2         10 $Test->ok( $ok, $name );
337             }
338              
339             =item ref_type_ok( REF1, REF2 )
340              
341             Ok if REF1 is the same reference type as REF2.
342              
343             =cut
344              
345             sub ref_type_ok($$;$) {
346 0     0 1 0 my $ref1 = ref $_[0];
347 0         0 my $ref2 = ref $_[1];
348 0         0 my $ok = $ref1 eq $ref2;
349 0   0     0 my $name = $_[2] || 'Scalar is right reference type';
350              
351 0 0       0 $Test->diag("Expected references to match\n\tGot $ref1\n\t" .
352             "Expected $ref2\n") unless $ok;
353              
354 0 0       0 ref $_[0] eq ref $_[1] ? $Test->ok( 1, $name ) : $Test->ok( 0, $name );
355             }
356              
357             =item strong_ok( SCALAR )
358              
359             Ok is the SCALAR is not a weak reference.
360              
361             =cut
362              
363             sub strong_ok($;$) {
364 2     2 1 702 my $ok = not Scalar::Util::isweak( $_[0] );
365 2   50     12 my $name = $_[1] || 'Scalar is not a weak reference';
366              
367 2 50       10 $Test->diag("Expected strong reference, got weak one\n")
368             unless $ok;
369              
370 2         8 $Test->ok( $ok, $name );
371             }
372              
373             =item tainted_ok( SCALAR )
374              
375             Ok is the SCALAR is tainted.
376              
377             (Tainted values may seem like a not-Ok thing, but remember, when
378             you use taint checking, you want Perl to taint data, so you
379             should have a test to make sure it happens.)
380              
381             =cut
382              
383             sub tainted_ok($;$) {
384 0     0 1 0 my $ok = Scalar::Util::tainted( $_[0] );
385 0   0     0 my $name = $_[1] || 'Scalar is tainted';
386              
387 0 0       0 $Test->diag("Expected tainted data, got untainted data\n")
388             unless $ok;
389              
390 0         0 $Test->ok( $ok, $name );
391             }
392              
393             =item untainted_ok( SCALAR )
394              
395             Ok if the SCALAR is not tainted.
396              
397             =cut
398              
399             sub untainted_ok($;$) {
400 1     1 1 5373 my $ok = not Scalar::Util::tainted( $_[0] );
401 1   50     9 my $name = $_[1] || 'Scalar is not tainted';
402              
403 1 50       4 $Test->diag("Expected untainted data, got tainted data\n")
404             unless $ok;
405              
406 1         5 $Test->ok( $ok, $name );
407             }
408              
409             =item weak_ok( SCALAR )
410              
411             Ok if the SCALAR is a weak reference.
412              
413             =cut
414              
415             sub weak_ok($;$) {
416 0     0 1   my $ok = Scalar::Util::isweak( $_[0] );
417 0   0       my $name = $_[1] || 'Scalar is a weak reference';
418              
419 0 0         $Test->diag("Expected weak reference, got stronge one\n")
420             unless $ok;
421              
422 0           $Test->ok( $ok, $name );
423             }
424              
425             =back
426              
427             =head1 TO DO
428              
429             * add is_a_filehandle test
430              
431             * add is_vstring test
432              
433             =head1 SEE ALSO
434              
435             L,
436             L,
437             L,
438             L,
439             L,
440             L
441              
442             =head1 SOURCE AVAILABILITY
443              
444             This source is in Github:
445              
446             https://github.com/briandfoy/test-data
447              
448             =head1 AUTHOR
449              
450             brian d foy, C<< >>
451              
452             =head1 COPYRIGHT AND LICENSE
453              
454             Copyright © 2002-2018, brian d foy . All rights reserved.
455              
456             This program is free software; you can redistribute it and/or modify
457             it under the terms of the Artistic License 2.0.
458              
459             =cut
460              
461              
462             "The quick brown fox jumped over the lazy dog";