Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Test-Deep CPAN distribution.

Report information
The Basics
Id: 32397
Status: resolved
Priority: 0/
Queue: Test-Deep

People
Owner: Nobody in particular
Requestors: zeta [...] tcscs.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.099
Fixed in: (no value)



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.
From: FDALY [...] cpan.org
Thanks for the report. The docs are quite explicit about the arguments to cmp_bag --- $ok = cmp_bag(\@got, \@bag, $name) Is shorthand for cmp_deeply(\@got, bag(@bag), $name) --- cmp_bag expects 2 array refs. Passing undef is an error and if you do that, you should expect an unhappy result - just the same as doing $x = undef; print @$x; [] is a reference to an empty array, undef is not. I could check that all arguments to all functions are of the correct form but in this case that would also result in the script dieing prematurely, just with a more useful error message. If you'd like to send me a patch for that (with tests) I will review it. In the meantime I'm closing the bug, F On Thu Jan 17 16:11:23 2008, ZETA wrote: Show quoted text
> 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. >