Re: [Perlunit-users] Comparing deeply nested data structures.
Status: Beta
Brought to you by:
mca1001
From: Adrian H. <ad...@qu...> - 2007-05-25 13:12:10
|
Hi Alain, Apologies for the belated response. Been in bed most of this week with evil lurgy and have been avoiding e-mail - and computers in general :-) On 23 May 2007, at 21:06, Desilets, Alain wrote: > I finally got round to trying assert_deep_equals(). Unfortunately, I > don't get the kind of nice diagnostic message you mentioned in your > message of a week ago. Attached is a sample test program. When I > run it, > all I get is the following error message: [snip] You're not getting the diagnostic because you gave your own description string for the failure. Leave it out and you'll get the diagnostics. As an aside - you should be passing class names as 'Strings' nor BareWords. It's better style and will save you the no warnings/ warnings. You can also take advantage of the fact that packages are lexically scoped to avoid having to remember to switch back to the main package, inherit the new() function in your test case, and use the include_tests() method in your test suite. Rolling all of this together would get you: ---- { package BlahTest; use base qw( Test::Unit::TestCase ); sub test_blah { my ( $self ) = @_; my $x = [ {'hello'=> 1 }, 1 ]; my $y = [ {'hello'=> 1 }, 2 ]; $self->assert_deep_equals( $x, $y ); } } { package AllTests; use base qw(Test::Unit::TestSuite); sub name { 'Testing assert_deep_equals' } sub include_tests { 'BlahTest' }; } use Test::Unit::TestRunner; Test::Unit::TestRunner->new->start( 'AllTests' ); ---- which will give you ---- .F Time: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) !!!FAILURES!!! Test Results: Run: 1, Failures: 1, Errors: 0 There was 1 failure: 1) BlahTest.pm:13 - test_blah(BlahTest) Structures begin differing at: $a->[1] = '1' $b->[1] = '2' Test was not successful. ---- and - just for fun - here's a version in the Test::Class style: ---- { package BlahTest; use base qw( Test::Class ); use Test::More; sub show_how_is_deeply_works : Test { my $x = [ {'hello'=> 1 }, 1 ]; my $y = [ {'hello'=> 1 }, 2 ]; is_deeply $x, $y; } } Test::Class->runtests; ---- which would give you: ---- 1..1 not ok 1 - show how is deeply works # Failed test 'show how is deeply works' # at BlahTest.pm line 13. # (in BlahTest->show_how_is_deeply_works) # Structures begin differing at: # $got->[1] = '1' # $expected->[1] = '2' # Looks like you failed 1 test of 1. ---- Cheers, Adrian |