test.pl runperl() nits from Chris Nandor and Craig Berry,
[p5sagit/p5-mst-13.2.git] / t / test.pl
1 #
2 # t/test.pl - most of Test::More functionality without the fuss
3 #
4
5 my $test = 1;
6 my $planned;
7
8 $TODO = 0;
9
10 sub plan {
11     my $n;
12     if (@_ == 1) {
13         $n = shift;
14     } else {
15         my %plan = @_;
16         $n = $plan{tests}; 
17     }
18     print "1..$n\n";
19     $planned = $n;
20 }
21
22 END {
23     my $ran = $test - 1;
24     if (defined $planned && $planned != $ran) {
25         print "# Looks like you planned $planned tests but ran $ran.\n";
26     }
27 }
28
29 sub skip_all {
30     if (@_) {
31         print "1..0 - @_\n";
32     } else {
33         print "1..0\n";
34     }
35     exit(0);
36 }
37
38 sub _ok {
39     my ($pass, $where, $name, @mess) = @_;
40     # Do not try to microoptimize by factoring out the "not ".
41     # VMS will avenge.
42     my $out;
43     if ($name) {
44         $out = $pass ? "ok $test - $name" : "not ok $test - $name";
45     } else {
46         $out = $pass ? "ok $test" : "not ok $test";
47     }
48
49     $out .= " # TODO $TODO" if $TODO;
50     print "$out\n";
51
52     unless ($pass) {
53         print "# Failed $where\n";
54     }
55
56     # Ensure that the message is properly escaped.
57     print map { /^#/ ? "$_\n" : "# $_\n" } 
58           map { split /\n/ } @mess if @mess;
59
60     $test++;
61
62     return $pass;
63 }
64
65 sub _where {
66     my @caller = caller(1);
67     return "at $caller[1] line $caller[2]";
68 }
69
70 sub ok {
71     my ($pass, $name, @mess) = @_;
72     _ok($pass, _where(), $name, @mess);
73 }
74
75 sub _q {
76     my $x = shift;
77     return 'undef' unless defined $x;
78     my $q = $x;
79     $q =~ s/'/\\'/;
80     return "'$q'";
81 }
82
83 sub is {
84     my ($got, $expected, $name, @mess) = @_;
85     my $pass = $got eq $expected;
86     unless ($pass) {
87         unshift(@mess, "#      got "._q($got)."\n",
88                        "# expected "._q($expected)."\n");
89     }
90     _ok($pass, _where(), $name, @mess);
91 }
92
93 sub isnt {
94     my ($got, $isnt, $name, @mess) = @_;
95     my $pass = $got ne $isnt;
96     unless( $pass ) {
97         unshift(@mess, "# it should not be "._q($got)."\n",
98                        "# but it is.\n");
99     }
100     _ok($pass, _where(), $name, @mess);
101 }
102
103 # Note: this isn't quite as fancy as Test::More::like().
104 sub like {
105     my ($got, $expected, $name, @mess) = @_;
106     my $pass;
107     if (ref $expected eq 'Regexp') {
108         $pass = $got =~ $expected;
109         unless ($pass) {
110             unshift(@mess, "#      got '$got'\n");
111         }
112     } else {
113         $pass = $got =~ /$expected/;
114         unless ($pass) {
115             unshift(@mess, "#      got '$got'\n",
116                            "# expected /$expected/\n");
117         }
118     }
119     _ok($pass, _where(), $name, @mess);
120 }
121
122 sub pass {
123     _ok(1, '', @_);
124 }
125
126 sub fail {
127     _ok(0, _where(), @_);
128 }
129
130 sub next_test {
131     $test++
132 }
133
134 # Note: can't pass multipart messages since we try to
135 # be compatible with Test::More::skip().
136 sub skip {
137     my $why = shift;
138     my $n    = @_ ? shift : 1;
139     for (1..$n) {
140         print "ok $test # skip: $why\n";
141         $test++;
142     }
143     local $^W = 0;
144     last SKIP;
145 }
146
147 sub eq_array {
148     my ($ra, $rb) = @_;
149     return 0 unless $#$ra == $#$rb;
150     for my $i (0..$#$ra) {
151         return 0 unless $ra->[$i] eq $rb->[$i];
152     }
153     return 1;
154 }
155
156 sub require_ok {
157     my ($require) = @_;
158     eval <<REQUIRE_OK;
159 require $require;
160 REQUIRE_OK
161     _ok(!$@, _where(), "require $require");
162 }
163
164 sub use_ok {
165     my ($use) = @_;
166     eval <<USE_OK;
167 use $use;
168 USE_OK
169     _ok(!$@, _where(), "use $use");
170 }
171
172 # runperl - Runs a separate perl interpreter.
173 # Arguments :
174 #   switches => [ command-line switches ]
175 #   nolib    => 1 # don't use -I../lib (included by default)
176 #   prog     => one-liner (avoid quotes)
177 #   progfile => perl script
178 #   stdin    => string to feed the stdin
179 #   stderr   => redirect stderr to stdout
180 #   args     => [ command-line arguments to the perl program ]
181 #   verbose  => print the command line
182
183 my $is_mswin    = $^O eq 'MSWin32';
184 my $is_netware  = $^O eq 'NetWare';
185 my $is_macos    = $^O eq 'MacOS';
186 my $is_vms      = $^O eq 'VMS';
187
188 sub _quote_args {
189     my ($runperl, $args) = @_;
190
191     foreach (@$args) {
192         # In VMS protect with doublequotes because otherwise
193         # DCL will lowercase -- unless already doublequoted.
194         $_ = q(").$_.q(") if $is_vms && !/^\"/;
195         $$runperl .= ' ' . $_;
196     }
197 }
198
199 sub runperl {
200     my %args = @_;
201     my $runperl = $^X;
202     if (defined $args{switches}) {
203         _quote_args(\$runperl, $args{switches});
204     }
205     unless (defined $args{nolib}) {
206         if ($is_macos && $args{stderr}) {
207             $runperl .= ' -I::lib';
208             # Use UNIX style error message instead of MPW style.
209             $runperl .= ' -MMac::err=unix' if $args{stderr};
210         }
211         else {
212             $runperl .= ' "-I../lib"'; # doublequotes because of VMS
213         }
214     }
215     if (defined $args{prog}) {
216         if ($is_mswin || $is_netware || $is_vms) {
217             $runperl .= qq( -e ") . $args{prog} . qq(");
218         }
219         else {
220             $runperl .= qq( -e ') . $args{prog} . qq(');
221         }
222     } elsif (defined $args{progfile}) {
223         $runperl .= qq( "$args{progfile}");
224     }
225     if (defined $args{stdin}) {
226         if ($is_mswin || $is_netware || $is_vms) {
227             $runperl = qq{$^X -e "print q(} .
228                 $args{stdin} . q{)" | } . $runperl;
229         }
230         else {
231             $runperl = qq{$^X -e 'print q(} .
232                 $args{stdin} . q{)' | } . $runperl;
233         }
234     }
235     if (defined $args{args}) {
236         _quote_args(\$runperl, $args{args});
237     }
238     $runperl .= ' 2>&1'          if  $args{stderr} && !$is_macos;
239     $runperl .= " \xB3 Dev:Null" if !$args{stderr} &&  $is_macos;
240     if ($args{verbose}) {
241         my $runperldisplay = $runperl;
242         $runperldisplay =~ s/\n/\n\#/g;
243         print "# $runperldisplay\n";
244     }
245     my $result = `$runperl`;
246     $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
247     return $result;
248 }
249
250 1;