3 # This is the test subs used for regex testing.
4 # This used to be part of re/pat.t
27 $Message ||= "Noname test";
29 our $ordA = ord ('A'); # This defines ASCII/UTF-8 vs EBCDIC/UTF-EBCDIC
30 # This defined the platform.
31 our $IS_ASCII = $ordA == 65;
32 our $IS_EBCDIC = $ordA == 193;
35 eval 'use Config'; # Defaults assumed if this fails
40 my (undef,$tests)= @_;
42 die "Number of tests already defined! ($EXPECTED_TESTS)"
44 $EXPECTED_TESTS= $tests;
46 if ($EXPECTED_TESTS) {
47 print "1..$EXPECTED_TESTS\n" if !$done_plan++;
49 print "Number of tests not declared!";
58 $mess =~ s/([\00-\37\177])/sprintf '\%03o', ord $1/eg;
64 defined($_) and s/#/\\#/g for $BugId, $PatchId, $TODO;
68 my ($ok, $mess, $error) = @_;
71 $mess = pretty ($mess // $Message);
72 $mess .= "; Bug $BugId" if defined $BugId;
73 $mess .= "; Patch $PatchId" if defined $PatchId;
74 $mess .= " # TODO $TODO" if defined $TODO;
76 my $line_nr = (caller(1)) [2];
78 printf "%sok %d - %s\n",
81 "$mess\tLine $line_nr";
84 print "# Failed test at line $line_nr\n" unless defined $TODO;
85 if ($error //= $Error) {
88 $error = join "\n#", map {pretty $_} split /\n\h*#/ => $error;
89 $error = "# $error" unless $error =~ /^\h*#/;
97 # Force scalar context on the pattern match
98 sub ok ($;$$) {_ok $_ [0], $_ [1], $_ [2]}
99 sub nok ($;$$) {_ok !$_ [0], "Failed: " . ($_ [1] // $Message), $_ [2]}
106 $why .= "; Bug $BugId" if defined $BugId;
107 # seems like the new harness code doesnt like todo and skip to be mixed.
108 # which seems like a bug in the harness to me. -- dmq
109 #$why .= " # TODO $TODO" if defined $TODO;
112 my $line_nr = (caller(0)) [2];
115 #print "not " if defined $TODO;
116 print "ok $test # skip $why\tLine $line_nr\n";
118 no warnings "exiting";
123 my ($got, $expect, $name) = @_;
125 $_ = defined ($_) ? "'$_'" : "undef" for $got, $expect;
127 my $ok = $got eq $expect;
128 my $error = "# expected: $expect\n" .
131 _ok $ok, $name, $error;
135 my ($got, $expect, $name) = @_;
136 my $todo = $TODO ? " # TODO $TODO" : '';
138 $_ = defined ($_) ? "'$_'" : "undef" for $got, $expect;
140 my $ok = $got ne $expect;
141 my $error = "# results are equal ($got)";
143 _ok $ok, $name, $error;
148 my ($code, $name) = @_;
151 _ok eval {&$code} && !$@, $name;
154 _ok eval ($code) && !$@, $name;
159 my ($code, $pattern, $name) = @_;
160 $pattern //= $DiePattern
161 or Carp::confess("Bad pattern");
163 ref $code ? &$code : eval $code;
164 my $r = $@ && $@ =~ /$pattern/;
165 _ok $r, $name // $Message // "\$\@ =~ /$pattern/";
169 my ($code, $pattern, $name) = @_;
170 $pattern //= $WarnPattern;
172 local $SIG {__WARN__} = sub {$w .= join "" => @_};
174 ref $code ? &$code : eval $code;
175 my $r = $w && $w =~ /$pattern/;
177 _ok $r, $name // $Message // "Got warning /$pattern/",
178 "# expected: /$pattern/\n" .
183 my ($code, $name) = @_;
185 local $SIG {__WARN__} = sub {$w .= join "" => @_};
187 ref $code ? &$code : eval $code;
188 _ok !$w, $name // ($Message ? "$Message (did not warn)"