tlib/Test/Tech.pm

filter: Statementstmt 100% 23 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 100% 31 / 31 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Net/Netid.pm ↑ Summary
1 #!perl
2 #
3 # Documentation, copyright and license is at the end of this file.
4 #
5 package Test::Tech;
6
7 1 ▶use 5.001;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 1 ▶use Test (); # do not import the "Test" subroutines
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 1 ▶use vars qw($VERSION $DATE $FILE);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 $VERSION = '1.11';
17 $DATE = '2003/07/27';
18 $FILE = __FILE__;
19
20 1 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 require Exporter;
22 @ISA=('Exporter');
23 @EXPORT_OK = qw(&tech_config &plan &ok &skip &skip_tests &stringify &demo);
24
25 #######
26 #
27 # Keep all data hidden in a local hash
28 #
29 # Too bad "Test" and "Data::Dumper" are not objectified
30 #
31 # Senseless to objectify "Test::Tech" if unless "Test" and "Data::Dumper"
32 # are objectified
33 #
34
35 my %tech = ();
36 my $tech_p = \%tech; # quasi objectify by using $tech_p instead of %tech
37
38 ########
39 # Tend to Data::Dumper variables
40 #
41 $tech_p->{Dumper} = {};
42 $tech_p->{Dumper}->{Terse} = \$Data::Dumper::Terse;
43 $tech_p->{Dumper}->{Indent} = \$Data::Indent;
44 $tech_p->{Dumper}->{Purity} = \$Data::Purity;
45 $tech_p->{Dumper}->{Pad} = \$Data::Pad;
46 $tech_p->{Dumper}->{Varname} = \$Data::Varname;
47 $tech_p->{Dumper}->{Useqq} = \$Data::Useqq;
48 $tech_p->{Dumper}->{Freezer} = \$Data::Freezer;
49 $tech_p->{Dumper}->{Toaster} = \$Data::Toaster;
50 $tech_p->{Dumper}->{Deepcopy} = \$Data::Deepcopy;
51 $tech_p->{Dumper}->{Quotekeys} = \$Data::Quotekeys;
52 $tech_p->{Dumper}->{Maxdepth} = \$Data::Maxdepth;
53
54 ######
55 # Tend to Test variables
56 #
57 $tech_p->{Test}->{ntest} = \$Test::ntest;
58 $tech_p->{Test}->{TESTOUT} = \$Test::TESTOUT;
59 $tech_p->{Test}->{TestLevel} = \$Test::TestLevel;
60 $tech_p->{Test}->{ONFAIL} = \$Test::ONFAIL;
61 $tech_p->{Test}->{todo} = \%Test::todo;
62 $tech_p->{Test}->{history} = \%Test::history;
63 $tech_p->{Test}->{planned} = \$Test::planned;
64 $tech_p->{Test}->{FAILDETAIL} = \@Test::FAILDETAIL;
65 $tech_p->{Test}->{Program_Lines} = \%Test::Program_Lines if defined %Test::Program_lines;
66 $tech_p->{Test}->{TESTERR} = \$Test::TESTERR if defined $Test::TESTERR;
67 $tech_p->{Skip_Tests} = 0;
68
69 #######
70 # Probe for internal storage
71 #
72 # The &Data::Dumper::Dumper subroutine stringifies the iternal Perl variable.
73 # Different Perls keep the have different internal formats for numbers. Some
74 # keep them as binary numbers, while others as strings. The ones that keep
75 # them as strings may be well spec. In any case they have been let loose in
76 # the wild so the test scripts that use Data::Dumper must deal with them.
77 #
78 # This is perl, v5.6.1 built for MSWin32-x86-multi-thread
79 # (with 1 registered patch, see perl -V for more detail)
80 #
81 # Copyright 1987-2001, Larry Wall
82 #
83 # Binary build 631 provided by ActiveState Tool Corp. http://www.ActiveState.com
84 # Built 17:16:22 Jan 2 2002
85 #
86 #
87 # Perl may be copied only under the terms of either the Artistic License or the
88 # GNU General Public License, which may be found in the Perl 5 source kit.
89 #
90 # Complete documentation for Perl, including FAQ lists, should be found on
91 # this system using `man perl' or `perldoc perl'. If you have access to the
92 # Internet, point your browser at http://www.perl.com/, the Perl Home Page.
93 #
94 # ~~~~~~~
95 #
96 # Wall, Christiansen and Orwant on Perl internal storage
97 #
98 # Page 351 of Programming Perl, Third Addition, Overloadable Operators
99 # quote:
100 #
101 # Conversion operators: ``'', 0+, bool
102 #
103 # These three keys let you provide behaviors for Perl's automatic conversions
104 # to strings, numbers, and Boolean values, respectively.
105 #
106 # ~~~~~~~
107 #
108 # Internal Storage of Perls that are in the wild
109 #
110 # string - Perl v5.6.1 MSWin32-x86-multi-thread, ActiveState build 631, binary
111 # number - Perl version 5.008 for solaris
112 #
113 # Perls in the wild with internal storage of string may be mutants that need to
114 # be hunted down killed.
115 #
116 my $probe = 3;
117 my $actual = Dumper([0+$probe]);
118 if( $actual eq Dumper([3]) ) {
119 $tech_p->{Internal_Number} = 'number';
120 }
121 elsif ( $actual eq Dumper(['3']) ) {
122 $tech_p->{Internal_Number} = 'string';
123 }
124 else {
125 $tech_p->{Internal_Number} = 'undetermine';
126 }
127
128
129 #####
130 # Stringify the variable and compare the string.
131 #
132 # This is the code that adds the big new capability of testing complex data
133 # structures to the "Test" module
134 #
135 sub stringify
136 {
137 my ($var_p) = @_;
138
139 return '' unless $var_p;
140
141 my ($result, $ref);
142 if($ref = ref($var_p)) {
143 if( $ref eq 'ARRAY' ) {
144 if( 1 < @$var_p ) {
145 $result = Dumper(@$var_p);
146 }
147 else {
148 $result = shift @$var_p;
149 }
150 }
151 elsif( $ref eq 'HASH' ) {
152 $result = Dumper(%$var_p);
153 }
154 else {
155 $result = Dumper($var_p);
156 }
157 }
158 else {
159 $result = $var_p;
160 }
161 $result;
162 }
163
164
165
166 ######
167 # Cover function for &Test::plan that sets the proper 'Test::TestLevel'
168 # and outputs some info on the current site
169 #
170 sub plan
171 {
172 &Test::plan( @_ );
173
174 ###############
175 #
176 # Establish default for Test and Data::Dumper
177 #
178 # Test 1.24 resets global variables in plan which
179 # never happens in 1.15
180 #
181 $Data::Dumper::Terse = 1;
182 $Test::TestLevel = 1;
183
184 my $loctime = localtime();
185 my $gmtime = gmtime();
186
187 my $perl = "$]";
188 if(defined(&Win32::BuildNumber) and defined &Win32::BuildNumber()) {
189 $perl .= " Win32 Build " . &Win32::BuildNumber();
190 }
191 elsif(defined $MacPerl::Version) {
192 $perl .= " MacPerl version " . $MacPerl::Version;
193 }
194
195 print $Test::TESTOUT <<"EOF" unless 1.20 < $Test::VERSION ;
196 # OS : $^O
197 # Perl : $perl
198 # Local Time : $loctime
199 # GMT Time : $gmtime
200 # Test : $Test::VERSION
201 EOF
202
203 print $Test::TESTOUT <<"EOF";
204 # Number Storage: $tech_p->{Internal_Number}
205 # Test::Tech : $VERSION
206 # Data::Dumper : $Data::Dumper::VERSION
207 # =cut
208 EOF
209
210 1
211 }
212
213
214 ######
215 #
216 # Cover function for &Test::ok that adds capability to test
217 # complex data structures.
218 #
219 sub ok
220 {
221 my ($actual_result, $expected_result, $diagnostic, $name) = @_;
222
223 print $Test::TESTOUT "# $name\n" if $name;
224 if($tech_p->{Skip_Tests}) { # skip rest of tests switch
225 print $Test::TESTOUT "# Test invalid because of previous failure.\n";
226 &Test::skip( 1, 0, '');
227 return 1;
228 }
229
230 &Test::ok(stringify($actual_result), stringify($expected_result), $diagnostic);
231 }
232
233
234 ######
235 #
236 #
237 sub skip
238 {
239 my ($mod, $actual_result, $expected_result, $diagnostic, $name) = @_;
240
241 print $Test::TESTOUT "# $name\n" if $name;
242
243 if($tech_p->{Skip_Tests}) { # skip rest of tests switch
244 print $Test::TESTOUT "# Test invalid because of previous failure.\n";
245 &Test::skip( 1, 0, '');
246 return 1;
247 }
248
249 &Test::skip($mod, stringify($actual_result), stringify($expected_result), $diagnostic);
250
251 }
252
253
254 ######
255 #
256 #
257 sub skip_tests
258 {
259 my ($value) = @_;
260 my $result = $tech_p->{Skip_Tests};
261 $tech_p->{Skip_Tests} = $value if defined $value;
262 $result;
263 }
264
265
266
267 #######
268 # This accesses the values in the %tech hash
269 #
270 # Use a dot notation for following down layers
271 # of hashes of hashes
272 #
273 sub tech_config
274 {
275 my ($key, @values) = @_;
276 my @keys = split /\./, $key;
277
278 #########
279 # Follow the hash with the current
280 # dot index until there are no more
281 # hashes. Hopefully the dot hash
282 # notation matches the structure.
283 #
284 my $key_p = $tech_p;
285 while (@keys) {
286
287 $key = shift @keys;
288
289 ######
290 # Do not allow creation of new configs
291 #
292 if( defined( $key_p->{$key}) ) {
293
294 ########
295 # Follow the hash
296 #
297 if( ref($key_p->{$key}) eq 'HASH' ) {
298 $key_p = $key_p->{$key};
299 }
300 else {
301 if(@keys) {
302 warn( "More key levels than hashes.\n");
303 return undef;
304 }
305 last;
306 }
307 }
308 }
309
310
311 #########
312 # References to arrays and scalars in the config may
313 # be transparent.
314 #
315 my $current_value = $key_p->{$key};
316 return $current_value if ref($current_value) eq 'HASH';
317 if (defined $values[0]) {
318 if(ref($key_p->{$key}) eq 'ARRAY') {
319 if( ref($values[0]) eq 'ARRAY' ) {
320 $key_p->{$key} = $values[0];
321 }
322 else {
323 my @current_value = @{$key_p->{$key}};
324 $key_p->{$key} = \@values;
325 return @current_value;
326 }
327 }
328 elsif( ref($key_p->{$key}) ) {
329 $current_value = ${$key_p->{$key}};
330 ${$key_p->{$key}} = $values[0];
331 }
332 else {
333 $key_p->{$key} = $values[0];
334 }
335 }
336
337 $current_value;
338
339 }
340
341
342
343 ######
344 # Demo
345 #
346 sub demo
347 {
348 my ($quoted_expression, @expression_results) = @_;
349
350 #######
351 # A demo trys to simulate someone typing expresssions
352 # at a console.
353 #
354
355 #########
356 # Print quoted expression so that see the non-executed
357 # expression. The extra space is so when pasted into
358 # a POD, the POD will process the line as code.
359 #
360 $quoted_expression =~ s/(\n+)/$1 => /g;
361 print $Test::TESTOUT ' => ' . $quoted_expression . "\n";
362
363 ########
364 # @data is the result of the script executing the
365 # quoted expression.
366 #
367 # The demo output most likely will end up in a pod.
368 # The the process of running the generated script
369 # will execute the setup. Thus the input is the
370 # actual results. Putting a space in front of it
371 # tells the POD that it is code.
372 #
373 return unless @expression_results;
374
375 $Data::Dumper::Terse = 1;
376 my $data = Dumper(@expression_results);
377 $data =~ s/(\n+)/$1 /g;
378 $data =~ s/\\\\/\\/g;
379 $data =~ s/\\'/'/g;
380
381 print $Test::TESTOUT ' ' . $data . "\n" ;
382
383 }
384
385 1
386
387
388 __END__
« blib/lib/Net/Netid.pm ↑ Summary