diff options
author | Karl Williamson <khw@cpan.org> | 2021-07-10 13:02:15 -0600 |
---|---|---|
committer | Karl Williamson <khw@cpan.org> | 2021-07-23 15:33:27 -0700 |
commit | c0605d31d02511d881ffebfc287e1faae9303491 (patch) | |
tree | 197c0445cb23610ecd4903286613a8b3813c71a3 /dist/Unicode-Normalize | |
parent | 2b8c1900114c7542182e5e5f09966838e7f5a0a0 (diff) | |
download | perl-c0605d31d02511d881ffebfc287e1faae9303491.tar.gz |
U::N: Improve ok() function
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.
Diffstat (limited to 'dist/Unicode-Normalize')
-rw-r--r-- | dist/Unicode-Normalize/Normalize.pm | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/dist/Unicode-Normalize/Normalize.pm b/dist/Unicode-Normalize/Normalize.pm index 8d85eda63c..5980b7dccd 100644 --- a/dist/Unicode-Normalize/Normalize.pm +++ b/dist/Unicode-Normalize/Normalize.pm @@ -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; |