3 |
|
# |
4 |
|
# Interface |
5 |
|
# --------- |
6 |
< |
# new($module,testdatadir,project) : module example - Utilities/urlhandler |
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"; |
35 |
|
my $fullmodule=shift; |
36 |
|
chomp $fullmodule; |
37 |
|
my $datadir=shift; |
32 |
– |
my $project=shift; |
38 |
|
|
39 |
|
# The usual Object blessing |
40 |
|
$self={}; |
43 |
|
#some local working variables |
44 |
|
my $testmodule; |
45 |
|
my $module; |
46 |
< |
my $dir; |
47 |
< |
if ( $fullmodule=~/\// ) { |
48 |
< |
($dir=$fullmodule)=~s/(.*\/)(.*)/$1/; |
49 |
< |
} |
46 |
> |
my $dir=""; |
47 |
> |
# if ( $fullmodule=~/\// ) { |
48 |
> |
# ($dir=$fullmodule)=~s/(.*\/)(.*)/$1/; |
49 |
> |
# } |
50 |
> |
if ( $fullmodule=~/::/ ) { |
51 |
> |
($dir=$fullmodule)=~s/(.*::)(.*)/$1/; |
52 |
> |
} |
53 |
|
else { $dir="" } |
54 |
< |
($testmodule=$fullmodule)=~s/(.*\/)(.*)/$1test\/test_$2/; |
55 |
< |
($testmodule=$fullmodule)=~s/${dir}(.*)/${dir}test\/test_$1/; |
54 |
> |
($testmodule=$fullmodule)=~s/(.*\/)(.*)/$1test::test_$2/; |
55 |
> |
($testmodule=$fullmodule)=~s/${dir}(.*)/${dir}test::test_$1/; |
56 |
|
($module=$testmodule)=~s/.*\///g; |
57 |
|
|
50 |
– |
# Data Initialisation |
51 |
– |
$self->{project}=$project; |
58 |
|
$self->{class}=$module; |
59 |
|
$self->{"datadir"}=$datadir; |
60 |
|
$self->{filenumber}=0; |
61 |
+ |
rmtree("/tmp/SCRAMtest++"); |
62 |
+ |
$self->{temparea}="/tmp/SCRAMtest++"; |
63 |
+ |
use File::Path; |
64 |
+ |
mkpath ($self->{temparea},0, 0777); |
65 |
|
|
66 |
|
# Now setup a new testobject of the appropriate type |
67 |
< |
require $testmodule."\.pm"; |
67 |
> |
eval "require $testmodule"; |
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 { |
127 |
|
my $self=shift; |
128 |
|
my $string=shift; |
129 |
|
$self->{testnumber}++; |
130 |
< |
$self->_testout(); |
130 |
> |
$self->_testout(""); |
131 |
|
$self->_testout("---------------------------* Test $self->{testnumber}". |
132 |
|
" *------------------------------"); |
133 |
|
$self->_testout("| $string "); |
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; |
252 |
|
|
253 |
|
# Data Initialisation |
254 |
|
$self->{testclass}=$class; |
255 |
< |
$self->{classname}=$module; |
255 |
> |
($self->{classname}=$module)=~s/::/\//g; |
256 |
|
($self->{class}=$module)=~s/.*\///g; |
257 |
|
$self->{classfile}=$ENV{SCRAM_HOME}."/src/".$self->{classname}."\.pm"; |
202 |
– |
$self->{testclassfile}=$path."/test/".$self->{testclass}."\.pm"; |
258 |
|
$self->init(@_); |
259 |
|
$self->analyseInterface(); |
260 |
|
delete $self->{expect}; |
268 |
|
my $self=shift; |
269 |
|
my $subname=shift; |
270 |
|
my $myreturn; |
271 |
+ |
my $expected; |
272 |
|
|
273 |
|
$self->_checkdoc($subname); |
274 |
|
$self->{inttest}{$subname}++; |
275 |
|
$self->_testout(">Trying interface $subname "); |
276 |
|
my $args=join ', ', @_; |
277 |
|
$self->_testout( " (".$args.")" ); |
278 |
+ |
$num=0; |
279 |
|
if ( exists $self->{expect} ) { |
280 |
< |
@myreturn=($self->{object}->$subname(@_)); |
281 |
< |
if ( "@myreturn" eq $self->{expect} ) { |
282 |
< |
$self->testpass("OK - returned as expected"); |
283 |
< |
} |
284 |
< |
else { |
285 |
< |
$self->testfail("Expecting $self->{expect}, got @myreturn"); |
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 Ref ".ref($myreturn)." returned\n"; |
299 |
> |
} |
300 |
> |
elsif ( $myreturn=~/CODE/ ) { |
301 |
> |
print "Code Ref returned\n"; |
302 |
|
} |
303 |
< |
return @myreturn; |
303 |
> |
elsif ( $myreturn=~/ARRAY/ ) { |
304 |
> |
print "Array Ref returned\n"; |
305 |
> |
} |
306 |
> |
else { |
307 |
> |
$expected=$self->{expect}[$num++]; |
308 |
> |
if ( $myreturn eq $expected ) { #simple return case |
309 |
> |
$self->testpass("OK - returned as expected ($expected)"); |
310 |
> |
} |
311 |
> |
else { |
312 |
> |
$self->testfail("Expecting $expected, got ". |
313 |
> |
$myreturn); |
314 |
> |
} |
315 |
> |
} |
316 |
> |
} # end foreach block |
317 |
> |
} |
318 |
> |
return @mylist; |
319 |
|
} |
320 |
|
else { |
321 |
|
return ($self->{object}->$subname(@_)); |
327 |
|
# |
328 |
|
sub expect { |
329 |
|
my $self=shift; |
242 |
– |
my $string=shift; |
330 |
|
|
331 |
< |
$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 |
+ |
} |