ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/TestClass.pm
Revision: 1.3
Committed: Fri Jul 2 16:36:12 1999 UTC (25 years, 10 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.2: +0 -1 lines
Log Message:
 Bug fix in clearexpect

File Contents

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