summaryrefslogtreecommitdiff
path: root/t/TestDriver.pm
blob: 274e5d36f4bd239274a4e57a3acb6666326becb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# driver.pm - common test driver code

use Test::More ;

BEGIN {
	*CORE::GLOBAL::syswrite =
	sub($$$;$) { my( $h, $b, $s, $o ) = @_; CORE::syswrite $h, $b, $s, $o} ;
#	sub(*\$$;$) { my( $h, $b, $s, $o ) = @_; CORE::syswrite $h, $b, $s, $o } ;

	*CORE::GLOBAL::sysread =
	sub($$$;$) { my( $h, $b, $s, $o ) = @_; CORE::sysread $h, $b, $s, $o } ;
#	sub(*\$$;$) { my( $h, $b, $s, $o ) = @_; CORE::sysread $h, $b, $s, $o } ;

	*CORE::GLOBAL::rename =
	sub($$) { my( $old, $new ) = @_; CORE::rename $old, $new } ;

	*CORE::GLOBAL::sysopen =
	sub($$$;$) { my( $h, $n, $m, $p ) = @_; CORE::sysopen $h, $n, $m, $p } ;
#	sub(*$$;$) { my( $h, $n, $m, $p ) = @_; CORE::sysopen $h, $n, $m, $p } ;
}

sub test_driver {

	my( $tests ) = @_ ;

use Data::Dumper ;

# plan for one expected ok() call per test

	plan( tests => scalar @{$tests} ) ;

# loop over all the tests

	foreach my $test ( @{$tests} ) {

#print Dumper $test ;

		if ( $test->{skip} ) {
			ok( 1, "SKIPPING $test->{name}" ) ;
			next ;
		}

		my $override = $test->{override} ;

# run any setup sub before this test. this can is used to modify the
# object for this test or create test files and data.

		if( my $pretest = $test->{pretest} ) {

			$pretest->($test) ;
		}

		if( my $sub = $test->{sub} ) {

			my $args = $test->{args} ;

			local( $^W ) ;
			local *{"CORE::GLOBAL::$override"} = sub {}
				if $override ;

			$test->{result} = eval { $sub->( @{$args} ) } ;

			if ( $@ ) {

# if we had an error and expected it, we pass this test

				if ( $test->{error} &&
				     $@ =~ /$test->{error}/ ) {

					$test->{ok} = 1 ;
				}
				else {
					print "unexpected error: $@\n" ;
					$test->{ok} = 0 ;
				}
			}
		}

		if( my $posttest = $test->{posttest} ) {

			$posttest->($test) ;
		}

		ok( $test->{ok}, $test->{name} ) if exists $test->{ok} ;
		is( $test->{result}, $test->{expected}, $test->{name} ) if
			exists $test->{expected} ;

	}
}

1 ;