Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Test::More is_deeply and binary data

by ig (Vicar)
on Nov 08, 2013 at 22:34 UTC ( #1061783=perlquestion: print w/replies, xml ) Need Help??

ig has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a test using Test::More and is_deeply where the expected result has a string with control characters, as in the following example:

use Test::More; use Data::Dumper; $Data::Dumper::Useqq = 1; my $expect = "\a"; my $got = "\b"; is_deeply($got, $expect, "example test") or diag "got: " . Dumper($got) . " expected: " . Dumper($expect);

When the test fails, is_deeply prints the values. As they are not human readable, it is difficult to see the difference. In this case the impact on the terminal is minimal but with arbitrary data it can be quite disruptive.

While I can add diagnostics using Data::Dumper or other serialization, as in the example above, this doesn't stop is_deeply printing the binary data and potentially corrupting the terminal display.

There does not appear to be a provision in Test::More to have is_deeply serialize the values to human readable form.

I tried searching, but was overwhelmed with irrelevant results and was unsuccessful trying to narrow the search, so pointers or suggestions would be much appreciated.

Replies are listed 'Best First'.
Re: Test::More is_deeply and binary data
by tobyink (Canon) on Nov 08, 2013 at 22:54 UTC

    This should do the trick:

    if (eval { require Unicode::Debug }) { my $builder = Test::More->builder; binmode $builder->$_, ':via(UnicodeDebug)' for qw/ output failure_output todo_output /; }

    It will pass all Test::More's output via Unicode::Debug, but only if Unicode::Debug is installed.

    (Note that Unicode::Debug currently outputs some warning messages under Perl 5.18.x. I really need to fix that.)

    Update: Unicode::Debug 0.002 fixes these warnings, and includes the above example.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

      That works very nicely!! Thanks for the quick response. I'll read up on Test::Builder, which I haven't looked at before.

Re: Test::More is_deeply and binary data
by boftx (Deacon) on Nov 08, 2013 at 22:51 UTC

    This is a "shoot-from-the-hip" response with the sole intent of opening a possible approach, so ...

    I recall having had to dink around with STDOUT/STDERR in a unit test once to keep STDERR from polluting the screen. Perhaps you could set the __WARN__ (?) handler and capture the output to a string where you could then massage it as needed. As I said above, this is just a thought without having had enough Scotch beforehand.

    The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.
Re: Test::More is_deeply and binary data
by kschwab (Vicar) on Nov 09, 2013 at 13:37 UTC

    Digging around in the source, it appears that setting the global $^C (aka $COMPILING) will suppress the diagnostic printing. So you could do this:

    sub is_deeply_quiet { local $^C=1; is_deeply(@_); }

    ...and call it instead. I have no idea though, if that creates other undesirable side effects.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1061783]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-09-28 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (143 votes). Check out past polls.

    Notices?