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

Moin Monks!

I need some help using Test::Deep ...

my problem: I wanna refactor a rather complicated module which produces nested data structures like the following and I wanna test that the output stays stable:

my $h_data1 = { '756' => { 'firstname' => [ '17833329', '17785333', ], 'lastname' => [ '17785333', '17785974', ], 'pks' => [ '17833329', '17785333', '17785974', ], 'time' => '0.00718307495117188', # How long + it took }, '755' => { 'lastname' => [ '17833329' ], 'pks' => [ '17833329' ], 'time' => '0.00639796257019043', } };
to be able to use Test::More::is_deeply() between to runs I need to ignore the time benchmark.

I already solved this quite complicatedly by copying new filtered hashes...

use strict; use warnings; use Test::More; use Data::Dump qw/pp/; sub ignore_fields { my $h_in = shift; my $h_out; my @to_ignore =qw(time); while ( my ($pk, $h_matches) = each %$h_in ) { my $h_filtered = { %$h_matches }; delete @$h_filtered{@to_ignore}; $h_out->{$pk}=$h_filtered; } return $h_out; } pp ignore_fields($h_data1); pp $h_data1; is_deeply(ignore_fields($h_data1), ignore_fields($h_data2), "is_deeply +");

But Test::Deep should be the right module to do this appropriately, unfortunately today my brain is to slow to understand the docs... (it's flue season ;-)

Any help appreciated!

Cheers Rolf

Replies are listed 'Best First'.
Re: Regression test with nested data structures -- Or how to use Test::Deep?
by ELISHEVA (Prior) on Oct 18, 2012 at 09:31 UTC

    I haven't tried this myself, but looking at the docs, another possibility would be to use num($number, $tolerance) to test the time field. If you set the time tolerance wide enough, then you should be able to pass a test on that field without having to pre-filter the hash and remove the timestamp key.

      Thanks, interesting option! :)

      But I fear using a fixed time tolerace is a good possibility to produce tests which fail occasionally...

      Cheers Rolf