This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In test.pl, change like() and unlike() to avoid copying the tested scalar.
[perl5.git] / t / re / ReTest.pl
CommitLineData
9d45b377
YO
1#!./perl
2#
3# This is the test subs used for regex testing.
4# This used to be part of re/pat.t
5use warnings;
6use strict;
7use 5.010;
8use base qw/Exporter/;
9use Carp;
10use vars qw(
11 $EXPECTED_TESTS
12 $TODO
13 $Message
9d45b377 14 $BugId
9d45b377
YO
15 $running_as_thread
16 $IS_ASCII
17 $IS_EBCDIC
18 $ordA
19);
20
21$| = 1;
22
23$Message ||= "Noname test";
24
25our $ordA = ord ('A'); # This defines ASCII/UTF-8 vs EBCDIC/UTF-EBCDIC
26# This defined the platform.
27our $IS_ASCII = $ordA == 65;
28our $IS_EBCDIC = $ordA == 193;
29
30use vars '%Config';
31eval 'use Config'; # Defaults assumed if this fails
32
33my $test = 0;
34my $done_plan;
35sub plan {
36 my (undef,$tests)= @_;
37 if (defined $tests) {
38 die "Number of tests already defined! ($EXPECTED_TESTS)"
39 if $EXPECTED_TESTS;
40 $EXPECTED_TESTS= $tests;
41 }
42 if ($EXPECTED_TESTS) {
43 print "1..$EXPECTED_TESTS\n" if !$done_plan++;
44 } else {
45 print "Number of tests not declared!";
46 }
47}
48
49sub pretty {
50 my ($mess) = @_;
51 $mess =~ s/\n/\\n/g;
52 $mess =~ s/\r/\\r/g;
53 $mess =~ s/\t/\\t/g;
54 $mess =~ s/([\00-\37\177])/sprintf '\%03o', ord $1/eg;
55 $mess =~ s/#/\\#/g;
56 $mess;
57}
58
59sub safe_globals {
1c6f1211 60 defined($_) and s/#/\\#/g for $BugId, $TODO;
9d45b377
YO
61}
62
63sub _ok {
64 my ($ok, $mess, $error) = @_;
65 plan();
66 safe_globals();
67 $mess = pretty ($mess // $Message);
68 $mess .= "; Bug $BugId" if defined $BugId;
9d45b377
YO
69 $mess .= " # TODO $TODO" if defined $TODO;
70
71 my $line_nr = (caller(1)) [2];
72
73 printf "%sok %d - %s\n",
74 ($ok ? "" : "not "),
75 ++ $test,
76 "$mess\tLine $line_nr";
77
78 unless ($ok) {
79 print "# Failed test at line $line_nr\n" unless defined $TODO;
c0dc474d 80 if ($error) {
9d45b377
YO
81 no warnings 'utf8';
82 chomp $error;
83 $error = join "\n#", map {pretty $_} split /\n\h*#/ => $error;
84 $error = "# $error" unless $error =~ /^\h*#/;
85 print $error, "\n";
86 }
87 }
88
89 return $ok;
90}
91
92# Force scalar context on the pattern match
93sub ok ($;$$) {_ok $_ [0], $_ [1], $_ [2]}
94sub nok ($;$$) {_ok !$_ [0], "Failed: " . ($_ [1] // $Message), $_ [2]}
95
96
97sub skip {
98 my $why = shift;
99 safe_globals();
100 $why =~ s/\n.*//s;
101 $why .= "; Bug $BugId" if defined $BugId;
76220d5e
NC
102 my $ok;
103 if (defined $TODO) {
104 $why = "TODO & SKIP $why $TODO";
105 $ok = "not ok";
106 } else {
107 $why = "SKIP $why";
108 $ok = "ok";
109 }
110
9d45b377
YO
111 my $n = shift // 1;
112 my $line_nr = (caller(0)) [2];
113 for (1 .. $n) {
114 ++ $test;
76220d5e 115 print "$ok $test # $why\tLine $line_nr\n";
9d45b377
YO
116 }
117 no warnings "exiting";
118 last SKIP;
119}
120
121sub iseq ($$;$) {
122 my ($got, $expect, $name) = @_;
123
124 $_ = defined ($_) ? "'$_'" : "undef" for $got, $expect;
125
126 my $ok = $got eq $expect;
127 my $error = "# expected: $expect\n" .
128 "# result: $got";
129
130 _ok $ok, $name, $error;
131}
132
133sub isneq ($$;$) {
134 my ($got, $expect, $name) = @_;
135 my $todo = $TODO ? " # TODO $TODO" : '';
136
137 $_ = defined ($_) ? "'$_'" : "undef" for $got, $expect;
138
139 my $ok = $got ne $expect;
140 my $error = "# results are equal ($got)";
141
142 _ok $ok, $name, $error;
143}
144
145
146sub eval_ok ($;$) {
147 my ($code, $name) = @_;
148 local $@;
149 if (ref $code) {
150 _ok eval {&$code} && !$@, $name;
151 }
152 else {
153 _ok eval ($code) && !$@, $name;
154 }
155}
156
157sub must_die {
158 my ($code, $pattern, $name) = @_;
88743d87 159 Carp::confess("Bad pattern") unless $pattern;
9d45b377
YO
160 undef $@;
161 ref $code ? &$code : eval $code;
162 my $r = $@ && $@ =~ /$pattern/;
88743d87 163 _ok $r, $name // "\$\@ =~ /$pattern/";
9d45b377
YO
164}
165
166sub must_warn {
167 my ($code, $pattern, $name) = @_;
45bd432f 168 Carp::confess("Bad pattern") unless $pattern;
9d45b377
YO
169 my $w;
170 local $SIG {__WARN__} = sub {$w .= join "" => @_};
171 use warnings 'all';
172 ref $code ? &$code : eval $code;
173 my $r = $w && $w =~ /$pattern/;
174 $w //= "UNDEF";
29fdfc93 175 _ok $r, $name // "Got warning /$pattern/",
9d45b377
YO
176 "# expected: /$pattern/\n" .
177 "# result: $w";
178}
179
180sub may_not_warn {
181 my ($code, $name) = @_;
182 my $w;
183 local $SIG {__WARN__} = sub {$w .= join "" => @_};
184 use warnings 'all';
185 ref $code ? &$code : eval $code;
d728c370 186 _ok !$w, $name, "Got warning '$w'";
9d45b377
YO
187}
188
1891;