ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/TestClass.pm
Revision: 1.9.2.2
Committed: Wed Apr 19 09:15:17 2000 UTC (25 years ago) by williamc
Content type: text/plain
Branch: V0_9branch
CVS Tags: V0_18_0, V0_17_1, V0_17_0, V0_16_4, V0_16_3, V0_16_2, V0_16_1, V0_16_0, V0_15_1, V0_15_0, V0_15_0beta, V0_14_0, V0_12_12_4, V0_12_12_3, V0_12_12_2, V0_12_12_1, V0_12_12_0, PlayGround_0, V0_12_12, V0_12_11, V0_12_9b, V0_12_10, V0_12_9, V0_12_8, V0_12_7, V0_12_6, V0_12_5, V0_12_4, V0_12_3, V0_12_2, V0_12_1, V0_12_0
Branch point for: V0_17branch, V0_16branch, V0_15branch, HPWbranch
Changes since 1.9.2.1: +17 -0 lines
Log Message:
document cmpstring

File Contents

# Content
1 #
2 # Test a documented perl class
3 #
4 # Interface
5 # ---------
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 # 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 Utilities::TestClass;
28 require 5.004;
29 $bold = "\033[1m";
30 $normal = "\033[0m";
31
32 # -------------------- Front line Interface methods ---------------------
33 sub new {
34 my $class=shift;
35 my $fullmodule=shift;
36 chomp $fullmodule;
37 my $datadir=shift;
38
39 # The usual Object blessing
40 $self={};
41 bless $self,$class;
42
43 #some local working variables
44 my $testmodule;
45 my $module;
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/;
56 ($module=$testmodule)=~s/.*\///g;
57
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 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
77 #
78 # Call the test routine of our inheriting class
79 #
80 sub dotest {
81 $self=shift;
82 $self->{testobj}->test(@_);
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 {
120 my $self=shift;
121 }
122
123 #
124 # initiate a new test sequence
125 #
126 sub newtest {
127 my $self=shift;
128 my $string=shift;
129 $self->{testnumber}++;
130 $self->_testout("");
131 $self->_testout("---------------------------* Test $self->{testnumber}".
132 " *------------------------------");
133 $self->_testout("| $string ");
134 $self->_testout("|");
135 $self->_testout(
136 "---------------------------------------------------------------");
137
138 }
139
140 sub verify {
141 my $self=shift;
142 my $file1=shift;
143 my $file2=shift;
144 my $same=1;
145
146 $self->_testout(">Verifying files : $file1 \n".
147 " $file2");
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=<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 }
158 close FILE1;
159 close FILE2;
160 if ( $same==0 ) {
161 $self->testfail("File $file1 is not the same as $file2");
162 }
163 }
164
165 sub verifydir {
166 my $self=shift;
167 my $name=shift;
168
169 if ( -d "$name" ) {
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;
192
193 $self->_testout("$bold $string $normal");
194 push @{$self->{failedtests}}, $self->{testnumber};
195 }
196
197 sub testpass {
198 my $self=shift;
199 my $string=shift;
200
201 $self->_testout($string);
202 }
203
204 sub newfilename {
205 my $self=shift;
206 $self->{filenumber}++;
207 return "temptest_$self->{filenumber}";
208 }
209
210 sub temparea {
211 my $self=shift;
212 return $self->{temparea};
213 }
214
215 #
216 # return the data directory ( and set if given an argument )
217 #
218 sub datadir {
219 my $self=shift;
220 my $dir=shift;
221 if ( $dir ne "" ) {
222 $self->{datadir}=$dir;
223 }
224 return $self->{datadir};
225 }
226
227 #
228 # -------------------- Private Methods ----------------------------
229 # (only to be used by the inheriting class)
230
231 #
232 # A basic new method for inheriting classes
233 #
234
235 sub _new {
236 my $class=shift;
237 my $testobject=shift;
238 my $module=shift;
239
240
241 # Bless this object and all those who inherit from her
242 my $self={};
243 bless $self, $class;
244
245 # we want the dat members from the initialisation of our class in
246 # here too
247 foreach $key ( keys %$testobject ) {
248 $self->{$key}=$testobject->{$key};
249 }
250
251 print "Initialising $class\n";
252
253 # Data Initialisation
254 $self->{testclass}=$class;
255 ($self->{classname}=$module)=~s/::/\//g;
256 ($self->{class}=$module)=~s/.*\///g;
257 $self->{classfile}=$ENV{SCRAM_HOME}."/src/".$self->{classname}."\.pm";
258 $self->init(@_);
259 $self->analyseInterface();
260 delete $self->{expect};
261 return $self;
262 }
263
264 #
265 # Test the interface
266 #
267 sub testinterface {
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 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 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(@_));
322 }
323 }
324
325 #
326 # expect - tell testinterface what returns to expect and fail/pass
327 #
328 sub expect {
329 my $self=shift;
330
331 push @{$self->{expect}}, @_;
332 }
333
334 sub clearexpect {
335 my $self=shift;
336 my $string=shift;
337
338 delete $self->{expect};
339 }
340
341 #
342 # checktests
343 #
344 sub checktests {
345 my $self=shift;
346 $self->newtest(">Checking all documented Interfaces have been tested ");
347 foreach $key ( keys %{$self->{interfaceargs}} ) {
348 if ( ! exists $self->{inttest}{$key} ) {
349 $self->testfail ("$key has not been tested");
350 }
351 }
352 # Now see whats failed
353 foreach $fail ( @{$self->{failedtests}} ) {
354 $self->_testout("$bold Failed in $fail $normal");
355 }
356 }
357
358 #
359 # Create a new object
360 #
361 sub newobject {
362 my $self=shift;
363
364 $self->_checkdoc("new");
365 $self->_testout(">Creating new Object $self->{class}");
366 $self->{object}=$self->{class}->new(@_);
367 $self->_testout(" ( $self->{object} )");
368 $self->{inttest}{"new"}++;
369 }
370
371 #
372 # check if interface has been documented
373 #
374 sub _checkdoc {
375 my $self=shift;
376 my $name=shift;
377
378 if ( exists $self->{interfaceargs}{$name} ) {
379 }
380 else {
381 $self->_testout
382 ("Tester: Interface Method '$name' is not documented\n");
383 }
384 }
385
386 #
387 # Output messages to screen/logs etc
388 #
389 sub _testout($) {
390 my $self=shift;
391 my $string=shift;
392
393 print $string;
394 print "\n";
395 }
396 #
397 # Method to read the interface documentation
398 #
399
400 sub analyseInterface {
401 my $self=shift;
402 my $intregion=0;
403
404 open ( SRCIN, $self->{"classfile"} )
405 or die "Unable to open $classfile $!\n";
406 while ( <SRCIN> ) {
407 if ( $_=~/#\s*Interface/g ) {
408 $intregion=1;
409 next;
410 }
411 if ( $intregion ) { # if we are in the interface documentation
412 if ( ( $_!~/^#/ ) || ( $_=~/^#\s?-{40}/ ) ) { #moving out of Int doc
413 $intregion=0;
414 next;
415 }
416 if ( $_=~/^#\s*(.*)\((.*)\)?:(.*)/ ) {
417 $interface=$1;
418 $args=$2;
419 $rest=$3;
420 next if ($interface eq "");
421 push @{$self->{'interfaces'}},$interface;
422 $self->{interfaceargs}{$interface}=$args;
423 print " Documented Interface $interface\n";
424 }
425 }
426 }
427 close SRCIN;
428 }
429
430 sub cleantemp {
431 my $self=shift;
432 use File::Path;
433 rmtree($self->temparea());
434 }