ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/TestClass.pm
Revision: 1.9
Committed: Thu Mar 2 16:40:24 2000 UTC (25 years, 2 months ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: ProtoEnd
Branch point for: V0_9branch
Changes since 1.8: +25 -3 lines
Log Message:
store so far

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