Re: [Perlunit-users] Comparing deeply nested data structures.
Status: Beta
Brought to you by:
mca1001
From: Desilets, A. <Ala...@nr...> - 2007-05-16 23:35:07
|
Great, thanks Adrian. That sounds exactly like what we're looking for. Alain =20 > -----Original Message----- > From: Adrian Howard [mailto:ad...@qu...]=20 > Sent: May 16, 2007 7:26 PM > To: per...@li...; Desilets, Alain > Cc: Farley, Benoit > Subject: Re: [Perlunit-users] Comparing deeply nested data 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=20 > expected output,=20 > > and if the two differ, get a good diagnostic message that 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=20 > to use it.=20 > > It's part of a testing framework different from PerlUnit,=20 > and I can't=20 > > seem to be able to use it without running it inside that=20 > other testing=20 > > framework (which I don't have time to learn). >=20 > It's not that hard to shim the more popular Test::Builder=20 > based "assertion" modules into Test::Unit. The=20 > off-the-top-of-my-head greasy 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=20 > based assertions it might be easier to use Test::Group or=20 > <bias class=3D"author"> Test::Class </bias> instead. >=20 > Cheers, >=20 > Adrian >=20 >=20 |