e802d2dcf9a07711a5fd32cc5f634ff4e3813c16
[urisagit/Perl-Docs.git] / t / TestDriver.pm
1 # driver.pm - common test driver code
2
3 use Test::More ;
4
5 BEGIN {
6         *CORE::GLOBAL::syswrite =
7         sub(*\$$;$) { my( $h, $b, $s ) = @_; CORE::syswrite $h, $b, $s } ;
8
9         *CORE::GLOBAL::sysread =
10         sub(*\$$;$) { my( $h, $b, $s ) = @_; CORE::sysread $h, $b, $s } ;
11
12         *CORE::GLOBAL::rename =
13         sub($$) { my( $old, $new ) = @_; CORE::rename $old, $new } ;
14
15         *CORE::GLOBAL::sysopen =
16         sub(*$$;$) { my( $h, $n, $m, $p ) = @_; CORE::sysopen $h, $n, $m, $p } ;
17 }
18
19 sub test_driver {
20
21         my( $tests ) = @_ ;
22
23 use Data::Dumper ;
24
25 # plan for one expected ok() call per test
26
27         plan( tests => scalar @{$tests} ) ;
28
29 # loop over all the tests
30
31         foreach my $test ( @{$tests} ) {
32
33 #print Dumper $test ;
34
35                 if ( $test->{skip} ) {
36                         ok( 1, "SKIPPING $test->{name}" ) ;
37                         next ;
38                 }
39
40                 my $override = $test->{override} ;
41
42 # run any setup sub before this test. this can is used to modify the
43 # object for this test or create test files and data.
44
45                 if( my $pretest = $test->{pretest} ) {
46
47                         $pretest->($test) ;
48                 }
49
50                 if( my $sub = $test->{sub} ) {
51
52                         my $args = $test->{args} ;
53
54                         local( $^W ) ;
55                         local *{"CORE::GLOBAL::$override"} = sub {}
56                                 if $override ;
57
58                         $test->{result} = eval { $sub->( @{$args} ) } ;
59
60                         if ( $@ ) {
61
62 # if we had an error and expected it, we pass this test
63
64                                 if ( $test->{error} &&
65                                      $@ =~ /$test->{error}/ ) {
66
67                                         $test->{ok} = 1 ;
68                                 }
69                                 else {
70                                         print "unexpected error: $@\n" ;
71                                         $test->{ok} = 0 ;
72                                 }
73                         }
74                 }
75
76                 if( my $posttest = $test->{posttest} ) {
77
78                         $posttest->($test) ;
79                 }
80
81                 ok( $test->{ok}, $test->{name} ) if exists $test->{ok} ;
82                 is( $test->{result}, $test->{expected}, $test->{name} ) if
83                         exists $test->{expected} ;
84
85         }
86 }
87
88 1 ;