ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/TestClass.pm
Revision: 1.5
Committed: Tue Nov 16 09:24:36 1999 UTC (25 years, 5 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.4: +32 -11 lines
Log Message:
Add cleantemp function

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