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.13 by williamc, Fri Sep 29 06:42:03 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 > # testswitch(int,"0 text"," non 0 text") : testpass or fail according to bool
16 > # testfail(string) : report that current test has failed
17 > # testpass(string) : report that current test has passed
18   # 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 + #cleantemp()    : delete the temporary area
25 + # cmparray(arrayref, @reqvals) : test the arrayref against expected
26 + # cmpstring(expectedstring,actualstring) :
27  
28 < package TestClass;
28 > package Utilities::TestClass;
29   require 5.004;
30   $bold  = "\033[1m";
31   $normal = "\033[0m";
# Line 29 | Line 36 | sub new {
36          my $fullmodule=shift;
37          chomp $fullmodule;
38          my $datadir=shift;
32        my $project=shift;
39  
40          # The usual Object blessing
41          $self={};
# Line 38 | Line 44 | sub new {
44          #some local working variables
45          my $testmodule;
46          my $module;
47 <        my $dir;
48 <        if ( $fullmodule=~/\// ) {
49 <          ($dir=$fullmodule)=~s/(.*\/)(.*)/$1/;
50 <        }
47 >        my $dir="";
48 > #       if ( $fullmodule=~/\// ) {
49 > #         ($dir=$fullmodule)=~s/(.*\/)(.*)/$1/;
50 > #       }
51 >        if ( $fullmodule=~/::/ ) {
52 >          ($dir=$fullmodule)=~s/(.*::)(.*)/$1/;
53 >        }
54          else { $dir="" }
55 <        ($testmodule=$fullmodule)=~s/(.*\/)(.*)/$1test\/test_$2/;
56 <        ($testmodule=$fullmodule)=~s/${dir}(.*)/${dir}test\/test_$1/;
55 >        ($testmodule=$fullmodule)=~s/(.*\/)(.*)/$1test::test_$2/;
56 >        ($testmodule=$fullmodule)=~s/${dir}(.*)/${dir}test::test_$1/;
57          ($module=$testmodule)=~s/.*\///g;
58  
50        # Data Initialisation
51        $self->{project}=$project;
59          $self->{class}=$module;
60          $self->{"datadir"}=$datadir;
61          $self->{filenumber}=0;
62 +        rmtree("/tmp/SCRAMtest++");
63 +        $self->{temparea}="/tmp/SCRAMtest++";
64 +        use File::Path;
65 +        mkpath ($self->{temparea},0, 0777);
66  
67          # Now setup a new testobject of the appropriate type
68 <        require $testmodule."\.pm";
68 >        eval "require $testmodule";
69 >        die $@ if $@;
70          $self->{testobj}=$module->_new($self, $fullmodule);
71  
72 +        # make sure the temparea is cleaned
73 +        use File::Path;
74 +
75          return $self;
76   }
77  
# Line 69 | Line 84 | sub dotest {
84          $self->{testobj}->checktests();
85   }
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 + }
101 +
102  
103 + sub cmpstring {
104 +        my $self=shift;
105 +        my $s1=shift;
106 +        my $s2=shift;
107 +
108 +        if ( ! defined $s2) {
109 +          if ( ( ! defined $s1 ) || ( $s1==undef )) {
110 +            $self->testpass("Got undefined as expected");
111 +          }
112 +          else {
113 +            $self->testfail("Return string is undefined expecting $s1"),
114 +          }
115 +        }
116 +        elsif ( $s1 ne $s2 ) {
117 +          $self->testfail("Expecting $s1 got $s2");
118 +        }
119 +        else {
120 +          $self->testpass("Got $s2 as expected");
121 +        }
122 + }
123          
124   # A virtual method to be overridden
125   sub init {
# Line 83 | Line 133 | sub newtest {
133          my $self=shift;
134          my $string=shift;
135          $self->{testnumber}++;
136 <        $self->_testout();
136 >        $self->_testout("");
137          $self->_testout("---------------------------* Test $self->{testnumber}".
138                                  " *------------------------------");
139          $self->_testout("|  $string ");
# Line 104 | Line 154 | sub verify {
154          open ( FILE1, "<$file1" ) or die "Cannot Read Test Output $file1 $!\n";
155          open ( FILE2, "<$file2" ) or die "Cannot Read Benchmark file ".
156                                                          "$file2 $!\n";
157 <        while ( $f1=<FILE1> ) {
158 <         $f2=<FILE2>;
159 <         if ( $f2 ne $f1 ) {
160 <           print "T:\n$f1\nB:$f2\n";
157 >        while ( $f1=<FILE2> ) {
158 >         $f2=<FILE1>;
159 >         if ( (!defined $f2 ) || ( ! defined $f1) || ($f2 ne $f1 )) {
160 >           #print "T:\n$f1\nB:$f2\n";
161             $same=0;
162           }
163          }
# Line 123 | Line 173 | sub verifydir {
173          my $name=shift;
174  
175          if ( -d "$name" ) {
176 <           $self->testpass();
176 >           $self->testpass("Directory $name exists - test passed");
177          }
178          else {
179             $self->testfail("Directory $name does not exist");
180          }
181   }
182  
183 + sub verifyexists {
184 +        my $self=shift;
185 +        my $name=shift;
186 +
187 +        if ( -e "$name" ) {
188 +           $self->testpass("$name exists - test passed");
189 +        }
190 +        else {
191 +           $self->testfail("$name does not exist");
192 +        }
193 + }
194 +
195 + sub testswitch {
196 +        my $self=shift;
197 +        my $bool=shift;
198 +        my $string1=shift;
199 +        my $string2=shift;
200 +
201 +        if ( ! $bool ) {
202 +           $self->testpass($string1);
203 +        }
204 +        else {
205 +           $self->testfail($string2);
206 +        }
207 + }
208 +
209   sub testfail {
210          my $self=shift;
211          my $string=shift;
# Line 196 | Line 272 | sub _new {
272  
273          # Data Initialisation
274          $self->{testclass}=$class;
275 <        $self->{classname}=$module;
275 >        ($self->{classname}=$module)=~s/::/\//g;
276          ($self->{class}=$module)=~s/.*\///g;
277          $self->{classfile}=$ENV{SCRAM_HOME}."/src/".$self->{classname}."\.pm";
202        $self->{testclassfile}=$path."/test/".$self->{testclass}."\.pm";
278          $self->init(@_);
279          $self->analyseInterface();
280          delete $self->{expect};
# Line 213 | Line 288 | sub testinterface {
288          my $self=shift;
289          my $subname=shift;
290          my $myreturn;
291 +        my $expected;
292  
293          $self->_checkdoc($subname);
294          $self->{inttest}{$subname}++;
295          $self->_testout(">Trying interface $subname ");
296          my $args=join ', ', @_;
297          $self->_testout( " (".$args.")" );
298 +        $num=0;
299          if ( exists $self->{expect} ) {
300 <          @myreturn=($self->{object}->$subname(@_));
301 <          if ( "@myreturn" eq $self->{expect} ) {
302 <            $self->testpass("OK - returned as expected");
303 <          }
304 <          else  {
305 <            $self->testfail("Expecting $self->{expect}, got @myreturn");
300 >         print "Testing Expected Values against actual returns ....\n";
301 >         @mylist=eval { $self->{object}->$subname(@_); };
302 >          die "Test Failed $@\n" if $@;
303 >         my $nrv=$#mylist+1; my $nrve=$#{$self->{expect}}+1;
304 >         print $nrv." values returned ".$nrve." expected\n";
305 >         if ( $nrv != $nrve ) {
306 >          $self->testfail("Number of returned values != that expected");
307 >         }
308 >         if ( defined @mylist ) {
309 >         # size check
310 >         if ( $#mylist != $#{$self->{expect}} ) {
311 >                $self->testfail("Number of returned values inconsistent");
312 >         }
313 >         foreach $myreturn ( @mylist ) {
314 >          if ( ! defined $myreturn ) {
315 >                print "Undefined Value Passed Back\n";
316            }
317 <          return @myreturn;
317 >          elsif ( $myreturn=~/HASH/ ) {
318 >             print "Hash Ref ".ref($myreturn)." returned\n";
319 >          }
320 >          elsif ( $myreturn=~/CODE/ ) {
321 >             print "Code Ref returned\n";
322 >          }
323 >          elsif ( $myreturn=~/ARRAY/ ) {
324 >            print "Array Ref returned\n";
325 >          }
326 >          else {
327 >            $expected=$self->{expect}[$num++];
328 >            if ( $myreturn eq $expected ) { #simple return case
329 >             $self->testpass("OK - returned as expected ($expected)");
330 >            }
331 >            else  {
332 >              $self->testfail("Expecting $expected, got ".
333 >                                                $myreturn);
334 >            }
335 >          }
336 >        } # end foreach block
337 >        }
338 >          return @mylist;
339          }
340          else {
341            return ($self->{object}->$subname(@_));
# Line 239 | Line 347 | sub testinterface {
347   #
348   sub expect {
349          my $self=shift;
242        my $string=shift;
350  
351 <        $self->{expect}=$string;
351 >        push @{$self->{expect}}, @_;
352   }
353  
354   sub clearexpect {
# Line 339 | Line 446 | sub analyseInterface {
446          }
447          close SRCIN;
448   }
449 +
450 + sub cleantemp {
451 +        my $self=shift;
452 +        use File::Path;
453 +        rmtree($self->temparea());
454 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines