Subject: | undef for expected causes crash |
The following test causes Test::Deep to crash and results in unfinished
test script.
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 2;
use Test::Deep;
sub test { return [ shift ] || undef; }
my $expected = [ 'test' ];
cmp_bag(test('test'), $expected, "Test array for data");
$expected = undef;
cmp_bag(test(), $expected, "Test array for no data (undef)");
When run, the following happens:
1..2
ok 1 - Test array for data
Can't use an undefined value as an ARRAY reference at
/usr/local/share/perl/5.8.8/Test/Deep.pm line 459.
# Looks like you planned 2 tests but only ran 1.
# Looks like your test died just after 1.
I think the correct behavior should be to check if the expected field is
undefined and return ok if actual is also undefined. For example, for
this particular test, the output should be:
1..2
ok 1 - Test array for data
ok 2 - Test array for no data (undef)
At the very least, the undefined expected value should be caught and the
crash prevented, something like the output below, which shows the test
script completing and noting the failure due to the undefined expected
value.
1..2
ok 1 - Test array for datar
# expect : undef
# Looks like you failed 1 test of 2.
I should point out that changing the test script to use
cmp_deeply(test(), bag(undef), "text");
instead of cmp_bag... works.
In quick testing, changing Test::Deep::cmp_bag (~ line 457) to:
sub cmp_bag
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
my $got = shift;
my $exp = shift;
$exp = bag((defined $exp ? @{$exp} : undef));
return cmp_deeply($got, $exp, shift);
}
allows cmp_bag to function as expected. The problem was an uncaught
undef value being used in @{} to get an array, as with bag(@{shift()}).
I also recognize that this type of test should not use cmp_bag to begin
with, though I think this still should be caught and handled more
gracefully.