File Coverage

blib/lib/Test/Data/Scalar.pm
Criterion Covered Total %
statement 89 128 69.5
branch 29 54 53.7
condition 15 44 34.0
subroutine 18 24 75.0
pod 19 19 100.0
total 170 269 63.2


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