ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/TestClass.pm
Revision: 1.11
Committed: Mon Sep 11 11:31:49 2000 UTC (24 years, 8 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.10: +15 -0 lines
Log Message:
add testswitch

File Contents

# User Rev Content
1 williamc 1.1 #
2     # Test a documented perl class
3     #
4     # Interface
5     # ---------
6 williamc 1.4 # new($module,testdatadir) : module example - Utilities/urlhandler
7 williamc 1.1 # dotest(@args) : Start testing - arguments dependent on inheriting class
8     #
9 williamc 1.9 # cmpstring(expectedstring,returnedstring) : cmp a string and fail if not equal
10 williamc 1.1 # 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 williamc 1.5 # verifyexists(file) : Verify the existence of file
14 williamc 1.1 # datadir([dir]) : return the current data directory (set it to dir if supplied)
15 williamc 1.11 # testswitch(bool,"true text","falsetext") : testpass or fail according to bool
16 williamc 1.5 # testfail(string) : report that current test has failed
17     # testpass(string) : report that current test has passed
18 williamc 1.1 # newfilename() : return a new filename that can be opened etc.
19     # temparea() : return a directory for building temporary stuff
20     # newobject(@args) : Set up a new object to be tested
21     # testinterface($name,@args) : perform interface tests for $name with @args
22     # expect(string) : tell the testinterface of any expected return values
23     # clearexpect() : Reset any expect variables.
24 williamc 1.5 #cleantemp() : delete the temporary area
25 williamc 1.10 # cmparray(arrayref, @reqvals) : test the arrayref against expected
26     # cmpstring(expectedstring,actualstring) :
27 williamc 1.1
28 williamc 1.7 package Utilities::TestClass;
29 williamc 1.1 require 5.004;
30     $bold = "\033[1m";
31     $normal = "\033[0m";
32    
33     # -------------------- Front line Interface methods ---------------------
34     sub new {
35     my $class=shift;
36     my $fullmodule=shift;
37     chomp $fullmodule;
38     my $datadir=shift;
39    
40     # The usual Object blessing
41     $self={};
42     bless $self,$class;
43    
44     #some local working variables
45     my $testmodule;
46     my $module;
47 williamc 1.2 my $dir="";
48 williamc 1.4 # if ( $fullmodule=~/\// ) {
49     # ($dir=$fullmodule)=~s/(.*\/)(.*)/$1/;
50     # }
51     if ( $fullmodule=~/::/ ) {
52     ($dir=$fullmodule)=~s/(.*::)(.*)/$1/;
53     }
54 williamc 1.1 else { $dir="" }
55 williamc 1.4 ($testmodule=$fullmodule)=~s/(.*\/)(.*)/$1test::test_$2/;
56     ($testmodule=$fullmodule)=~s/${dir}(.*)/${dir}test::test_$1/;
57 williamc 1.1 ($module=$testmodule)=~s/.*\///g;
58    
59     $self->{class}=$module;
60     $self->{"datadir"}=$datadir;
61     $self->{filenumber}=0;
62 williamc 1.9 rmtree("/tmp/SCRAMtest++");
63 williamc 1.5 $self->{temparea}="/tmp/SCRAMtest++";
64 williamc 1.2 use File::Path;
65     mkpath ($self->{temparea},0, 0777);
66 williamc 1.1
67     # Now setup a new testobject of the appropriate type
68 williamc 1.4 eval "require $testmodule";
69     die $@ if $@;
70 williamc 1.1 $self->{testobj}=$module->_new($self, $fullmodule);
71    
72 williamc 1.8 # make sure the temparea is cleaned
73     use File::Path;
74    
75 williamc 1.1 return $self;
76     }
77    
78     #
79     # Call the test routine of our inheriting class
80     #
81     sub dotest {
82     $self=shift;
83     $self->{testobj}->test(@_);
84     $self->{testobj}->checktests();
85 williamc 1.10 }
86    
87     sub cmparray {
88     my $self=shift;
89     my $array=shift;
90     my @vals=@_;
91    
92     if ( $#{$array} ne $#vals) { $self->testfail(
93     $#{$array}." items retuned, $#vals expected");
94     }
95     else {
96     for( my $i=0; $i<= $#{$array}; $i++) {
97     $self->cmpstring($vals[$i],$$array[$i]);
98     }
99     }
100 williamc 1.1 }
101    
102    
103 williamc 1.9 sub cmpstring {
104     my $self=shift;
105     my $s1=shift;
106     my $s2=shift;
107    
108     if ( ! defined $s2) {
109     $self->testfail("Return string is undefined expecting $s1"),
110     }
111     elsif ( $s1 ne $s2 ) {
112     $self->testfail("Expecting $s1 got $s2");
113     }
114     else {
115     $self->testpass("Got $s2 as expected");
116     }
117     }
118 williamc 1.1
119     # A virtual method to be overridden
120     sub init {
121     my $self=shift;
122     }
123    
124     #
125     # initiate a new test sequence
126     #
127     sub newtest {
128     my $self=shift;
129     my $string=shift;
130     $self->{testnumber}++;
131 williamc 1.2 $self->_testout("");
132 williamc 1.1 $self->_testout("---------------------------* Test $self->{testnumber}".
133     " *------------------------------");
134     $self->_testout("| $string ");
135     $self->_testout("|");
136     $self->_testout(
137     "---------------------------------------------------------------");
138    
139     }
140    
141     sub verify {
142     my $self=shift;
143     my $file1=shift;
144     my $file2=shift;
145     my $same=1;
146    
147     $self->_testout(">Verifying files : $file1 \n".
148     " $file2");
149     open ( FILE1, "<$file1" ) or die "Cannot Read Test Output $file1 $!\n";
150     open ( FILE2, "<$file2" ) or die "Cannot Read Benchmark file ".
151     "$file2 $!\n";
152 williamc 1.5 while ( $f1=<FILE2> ) {
153     $f2=<FILE1>;
154     if ( (!defined $f2 ) || ( ! defined $f1) || ($f2 ne $f1 )) {
155     #print "T:\n$f1\nB:$f2\n";
156 williamc 1.1 $same=0;
157     }
158     }
159     close FILE1;
160     close FILE2;
161     if ( $same==0 ) {
162     $self->testfail("File $file1 is not the same as $file2");
163     }
164     }
165    
166     sub verifydir {
167     my $self=shift;
168     my $name=shift;
169    
170     if ( -d "$name" ) {
171 williamc 1.5 $self->testpass("Directory $name exists - test passed");
172 williamc 1.1 }
173     else {
174     $self->testfail("Directory $name does not exist");
175     }
176     }
177    
178 williamc 1.5 sub verifyexists {
179     my $self=shift;
180     my $name=shift;
181    
182 williamc 1.6 if ( -e "$name" ) {
183     $self->testpass("$name exists - test passed");
184 williamc 1.5 }
185     else {
186 williamc 1.6 $self->testfail("$name does not exist");
187 williamc 1.11 }
188     }
189    
190     sub testswitch {
191     my $self=shift;
192     my $bool=shift;
193     my $string1=shift;
194     my $string2=shift;
195    
196     if ( $bool ) {
197     $self->testpass($string1);
198     }
199     else {
200     $self->testfail($string2);
201 williamc 1.5 }
202     }
203    
204 williamc 1.1 sub testfail {
205     my $self=shift;
206     my $string=shift;
207    
208     $self->_testout("$bold $string $normal");
209     push @{$self->{failedtests}}, $self->{testnumber};
210     }
211    
212     sub testpass {
213     my $self=shift;
214     my $string=shift;
215    
216     $self->_testout($string);
217     }
218    
219     sub newfilename {
220     my $self=shift;
221     $self->{filenumber}++;
222     return "temptest_$self->{filenumber}";
223     }
224    
225     sub temparea {
226     my $self=shift;
227     return $self->{temparea};
228     }
229    
230     #
231     # return the data directory ( and set if given an argument )
232     #
233     sub datadir {
234     my $self=shift;
235     my $dir=shift;
236     if ( $dir ne "" ) {
237     $self->{datadir}=$dir;
238     }
239     return $self->{datadir};
240     }
241    
242     #
243     # -------------------- Private Methods ----------------------------
244     # (only to be used by the inheriting class)
245    
246     #
247     # A basic new method for inheriting classes
248     #
249    
250     sub _new {
251     my $class=shift;
252     my $testobject=shift;
253     my $module=shift;
254    
255    
256     # Bless this object and all those who inherit from her
257     my $self={};
258     bless $self, $class;
259    
260     # we want the dat members from the initialisation of our class in
261     # here too
262     foreach $key ( keys %$testobject ) {
263     $self->{$key}=$testobject->{$key};
264     }
265    
266     print "Initialising $class\n";
267    
268     # Data Initialisation
269     $self->{testclass}=$class;
270 williamc 1.4 ($self->{classname}=$module)=~s/::/\//g;
271 williamc 1.1 ($self->{class}=$module)=~s/.*\///g;
272     $self->{classfile}=$ENV{SCRAM_HOME}."/src/".$self->{classname}."\.pm";
273     $self->init(@_);
274     $self->analyseInterface();
275     delete $self->{expect};
276     return $self;
277     }
278    
279     #
280     # Test the interface
281     #
282     sub testinterface {
283     my $self=shift;
284     my $subname=shift;
285     my $myreturn;
286 williamc 1.4 my $expected;
287 williamc 1.1
288     $self->_checkdoc($subname);
289     $self->{inttest}{$subname}++;
290     $self->_testout(">Trying interface $subname ");
291     my $args=join ', ', @_;
292     $self->_testout( " (".$args.")" );
293 williamc 1.2 $num=0;
294 williamc 1.1 if ( exists $self->{expect} ) {
295 williamc 1.9 print "Testing Expected Values against actual returns ....\n";
296 williamc 1.2 @mylist=eval { $self->{object}->$subname(@_); };
297     die "Test Failed $@\n" if $@;
298 williamc 1.9 my $nrv=$#mylist+1; my $nrve=$#{$self->{expect}}+1;
299     print $nrv." values returned ".$nrve." expected\n";
300     if ( $nrv != $nrve ) {
301     $self->testfail("Number of returned values != that expected");
302     }
303 williamc 1.4 if ( defined @mylist ) {
304 williamc 1.6 # size check
305 williamc 1.9 if ( $#mylist != $#{$self->{expect}} ) {
306     $self->testfail("Number of returned values inconsistent");
307 williamc 1.6 }
308 williamc 1.4 foreach $myreturn ( @mylist ) {
309     if ( ! defined $myreturn ) {
310     print "Undefined Value Passed Back\n";
311     }
312 williamc 1.5 elsif ( $myreturn=~/HASH/ ) {
313 williamc 1.8 print "Hash Ref ".ref($myreturn)." returned\n";
314 williamc 1.1 }
315 williamc 1.5 elsif ( $myreturn=~/CODE/ ) {
316 williamc 1.2 print "Code Ref returned\n";
317     }
318 williamc 1.5 elsif ( $myreturn=~/ARRAY/ ) {
319 williamc 1.2 print "Array Ref returned\n";
320     }
321     else {
322 williamc 1.4 $expected=$self->{expect}[$num++];
323 williamc 1.2 if ( $myreturn eq $expected ) { #simple return case
324     $self->testpass("OK - returned as expected ($expected)");
325     }
326     else {
327     $self->testfail("Expecting $expected, got ".
328     $myreturn);
329     }
330     }
331 williamc 1.4 } # end foreach block
332     }
333 williamc 1.2 return @mylist;
334 williamc 1.1 }
335     else {
336     return ($self->{object}->$subname(@_));
337     }
338     }
339    
340     #
341     # expect - tell testinterface what returns to expect and fail/pass
342     #
343     sub expect {
344     my $self=shift;
345    
346 williamc 1.6 push @{$self->{expect}}, @_;
347 williamc 1.1 }
348    
349     sub clearexpect {
350     my $self=shift;
351     my $string=shift;
352    
353     delete $self->{expect};
354     }
355    
356     #
357     # checktests
358     #
359     sub checktests {
360     my $self=shift;
361     $self->newtest(">Checking all documented Interfaces have been tested ");
362     foreach $key ( keys %{$self->{interfaceargs}} ) {
363     if ( ! exists $self->{inttest}{$key} ) {
364     $self->testfail ("$key has not been tested");
365     }
366     }
367     # Now see whats failed
368     foreach $fail ( @{$self->{failedtests}} ) {
369     $self->_testout("$bold Failed in $fail $normal");
370     }
371     }
372    
373     #
374     # Create a new object
375     #
376     sub newobject {
377     my $self=shift;
378    
379     $self->_checkdoc("new");
380     $self->_testout(">Creating new Object $self->{class}");
381     $self->{object}=$self->{class}->new(@_);
382     $self->_testout(" ( $self->{object} )");
383     $self->{inttest}{"new"}++;
384     }
385    
386     #
387     # check if interface has been documented
388     #
389     sub _checkdoc {
390     my $self=shift;
391     my $name=shift;
392    
393     if ( exists $self->{interfaceargs}{$name} ) {
394     }
395     else {
396     $self->_testout
397     ("Tester: Interface Method '$name' is not documented\n");
398     }
399     }
400    
401     #
402     # Output messages to screen/logs etc
403     #
404     sub _testout($) {
405     my $self=shift;
406     my $string=shift;
407    
408     print $string;
409     print "\n";
410     }
411     #
412     # Method to read the interface documentation
413     #
414    
415     sub analyseInterface {
416     my $self=shift;
417     my $intregion=0;
418    
419     open ( SRCIN, $self->{"classfile"} )
420     or die "Unable to open $classfile $!\n";
421     while ( <SRCIN> ) {
422     if ( $_=~/#\s*Interface/g ) {
423     $intregion=1;
424     next;
425     }
426     if ( $intregion ) { # if we are in the interface documentation
427     if ( ( $_!~/^#/ ) || ( $_=~/^#\s?-{40}/ ) ) { #moving out of Int doc
428     $intregion=0;
429     next;
430     }
431     if ( $_=~/^#\s*(.*)\((.*)\)?:(.*)/ ) {
432     $interface=$1;
433     $args=$2;
434     $rest=$3;
435     next if ($interface eq "");
436     push @{$self->{'interfaces'}},$interface;
437     $self->{interfaceargs}{$interface}=$args;
438     print " Documented Interface $interface\n";
439     }
440     }
441     }
442     close SRCIN;
443 williamc 1.5 }
444    
445     sub cleantemp {
446     my $self=shift;
447     use File::Path;
448     rmtree($self->temparea());
449 williamc 1.1 }