This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
s/parse_perl/parse_body/
[perl5.git] / t / test.pl
CommitLineData
69026470
JH
1#
2# t/test.pl - most of Test::More functionality without the fuss
3#
4
5my $test = 1;
6my $planned;
7
7d932aad
MS
8$TODO = 0;
9
69026470
JH
10sub 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
22END {
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
29sub skip_all {
30 if (@_) {
31 print "1..0 - @_\n";
32 } else {
33 print "1..0\n";
34 }
35 exit(0);
36}
37
38sub _ok {
7d932aad 39 my ($pass, $where, $name, @mess) = @_;
69026470
JH
40 # Do not try to microoptimize by factoring out the "not ".
41 # VMS will avenge.
7d932aad
MS
42 my $out;
43 if ($name) {
44 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
69026470 45 } else {
7d932aad 46 $out = $pass ? "ok $test" : "not ok $test";
69026470 47 }
7d932aad
MS
48
49 $out .= " # TODO $TODO" if $TODO;
50 print "$out\n";
51
69026470
JH
52 unless ($pass) {
53 print "# Failed $where\n";
54 }
7d932aad
MS
55
56 # Ensure that the message is properly escaped.
57 print map { /^#/ ? "$_\n" : "# $_\n" }
58 map { split /\n/ } @mess if @mess;
59
69026470 60 $test++;
1577bb16
MS
61
62 return $pass;
69026470
JH
63}
64
65sub _where {
66 my @caller = caller(1);
67 return "at $caller[1] line $caller[2]";
68}
69
70sub ok {
7d932aad
MS
71 my ($pass, $name, @mess) = @_;
72 _ok($pass, _where(), $name, @mess);
69026470
JH
73}
74
b3c72391
JH
75sub _q {
76 my $x = shift;
77 return 'undef' unless defined $x;
78 my $q = $x;
79 $q =~ s/'/\\'/;
80 return "'$q'";
81}
82
69026470 83sub is {
7d932aad 84 my ($got, $expected, $name, @mess) = @_;
69026470
JH
85 my $pass = $got eq $expected;
86 unless ($pass) {
b3c72391
JH
87 unshift(@mess, "# got "._q($got)."\n",
88 "# expected "._q($expected)."\n");
69026470 89 }
7d932aad 90 _ok($pass, _where(), $name, @mess);
69026470
JH
91}
92
3e90d5a3
MS
93sub isnt {
94 my ($got, $isnt, $name, @mess) = @_;
95 my $pass = $got ne $isnt;
96 unless( $pass ) {
b3c72391 97 unshift(@mess, "# it should not be "._q($got)."\n",
3e90d5a3
MS
98 "# but it is.\n");
99 }
100 _ok($pass, _where(), $name, @mess);
101}
102
69026470
JH
103# Note: this isn't quite as fancy as Test::More::like().
104sub like {
7d932aad 105 my ($got, $expected, $name, @mess) = @_;
69026470
JH
106 my $pass;
107 if (ref $expected eq 'Regexp') {
108 $pass = $got =~ $expected;
109 unless ($pass) {
7d932aad 110 unshift(@mess, "# got '$got'\n");
69026470
JH
111 }
112 } else {
113 $pass = $got =~ /$expected/;
114 unless ($pass) {
7d932aad
MS
115 unshift(@mess, "# got '$got'\n",
116 "# expected /$expected/\n");
69026470
JH
117 }
118 }
7d932aad 119 _ok($pass, _where(), $name, @mess);
69026470
JH
120}
121
122sub pass {
123 _ok(1, '', @_);
124}
125
126sub fail {
127 _ok(0, _where(), @_);
128}
129
3e90d5a3
MS
130sub next_test {
131 $test++
132}
133
69026470
JH
134# Note: can't pass multipart messages since we try to
135# be compatible with Test::More::skip().
136sub skip {
7d932aad 137 my $why = shift;
982b7cb7 138 my $n = @_ ? shift : 1;
69026470 139 for (1..$n) {
e6c299c8
JH
140 print "ok $test # skip: $why\n";
141 $test++;
69026470
JH
142 }
143 local $^W = 0;
144 last SKIP;
145}
146
147sub 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
156sub require_ok {
157 my ($require) = @_;
158 eval <<REQUIRE_OK;
159require $require;
160REQUIRE_OK
1577bb16 161 _ok(!$@, _where(), "require $require");
69026470
JH
162}
163
164sub use_ok {
165 my ($use) = @_;
166 eval <<USE_OK;
167use $use;
168USE_OK
1577bb16 169 _ok(!$@, _where(), "use $use");
69026470
JH
170}
171
1721;