Skip to content

Commit

Permalink
U::N: Improve ok() function
Browse files Browse the repository at this point in the history
This hand-rolled function can serve as an is() when called with a 2nd
argument.  And in this module much of the data is non-printable so
should be output escaped.

None of the calls have a name parameter, so this can work.
  • Loading branch information
khwilliamson committed Jul 23, 2021
1 parent 2b8c190 commit c0605d3
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions dist/Unicode-Normalize/Normalize.pm
Expand Up @@ -52,15 +52,36 @@ sub unpack_U {
return unpack('U*', shift(@_).pack('U*'));
}

sub get_printable_string ($) {
use bytes;
my $s = shift;

# DeMorgan's laws cause this to mean ascii printables
return $s if $s =~ /[^[:^ascii:][:^print:]]/;

return join " ", map { sprintf "\\x%02x", ord $_ } split "", $s;
}

sub ok ($$;$) {
my $count_ref = shift; # Test number in caller
my $p = my $r = shift;
my $x;
if (@_) {
my $x = shift;
$p = !defined $x ? !defined $r : !defined $r ? 0 : $r eq $x;
$x = shift;
$p = !defined $x ? !defined $r : !defined $r ? 0 : $r eq $x;
}

print $p ? "ok" : "not ok", ' ', ++$$count_ref, "\n";

return if $p;

my (undef, $file, $line) = caller(1);
print STDERR "# Failed test $$count_ref at $file line $line\n";

return unless defined $x;

print STDERR "# got ", get_printable_string($r), "\n";
print STDERR "# expected ", get_printable_string($x), "\n";
}

require Exporter;
Expand Down

0 comments on commit c0605d3

Please sign in to comment.