6 |
|
# new($module,testdatadir) : module example - Utilities/urlhandler |
7 |
|
# dotest(@args) : Start testing - arguments dependent on inheriting class |
8 |
|
# |
9 |
+ |
# cmpstring(expectedstring,returnedstring) : cmp a string and fail if not equal |
10 |
|
# newtest() : Initiate a testing sequence |
11 |
|
# verify(actual_result_file, expected_result_file) : compare two files |
12 |
|
# verifydir(dir) : Check existence of the directory |
13 |
+ |
# verifyexists(file) : Verify the existence of file |
14 |
|
# datadir([dir]) : return the current data directory (set it to dir if supplied) |
15 |
< |
# ------------------- Private Methods ---------------------------------- |
15 |
> |
# testfail(string) : report that current test has failed |
16 |
> |
# testpass(string) : report that current test has passed |
17 |
|
# newfilename() : return a new filename that can be opened etc. |
18 |
|
# temparea() : return a directory for building temporary stuff |
19 |
|
# newobject(@args) : Set up a new object to be tested |
20 |
|
# testinterface($name,@args) : perform interface tests for $name with @args |
21 |
|
# expect(string) : tell the testinterface of any expected return values |
22 |
|
# clearexpect() : Reset any expect variables. |
23 |
+ |
#cleantemp() : delete the temporary area |
24 |
+ |
# cmparray(arrayref, @reqvals) : test the arrayref against expected |
25 |
+ |
# cmpstring(expectedstring,actualstring) : |
26 |
|
|
27 |
< |
package TestClass; |
27 |
> |
package Utilities::TestClass; |
28 |
|
require 5.004; |
29 |
|
$bold = "\033[1m"; |
30 |
|
$normal = "\033[0m"; |
58 |
|
$self->{class}=$module; |
59 |
|
$self->{"datadir"}=$datadir; |
60 |
|
$self->{filenumber}=0; |
61 |
< |
$self->{temparea}="/tmp/SCRAMtest"; |
61 |
> |
rmtree("/tmp/SCRAMtest++"); |
62 |
> |
$self->{temparea}="/tmp/SCRAMtest++"; |
63 |
|
use File::Path; |
64 |
|
mkpath ($self->{temparea},0, 0777); |
65 |
|
|
68 |
|
die $@ if $@; |
69 |
|
$self->{testobj}=$module->_new($self, $fullmodule); |
70 |
|
|
71 |
+ |
# make sure the temparea is cleaned |
72 |
+ |
use File::Path; |
73 |
+ |
|
74 |
|
return $self; |
75 |
|
} |
76 |
|
|
83 |
|
$self->{testobj}->checktests(); |
84 |
|
} |
85 |
|
|
86 |
+ |
sub cmparray { |
87 |
+ |
my $self=shift; |
88 |
+ |
my $array=shift; |
89 |
+ |
my @vals=@_; |
90 |
+ |
|
91 |
+ |
if ( $#{$array} ne $#vals) { $self->testfail( |
92 |
+ |
$#{$array}." items retuned, $#vals expected"); |
93 |
+ |
} |
94 |
+ |
else { |
95 |
+ |
for( my $i=0; $i<= $#{$array}; $i++) { |
96 |
+ |
$self->cmpstring($vals[$i],$$array[$i]); |
97 |
+ |
} |
98 |
+ |
} |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
|
102 |
+ |
sub cmpstring { |
103 |
+ |
my $self=shift; |
104 |
+ |
my $s1=shift; |
105 |
+ |
my $s2=shift; |
106 |
|
|
107 |
+ |
if ( ! defined $s2) { |
108 |
+ |
$self->testfail("Return string is undefined expecting $s1"), |
109 |
+ |
} |
110 |
+ |
elsif ( $s1 ne $s2 ) { |
111 |
+ |
$self->testfail("Expecting $s1 got $s2"); |
112 |
+ |
} |
113 |
+ |
else { |
114 |
+ |
$self->testpass("Got $s2 as expected"); |
115 |
+ |
} |
116 |
+ |
} |
117 |
|
|
118 |
|
# A virtual method to be overridden |
119 |
|
sub init { |
148 |
|
open ( FILE1, "<$file1" ) or die "Cannot Read Test Output $file1 $!\n"; |
149 |
|
open ( FILE2, "<$file2" ) or die "Cannot Read Benchmark file ". |
150 |
|
"$file2 $!\n"; |
151 |
< |
while ( $f1=<FILE1> ) { |
152 |
< |
$f2=<FILE2>; |
153 |
< |
if ( $f2 ne $f1 ) { |
154 |
< |
print "T:\n$f1\nB:$f2\n"; |
151 |
> |
while ( $f1=<FILE2> ) { |
152 |
> |
$f2=<FILE1>; |
153 |
> |
if ( (!defined $f2 ) || ( ! defined $f1) || ($f2 ne $f1 )) { |
154 |
> |
#print "T:\n$f1\nB:$f2\n"; |
155 |
|
$same=0; |
156 |
|
} |
157 |
|
} |
167 |
|
my $name=shift; |
168 |
|
|
169 |
|
if ( -d "$name" ) { |
170 |
< |
$self->testpass(""); |
170 |
> |
$self->testpass("Directory $name exists - test passed"); |
171 |
|
} |
172 |
|
else { |
173 |
|
$self->testfail("Directory $name does not exist"); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
+ |
sub verifyexists { |
178 |
+ |
my $self=shift; |
179 |
+ |
my $name=shift; |
180 |
+ |
|
181 |
+ |
if ( -e "$name" ) { |
182 |
+ |
$self->testpass("$name exists - test passed"); |
183 |
+ |
} |
184 |
+ |
else { |
185 |
+ |
$self->testfail("$name does not exist"); |
186 |
+ |
} |
187 |
+ |
} |
188 |
+ |
|
189 |
|
sub testfail { |
190 |
|
my $self=shift; |
191 |
|
my $string=shift; |
277 |
|
$self->_testout( " (".$args.")" ); |
278 |
|
$num=0; |
279 |
|
if ( exists $self->{expect} ) { |
280 |
+ |
print "Testing Expected Values against actual returns ....\n"; |
281 |
|
@mylist=eval { $self->{object}->$subname(@_); }; |
282 |
|
die "Test Failed $@\n" if $@; |
283 |
+ |
my $nrv=$#mylist+1; my $nrve=$#{$self->{expect}}+1; |
284 |
+ |
print $nrv." values returned ".$nrve." expected\n"; |
285 |
+ |
if ( $nrv != $nrve ) { |
286 |
+ |
$self->testfail("Number of returned values != that expected"); |
287 |
+ |
} |
288 |
|
if ( defined @mylist ) { |
289 |
+ |
# size check |
290 |
+ |
if ( $#mylist != $#{$self->{expect}} ) { |
291 |
+ |
$self->testfail("Number of returned values inconsistent"); |
292 |
+ |
} |
293 |
|
foreach $myreturn ( @mylist ) { |
294 |
|
if ( ! defined $myreturn ) { |
295 |
|
print "Undefined Value Passed Back\n"; |
296 |
|
} |
297 |
< |
elsif ( \$myreturn=~/HASH/ ) { |
298 |
< |
print "Hash Refreturned\n"; |
297 |
> |
elsif ( $myreturn=~/HASH/ ) { |
298 |
> |
print "Hash Ref ".ref($myreturn)." returned\n"; |
299 |
|
} |
300 |
< |
elsif ( \$myreturn=~/CODE/ ) { |
300 |
> |
elsif ( $myreturn=~/CODE/ ) { |
301 |
|
print "Code Ref returned\n"; |
302 |
|
} |
303 |
< |
elsif ( \$myreturn=~/ARRAY/ ) { |
303 |
> |
elsif ( $myreturn=~/ARRAY/ ) { |
304 |
|
print "Array Ref returned\n"; |
305 |
|
} |
306 |
|
else { |
327 |
|
# |
328 |
|
sub expect { |
329 |
|
my $self=shift; |
268 |
– |
my $string=shift; |
330 |
|
|
331 |
< |
push @{$self->{expect}}, $string; |
331 |
> |
push @{$self->{expect}}, @_; |
332 |
|
} |
333 |
|
|
334 |
|
sub clearexpect { |
426 |
|
} |
427 |
|
close SRCIN; |
428 |
|
} |
429 |
+ |
|
430 |
+ |
sub cleantemp { |
431 |
+ |
my $self=shift; |
432 |
+ |
use File::Path; |
433 |
+ |
rmtree($self->temparea()); |
434 |
+ |
} |