Subject: | [PATCH] Improving diagnostics for non-printable characters. |
I am in the process of writing tests that compare binary data that
includes non-printable characters. As it stands, Test::More does not
provide very useful diagnostics for when these tests fail. The attached
patch attempts to solve this problem by replacing non-printable
characters with equivalent escape sequences in _is_diag. It also
modifies t/fail-more.t to test the failure diagnostics.
The patch should only affect is_eq tests.
Subject: | is_diag-unprintables.patch |
diff -urN Test-Simple-0.80~orig/lib/Test/Builder.pm Test-Simple-0.80~mod/lib/Test/Builder.pm
--- Test-Simple-0.80~orig/lib/Test/Builder.pm 2008-04-06 07:26:10.000000000 -0800
+++ Test-Simple-0.80~mod/lib/Test/Builder.pm 2008-05-07 01:45:23.000000000 -0800
@@ -563,6 +563,14 @@
if( defined $$val ) {
if( $type eq 'eq' ) {
# quote and force string context
+ $$val =~ s{
+ (\\)|([^[:print:]\n])
+ }{
+ defined $1
+ ? "\\\\"
+ : sprintf('\\x%02X', ord $2)
+ }egx;
+
$$val = "'$$val'"
}
else {
diff -urN Test-Simple-0.80~orig/t/fail-more.t Test-Simple-0.80~mod/t/fail-more.t
--- Test-Simple-0.80~orig/t/fail-more.t 2007-09-13 19:12:35.000000000 -0800
+++ Test-Simple-0.80~mod/t/fail-more.t 2008-05-07 02:14:49.000000000 -0800
@@ -45,7 +45,7 @@
package main;
require Test::More;
-my $Total = 30;
+my $Total = 33;
Test::More->import(tests => $Total);
# This should all work in the presence of a __DIE__ handler.
@@ -66,10 +66,13 @@
ERR
#line 40
-is( "foo", "bar", 'foo is bar?');
-is( undef, '', 'undef is empty string?');
-is( undef, 0, 'undef is 0?');
-is( '', 0, 'empty string is 0?' );
+is( "foo", "bar", 'foo is bar?');
+is( undef, '', 'undef is empty string?');
+is( undef, 0, 'undef is 0?');
+is( '', 0, 'empty string is 0?' );
+is( "\0", '', 'nul is ""?' );
+is( "\0", "\xFF", 'nul is \\xFF?' );
+is( "\1foo", "\1bar", '\\1foo is \\1bar?' );
err_ok( <<ERR );
# Failed test 'foo is bar?'
# at $0 line 40.
@@ -87,6 +90,18 @@
# at $0 line 43.
# got: ''
# expected: '0'
+# Failed test 'nul is ""?'
+# at $0 line 44.
+# got: '\\x00'
+# expected: ''
+# Failed test 'nul is \\xFF?'
+# at $0 line 45.
+# got: '\\x00'
+# expected: '\\xFF'
+# Failed test '\\1foo is \\1bar?'
+# at $0 line 46.
+# got: '\\x01foo'
+# expected: '\\x01bar'
ERR
#line 45
@@ -290,6 +305,9 @@
not ok - undef is empty string?
not ok - undef is 0?
not ok - empty string is 0?
+not ok - nul is ""?
+not ok - nul is \\xFF?
+not ok - \\1foo is \\1bar?
not ok - foo isnt foo?
not ok - foo isn't foo?
not ok - undef isnt undef?