File Coverage

blib/lib/Fred/Fish/DBUG/Test.pm
Criterion Covered Total %
statement 83 112 74.1
branch 2 22 9.0
condition 2 10 20.0
subroutine 16 17 94.1
pod 10 10 100.0
total 113 171 66.0


line stmt bran cond sub pod time code
1             ###
2             ### Copyright (c) 2024 - 2025 Curtis Leach. All rights reserved.
3             ###
4             ### Based on the Fred Fish DBUG macros in C/C++.
5             ### This Algorithm is in the public domain!
6             ###
7             ### Module: Fred::Fish::DBUG::Test
8              
9             =head1 NAME
10              
11             Fred::Fish::DBUG::Test - Fred Fish library extension to Test::More
12              
13             =head1 SYNOPSIS
14              
15             use Fred::Fish::DBUG::Test;
16             or
17             require Fred::Fish::DBUG::Test;
18              
19             =head1 DESCRIPTION
20              
21             F is an extension to the Fred Fish DBUG module that
22             allows your test programs to write L's output to your B logs
23             as well as your screen. Only for use by your module's test scripts. (t/*.t)
24              
25             So see L for more details on the supported functions below. Most
26             are not supported.
27              
28             Also be aware that if B has been used, you must source this module
29             after it to avoid problems.
30              
31             use threads;
32             use Fred::Fish::DBUG::Test;
33              
34             All functions return what the corresponding Test::More call does.
35              
36             =head1 FUNCTIONS
37              
38             =over 4
39              
40             =cut
41              
42             package Fred::Fish::DBUG::Test;
43              
44 33     33   4536666 use strict;
  33         73  
  33         1292  
45 33     33   152 use warnings;
  33         76  
  33         2528  
46              
47 33     33   221 use vars qw( @ISA @EXPORT @EXPORT_OK $VERSION );
  33         99  
  33         3091  
48 33     33   218 use Exporter;
  33         135  
  33         1858  
49              
50             # This Test module always assumes the Fish calls are live.
51 33     33   29685 use Fred::Fish::DBUG::ON;
  33         138  
  33         6937  
52              
53 33     33   415 use Test::More 0.88;
  33         1050  
  33         314  
54              
55             $VERSION = "2.10";
56             @ISA = qw( Exporter );
57              
58             @EXPORT = qw(
59             dbug_ok dbug_is dbug_isnt
60             dbug_like dbug_unlike dbug_cmp_ok
61             dbug_can_ok dbug_isa_ok dbug_new_ok
62             dbug_BAIL_OUT
63             );
64              
65              
66             # --------------------------------------------------------------------------
67              
68             sub _write_to_fish
69             {
70 1249     1249   2813 my $res = shift;
71 1249         2298 my $got = shift;
72 1249         2117 my $oper = shift;
73 1249         2052 my $expected = shift;
74 1249         2335 my $test_name = shift;
75 1249   100     7107 my $diag_flag = shift || 0;
76              
77 1249         8653 my $subName = (caller(1))[3]; # which func called me.
78 1249 50       14335 my $lbl = ($subName =~ m/:dbug_(.*)$/) ? $1 : 0;
79              
80 1249         7489 my $test = Test::Builder->new()->current_test();
81              
82 1249 50       340446 if ( $res ) {
83 1249         5438 DBUG_PRINT ("OK", "%d - %s() - [%s]", $test, $lbl, $test_name);
84             } else {
85 0         0 my @c = (caller(1))[1,2]; # Filename & line # of who called my caller.
86 0         0 my $line = " at $c[0] line $c[1].";
87 0 0       0 diag ( $line ) if ( $diag_flag );
88              
89 0 0       0 $got = (defined $got) ? "'${got}'" : "undef";
90 0 0       0 $oper = (defined $oper) ? "'${oper}'" : "undef";
91 0 0       0 $expected = (defined $expected) ? "'${expected}'" : "undef";
92              
93 0         0 my $msg1 = "";
94 0         0 my $msg2 = "";
95 0         0 my $msg3 = "";
96              
97 0 0 0     0 if ( $lbl eq "cmp_ok" ) {
    0 0        
    0          
98 0         0 $msg1 = sprintf ("#%12s: %s\n", "got", $got);
99 0         0 $msg2 = sprintf ("#%12s: %s\n", "operator", $oper);
100 0         0 $msg3 = sprintf ("#%12s: %s\n", "expected", $expected);
101             } elsif ( $lbl eq "is" || $lbl eq "isnt" ) {
102 0         0 $msg1 = sprintf ("#%12s: %s\n", "got", $got);
103 0         0 $msg3 = sprintf ("#%12s: %s\n", "expected", $expected);
104             } elsif ( $lbl eq "like" || $lbl eq "unlike" ) {
105 0         0 $msg1 = sprintf ("#%12s: %s\n", "got", $got);
106 0         0 $msg2 = sprintf ("#%12s: %s\n", "RegExpr", $oper);
107             }
108             # else - ok, can_ok, isa_ok, new_ok.
109              
110 0 0       0 if ( Fred::Fish::DBUG::ON::dbug_get_frame_value ("who_called") ) {
111 0         0 $line = ""; # Fish will automatically get the caller info itself.
112             } else {
113 0         0 $line = '#' . $line . "\n";
114             }
115              
116 0         0 DBUG_PRINT ("NOT OK", "%d - %s() - [%s]\n%s%s%s%s",
117             $test, $lbl, $test_name, $line, $msg1, $msg2, $msg3);
118             }
119              
120 1249         3524 return;
121             }
122              
123             # =============================================================================
124              
125             =item dbug_ok ( $status, $test_name )
126              
127             Writes the message to B and then calls Test::More::ok().
128              
129             =cut
130              
131             sub dbug_ok
132             {
133 844     844 1 8100012 my $status = shift;
134 844         1926 my $test_name = shift;
135              
136             # my $res = ok ( $status, $test_name );
137 844         5157 my $res = Test::Builder->new()->ok ( $status, $test_name );
138              
139 844         533856 _write_to_fish ($res, undef, undef, undef, $test_name, 0);
140              
141 844         8536 return ( $res );
142             }
143              
144             # =============================================================================
145              
146             =item dbug_is ( $got, $expected. $test_name )
147              
148             Writes the message to B and then calls Test::More::is().
149              
150             =cut
151              
152             sub dbug_is
153             {
154 385     385 1 4088 my $got = shift;
155 385         767 my $expected = shift;
156 385         805 my $test_name = shift;
157              
158             # my $res = is ( $got, $expected, $test_name );
159 385         2008 my $res = Test::Builder->new()->is_eq ( $got, $expected, $test_name );
160              
161 385         445271 _write_to_fish ($res, $got, undef, $expected, $test_name, 0);
162              
163 385         1469 return ( $res );
164             }
165             # =============================================================================
166              
167             =item dbug_isnt ( $got, $expected. $test_name )
168              
169             Writes the message to B and then calls Test::More::isnt().
170              
171             =cut
172              
173             sub dbug_isnt
174             {
175 3     3 1 18 my $got = shift;
176 3         7 my $expected = shift;
177 3         6 my $test_name = shift;
178              
179             # my $res = isnt ( $got, $expected, $test_name );
180 3         17 my $res = Test::Builder->new()->isnt_eq ( $got, $expected, $test_name );
181              
182 3         2498 _write_to_fish ($res, $got, undef, $expected, $test_name, 0);
183              
184 3         15 return ( $res );
185             }
186              
187             # =============================================================================
188              
189             =item dbug_like ( $got, $regexpr. $test_name )
190              
191             Writes the message to B and then calls Test::More::like().
192              
193             =cut
194              
195             sub dbug_like
196             {
197 1     1 1 10 my $got = shift;
198 1         3 my $regexpr = shift;
199 1         3 my $test_name = shift;
200              
201             # my $res = like ( $got, $regexpr, $test_name );
202 1         6 my $res = Test::Builder->new()->like ( $got, $regexpr, $test_name );
203              
204 1         826 _write_to_fish ($res, $got, $regexpr, undef, $test_name, 0);
205              
206 1         5 return ( $res );
207             }
208              
209             # =============================================================================
210              
211             =item dbug_unlike ( $got, $regexpr. $test_name )
212              
213             Writes the message to B and then calls Test::More::unlike().
214              
215             =cut
216              
217             sub dbug_unlike
218             {
219 1     1 1 12 my $got = shift;
220 1         95 my $regexpr = shift;
221 1         3 my $test_name = shift;
222              
223             # my $res = unlike ( $got, $regexpr, $test_name );
224 1         6 my $res = Test::Builder->new()->unlike ( $got, $regexpr, $test_name );
225              
226 1         820 _write_to_fish ($res, $got, $regexpr, undef, $test_name, 0);
227              
228 1         6 return ( $res );
229             }
230              
231             # =============================================================================
232              
233             =item dbug_cmp_ok ( $got, $op, $expected. $test_name )
234              
235             Writes the message to B and then calls Test::More::cmp_ok().
236              
237             =cut
238              
239             sub dbug_cmp_ok
240             {
241 12     12 1 207 my $got = shift;
242 12         38 my $op = shift;
243 12         24 my $expected = shift;
244 12         23 my $test_name = shift;
245              
246             # my $res = cmp_ok ( $got, $op, $expected, $test_name );
247 12         67 my $res = Test::Builder->new()->cmp_ok ( $got, $op, $expected, $test_name );
248              
249 12         12345 _write_to_fish ($res, $got, $op, $expected, $test_name, 0);
250              
251 12         410 return ( $res );
252             }
253              
254             # =============================================================================
255              
256             =item dbug_can_ok ( $module_or_object, @methods )
257              
258             Writes the message to B and then calls Test::More::can_ok().
259              
260             =cut
261              
262             sub dbug_can_ok
263             {
264 1     1 1 7 my $module = shift;
265 1         5 my @methods = @_;
266              
267 1         2 my $cnt = @methods;
268 1         4 my $test_name = "Testing existance of $cnt method(s).";
269              
270             # To complex to write my own version, so have to write additional "at" diag.
271 1         6 my $res = can_ok ( $module, @methods );
272              
273 1         566 _write_to_fish ($res, undef, undef, undef, $test_name, 1);
274              
275 1         6 return ( $res );
276             }
277              
278             # =============================================================================
279              
280             =item dbug_isa_ok ( $object, $class, $object_name )
281              
282             Writes the message to B and then calls Test::More::isa_ok().
283              
284             =cut
285              
286             sub dbug_isa_ok
287             {
288 1     1 1 9 my @opts = @_;
289              
290 1         5 my $test_name = join (", ", @opts);
291              
292             # To complex to write my own version, so have to write additional "at" diag.
293 1         8 my $res = isa_ok ( $opts[0], $opts[1], $opts[2] );
294              
295 1         544 _write_to_fish ($res, undef, undef, undef, $test_name, 1);
296              
297 1         5 return ( $res );
298             }
299              
300             # =============================================================================
301              
302             =item my $obj = dbug_new_ok ( $class, ... )
303              
304             Writes the message to B and then calls Test::More::new_ok().
305              
306             =cut
307              
308             sub dbug_new_ok
309             {
310 1     1 1 8 my @opts = @_;
311              
312 1         4 my $test_name = join (", ", @opts);
313              
314 1         6 my $obj = new_ok ( @opts );
315              
316             # To complex to write my own version, so have to write additional "at" diag.
317 1         619 _write_to_fish (defined $obj, undef, undef, undef, $test_name, 1);
318              
319 1         4 return ( $obj );
320             }
321              
322             # =============================================================================
323              
324             =item dbug_BAIL_OUT ( $message )
325              
326             Writes the message to B and then calls Test::More::done_testing() if
327             needed and then Test::More::BAIL_OUT($message) before terminating your test
328             script.
329              
330             =cut
331              
332             sub dbug_BAIL_OUT
333             {
334 0   0 0 1   my $msg = shift || "Unknown reason for bailing.";
335              
336 0           my $tb = Test::Builder->new ();
337 0           my $test = $tb->current_test ();
338 0           my $plan = $tb->expected_tests ();
339             # diag ( DBUG_PRINT ("test", "Current: %d, Planed: %s", $test, $plan) );
340              
341 0           DBUG_PRINT ("BAIL_OUT", "%s", $msg);
342 0 0         done_testing () if ($plan == 0);
343             # BAIL_OUT ( $msg );
344 0           Test::Builder->new()->BAIL_OUT ( $msg );
345 0           exit (255); # Should never get here.
346             }
347              
348             # =============================================================================
349              
350             # ---------------------------------------------------------------------------
351             # End of Fred::Fish::DBUG::Test ...
352             # ---------------------------------------------------------------------------
353              
354             =back
355              
356             =head1 CREDITS
357              
358             To Fred Fish for developing the basic algorithm and putting it into the
359             public domain! Any bugs in its implementation are purely my fault.
360              
361             =head1 SEE ALSO
362              
363             L - The controling module which you should be using to enable
364             this module.
365              
366             L - The live version of the DBUG module.
367              
368             L - The stub version of the DBUG module.
369              
370             L - Allows you to trap and log STDOUT/STDERR to B.
371              
372             L - Allows you to trap and log signals to B.
373              
374             L - Allows you to implement action
375             DBUG_SIG_ACTION_LOG for B. Really dangerous to use. Will break most
376             code bases.
377              
378             L - Sample code demonstrating using DBUG module.
379              
380             =head1 COPYRIGHT
381              
382             Copyright (c) 2024 - 2025 Curtis Leach. All rights reserved.
383              
384             This program is free software. You can redistribute it and/or modify it
385             under the same terms as Perl itself.
386              
387             =cut
388              
389             # ============================================================
390             #required if module is included w/ require command;
391             1;
392