Re: [Perlunit-users] Comparing deeply nested data structures.
Status: Beta
Brought to you by:
mca1001
From: Desilets, A. <Ala...@nr...> - 2007-05-23 20:09:27
|
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: ----=20 !!!FAILURES!!! Test Results: Run: 1, Failures: 1, Errors: 0 There was 1 failure: 1) test_blah: Test failed Test was not successful. ---- Which is not very helpful for finding where in the structure the difference was found. Am I missing some important configuration for my test harness? Thx. Alain > -----Original Message----- > From: Desilets, Alain=20 > Sent: May 16, 2007 7:35 PM > To: 'Adrian Howard'; per...@li... > Cc: Farley, Benoit > Subject: RE: [Perlunit-users] Comparing deeply nested data structures. >=20 > Great, thanks Adrian. That sounds exactly like what we're looking for. >=20 > Alain > =20 >=20 > > -----Original Message----- > > From: Adrian Howard [mailto:ad...@qu...] > > Sent: May 16, 2007 7:26 PM > > To: per...@li...; Desilets, Alain > > Cc: Farley, Benoit > > Subject: Re: [Perlunit-users] Comparing deeply nested data=20 > structures. > >=20 > >=20 > > On 16 May 2007, at 18:17, Desilets, Alain wrote: > > [snip] > > > I want to easily compare the output of that method to > > expected output, > > > and if the two differ, get a good diagnostic message that=20 > describes=20 > > > exactly how the two differ. > > [snip] > > > Is there something in Perl that would allow me to do this? > >=20 > > In Test::Unit there's assert_deep_equals() - for example: > >=20 > > sub test_using_assert_deep_equals { > > my $self =3D shift; > > $self->assert_deep_equals( > > [ { hello =3D>1}, { world =3D> 1 } ], > > [ { hello =3D>1}, { earth =3D> 1 } ] > > ); > > } > >=20 > > that gives you a diagnostic like > >=20 > > 1) /Users/adrianh/Desktop/test.pl:53 - test_using_assert_deep_equals > > (Comparison::Test) > > Structures begin differing at: > > $a->[1]{earth} =3D Does not exist > > $b->[1]{earth} =3D '1' > >=20 > > > I stumbled across cmp_deeply(), but I can't figure out how > > to use it.=20 > > > It's part of a testing framework different from PerlUnit, > > and I can't > > > seem to be able to use it without running it inside that > > other testing > > > framework (which I don't have time to learn). > >=20 > > It's not that hard to shim the more popular Test::Builder based=20 > > "assertion" modules into Test::Unit. The=20 > off-the-top-of-my-head greasy=20 > > hack of: > >=20 > > { package Test::Unit::MyTestCase; > > use base qw( Test::Unit::TestCase ); > > use Test::Builder '0.7'; > > use Carp qw( confess ); > >=20 > > sub assert_using { > > my ( $self, $test_coderef ) =3D @_; > > my $builder =3D Test::Builder->new; > > $builder->reset; > > $builder->plan( 'no_plan' ); > > my $output =3D ''; > > open my $output_fh, '>', \$output or die; > > $builder->output( $output_fh ); > > $builder->failure_output( $output_fh ); > > $builder->todo_output( $output_fh ); > > $self->assert( $test_coderef->(), $output ); > > } > > } > >=20 > > would allow you to do things like: > >=20 > > { package Comparison::Test; > > use base qw( Test::Unit::MyTestCase ); > >=20 > > use Test::More; # provides is_deeply > >=20 > > sub test_using_is_deeply { > > my $self =3D shift; > > $self->assert_using( sub { > > is_deeply [ { hello =3D>1}, { world =3D> 1 } ], > > [ { hello =3D>1}, { earth =3D> 1 } ]; > > } ); > > } > >=20 > > use Test::Deep; # provides cmp_deeply > >=20 > > sub test_using_cmp_deeply { > > my $self =3D shift; > > $self->assert_using( sub { > > cmp_deeply( [ { hello =3D>1}, { world =3D> 1 } ], > > [ { hello =3D>1}, { earth =3D> 1 } ] > > ); > > } ); > > } > > } > >=20 > > giving you diagnostics like > >=20 > > 2) /private/var/tmp/folders.502/Cleanup At Startup/ > > test-201050484.793.pl:21 - test_using_cmp_deeply(Comparison::Test) > > not ok 1 > > # Failed test at /private/var/tmp/folders.502/Cleanup At Startup/=20 > > test-201050484.793.pl line 43. > > # Comparing hash keys of $data->[1] > > # Missing: 'earth' > > # Extra: 'world' > >=20 > > 3) /private/var/tmp/folders.502/Cleanup At Startup/ > > test-201050484.793.pl:21 - test_using_is_deeply(Comparison::Test) > > not ok 1 > > # Failed test at /private/var/tmp/folders.502/Cleanup At Startup/=20 > > test-201050484.793.pl line 33. > > # Structures begin differing at: > > # $got->[1]{earth} =3D Does not exist > > # $expected->[1]{earth} =3D '1' > >=20 > > However if you're going to be using a bunch of Test::Builder based=20 > > assertions it might be easier to use Test::Group or <bias=20 > > class=3D"author"> Test::Class </bias> instead. > >=20 > > Cheers, > >=20 > > Adrian > >=20 > >=20 >=20 |