Hi Dallaylaen,
Well, it may be because I am grumpy as always at this time of year, but to me it seems that you are trying to make things too shiny.
I prefer to use Lego bricks rather than modelling clay. As soon as you get your testing function just right, Murphy's Law says you will encounter data that doesn't fit. I would test your condition like this:
use strict; use warnings;
use Test::More;
my @data = (
{ id => 1, start => 2, end => 3 },
{ id => 2, start => 3, end => 4 },
{ id => 3, start => 4, end => 5 }, # fail
{ id => 42, start => 6, end => 7 },
{ id => 666, start => 7, end => 8 },
{ id => 999, start => 8, end => 9 },
);
for ( 0 .. $#data - 1 ) {
is( $data[ $_ + 1 ]->{'start'}, $data[ $_ ]->{'end'}, "$data[ $_ ]
+->{'id'} sequence" );
}
done_testing;
__END__
Output:
1206160.pl .. 1/?
# Failed test '3 sequence'
# at 1206160.pl line 18.
# got: '6'
# expected: '5'
# Looks like you failed 1 test of 5.
1206160.pl .. Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/5 subtests
Test Summary Report
-------------------
1206160.pl (Wstat: 256 Tests: 5 Failed: 1)
Failed test: 3
Non-zero exit status: 1
Files=1, Tests=5, 0 wallclock secs ( 0.03 usr 0.00 sys + 0.04 cusr
+ 0.01 csys = 0.08 CPU)
Result: FAIL
Or maybe like this .... Edit: as ikegami revealed the below is buggy. (emits a warning if number of list elements is not even):
se strict; use warnings;
use Test::More;
use List::Util 'pairfirst';
my @data = (
{ id => 1, start => 2, end => 3 },
{ id => 2, start => 3, end => 4 },
{ id => 3, start => 4, end => 5 }, # fail
{ id => 42, start => 6, end => 7 },
{ id => 666, start => 7, end => 8 },
{ id => 999, start => 8, end => 9 },
);
my @failed = pairfirst { $a->{'end'} ne $b->{'start'} } @data;
is( @failed, undef, 'sequencing' )
or diag sprintf 'ID %s and ID %s are not sequential', map { $_->{'id
+'} } @failed;
done_testing;
__END__
Output:
prove 1206160.pl
1206160.pl .. 1/?
# Failed test 'sequencing'
# at 1206160.pl line 16.
# got: '2'
# expected: undef
# ID 3 and ID 42 are not sequential
# Looks like you failed 1 test of 1.
1206160.pl .. Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests
Test Summary Report
-------------------
1206160.pl (Wstat: 256 Tests: 1 Failed: 1)
Failed test: 1
Non-zero exit status: 1
Files=1, Tests=1, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.06 cusr
+ 0.00 csys = 0.08 CPU)
Result: FAIL
Hope this helps, and Happy Merry!
The way forward always starts with a minimal test.