ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/TestClass.pm
(Generate patch)

Comparing COMP/SCRAM/src/Utilities/TestClass.pm (file contents):
Revision 1.1 by williamc, Thu Jun 24 14:46:14 1999 UTC vs.
Revision 1.9 by williamc, Thu Mar 2 16:40:24 2000 UTC

# Line 3 | Line 3
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  
25 < package TestClass;
25 > package Utilities::TestClass;
26   require 5.004;
27   $bold  = "\033[1m";
28   $normal = "\033[0m";
# Line 29 | Line 33 | sub new {
33          my $fullmodule=shift;
34          chomp $fullmodule;
35          my $datadir=shift;
32        my $project=shift;
36  
37          # The usual Object blessing
38          $self={};
# Line 38 | Line 41 | sub new {
41          #some local working variables
42          my $testmodule;
43          my $module;
44 <        my $dir;
45 <        if ( $fullmodule=~/\// ) {
46 <          ($dir=$fullmodule)=~s/(.*\/)(.*)/$1/;
47 <        }
44 >        my $dir="";
45 > #       if ( $fullmodule=~/\// ) {
46 > #         ($dir=$fullmodule)=~s/(.*\/)(.*)/$1/;
47 > #       }
48 >        if ( $fullmodule=~/::/ ) {
49 >          ($dir=$fullmodule)=~s/(.*::)(.*)/$1/;
50 >        }
51          else { $dir="" }
52 <        ($testmodule=$fullmodule)=~s/(.*\/)(.*)/$1test\/test_$2/;
53 <        ($testmodule=$fullmodule)=~s/${dir}(.*)/${dir}test\/test_$1/;
52 >        ($testmodule=$fullmodule)=~s/(.*\/)(.*)/$1test::test_$2/;
53 >        ($testmodule=$fullmodule)=~s/${dir}(.*)/${dir}test::test_$1/;
54          ($module=$testmodule)=~s/.*\///g;
55  
50        # Data Initialisation
51        $self->{project}=$project;
56          $self->{class}=$module;
57          $self->{"datadir"}=$datadir;
58          $self->{filenumber}=0;
59 +        rmtree("/tmp/SCRAMtest++");
60 +        $self->{temparea}="/tmp/SCRAMtest++";
61 +        use File::Path;
62 +        mkpath ($self->{temparea},0, 0777);
63  
64          # Now setup a new testobject of the appropriate type
65 <        require $testmodule."\.pm";
65 >        eval "require $testmodule";
66 >        die $@ if $@;
67          $self->{testobj}=$module->_new($self, $fullmodule);
68  
69 +        # make sure the temparea is cleaned
70 +        use File::Path;
71 +
72          return $self;
73   }
74  
# Line 70 | Line 82 | sub dotest {
82   }
83  
84  
85 + 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          
101   # A virtual method to be overridden
102   sub init {
# Line 83 | Line 110 | sub newtest {
110          my $self=shift;
111          my $string=shift;
112          $self->{testnumber}++;
113 <        $self->_testout();
113 >        $self->_testout("");
114          $self->_testout("---------------------------* Test $self->{testnumber}".
115                                  " *------------------------------");
116          $self->_testout("|  $string ");
# Line 104 | Line 131 | sub verify {
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 <        while ( $f1=<FILE1> ) {
135 <         $f2=<FILE2>;
136 <         if ( $f2 ne $f1 ) {
137 <           print "T:\n$f1\nB:$f2\n";
134 >        while ( $f1=<FILE2> ) {
135 >         $f2=<FILE1>;
136 >         if ( (!defined $f2 ) || ( ! defined $f1) || ($f2 ne $f1 )) {
137 >           #print "T:\n$f1\nB:$f2\n";
138             $same=0;
139           }
140          }
# Line 123 | Line 150 | sub verifydir {
150          my $name=shift;
151  
152          if ( -d "$name" ) {
153 <           $self->testpass();
153 >           $self->testpass("Directory $name exists - test passed");
154          }
155          else {
156             $self->testfail("Directory $name does not exist");
157          }
158   }
159  
160 + sub verifyexists {
161 +        my $self=shift;
162 +        my $name=shift;
163 +
164 +        if ( -e "$name" ) {
165 +           $self->testpass("$name exists - test passed");
166 +        }
167 +        else {
168 +           $self->testfail("$name does not exist");
169 +        }
170 + }
171 +
172   sub testfail {
173          my $self=shift;
174          my $string=shift;
# Line 196 | Line 235 | sub _new {
235  
236          # Data Initialisation
237          $self->{testclass}=$class;
238 <        $self->{classname}=$module;
238 >        ($self->{classname}=$module)=~s/::/\//g;
239          ($self->{class}=$module)=~s/.*\///g;
240          $self->{classfile}=$ENV{SCRAM_HOME}."/src/".$self->{classname}."\.pm";
202        $self->{testclassfile}=$path."/test/".$self->{testclass}."\.pm";
241          $self->init(@_);
242          $self->analyseInterface();
243          delete $self->{expect};
# Line 213 | Line 251 | sub testinterface {
251          my $self=shift;
252          my $subname=shift;
253          my $myreturn;
254 +        my $expected;
255  
256          $self->_checkdoc($subname);
257          $self->{inttest}{$subname}++;
258          $self->_testout(">Trying interface $subname ");
259          my $args=join ', ', @_;
260          $self->_testout( " (".$args.")" );
261 +        $num=0;
262          if ( exists $self->{expect} ) {
263 <          @myreturn=($self->{object}->$subname(@_));
264 <          if ( "@myreturn" eq $self->{expect} ) {
265 <            $self->testpass("OK - returned as expected");
266 <          }
267 <          else  {
268 <            $self->testfail("Expecting $self->{expect}, got @myreturn");
263 >         print "Testing Expected Values against actual returns ....\n";
264 >         @mylist=eval { $self->{object}->$subname(@_); };
265 >          die "Test Failed $@\n" if $@;
266 >         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 >         if ( defined @mylist ) {
272 >         # size check
273 >         if ( $#mylist != $#{$self->{expect}} ) {
274 >                $self->testfail("Number of returned values inconsistent");
275 >         }
276 >         foreach $myreturn ( @mylist ) {
277 >          if ( ! defined $myreturn ) {
278 >                print "Undefined Value Passed Back\n";
279            }
280 <          return @myreturn;
280 >          elsif ( $myreturn=~/HASH/ ) {
281 >             print "Hash Ref ".ref($myreturn)." returned\n";
282 >          }
283 >          elsif ( $myreturn=~/CODE/ ) {
284 >             print "Code Ref returned\n";
285 >          }
286 >          elsif ( $myreturn=~/ARRAY/ ) {
287 >            print "Array Ref returned\n";
288 >          }
289 >          else {
290 >            $expected=$self->{expect}[$num++];
291 >            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 >        } # end foreach block
300 >        }
301 >          return @mylist;
302          }
303          else {
304            return ($self->{object}->$subname(@_));
# Line 239 | Line 310 | sub testinterface {
310   #
311   sub expect {
312          my $self=shift;
242        my $string=shift;
313  
314 <        $self->{expect}=$string;
314 >        push @{$self->{expect}}, @_;
315   }
316  
317   sub clearexpect {
# Line 339 | Line 409 | sub analyseInterface {
409          }
410          close SRCIN;
411   }
412 +
413 + sub cleantemp {
414 +        my $self=shift;
415 +        use File::Path;
416 +        rmtree($self->temparea());
417 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines