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

Test::More just started displaying some behavior that I've never seen before. My unit tests include this code:

my $main_image = $pdf->main_image_params(); is_deeply($pdf->adjust_to_bounding_box($main_image), undef, "adjust_to_bounding_box returns on no second param" ); # test

Here is what Test::More displays when I run the tests (slightly edited for line length):

# Failed test <path to unit_tests.pl> at line 1131) # got: undef # expected: 'adjust_to_bounding_box returns on no second param'

It's like it doesn't see the 'undef' at all...any ideas on what's up?

UPDATE: If I change the code to this, it works as expected:

my $main_image = $pdf->main_image_params(); my $got = $pdf->adjust_to_bounding_box($main_image); is_deeply($got, undef, "adjust_to_bounding_box returns on no second param" ); # test

Replies are listed 'Best First'.
Re: Test::More::is_deeply apparently ignoring an argument?
by Tanktalus (Canon) on Apr 03, 2005 at 15:12 UTC

    Test::More::is_deeply is not ignoring an argument - that argument is never reaching it. That's because, in list (array) context (i.e., "wantarray" returns true), it is returning an empty list.

    Try:

    is_deeply(scalar $pdf->adjust_to_bounding_box($main_image), undef, "adjust_to_bounding_box returns on no second param" ); # test
    And compare with:
    my @got = $pdf->adjust_to_bounding_box($main); # @got should be an empty list.
    Hope that helps.

      is_deeply(scalar $pdf->adjust_to_bounding_box($main_image), undef, "adjust_to_bounding_box returns on no second param" ); # test
      And that checks the scalar return value. If you want to check the list return value separately:
      is_deeply([$pdf->adjust_to_bounding_box($main_image)], [], "adjust_to_bounding_box returns on no second param (list con +text)" ); # test

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

      Thank you, yes, that makes perfect sense. Ghu, I was tearing my hair out over this one; I knew that it had to be something simple and obvious, I just couldn't see it. Thanks again.

        Thank you, yes, that makes perfect sense. Ghu, I was tearing my hair out over this one; I knew that it had to be something simple and obvious, I just couldn't see it. Thanks again.

        If it makes you feel any better, I've been programming in Perl for about 9 years, and still, to this day, context-related bugs are the ones that trip me the most often, even though I learned about Perl's contexts from day 1 basically (I learned Perl by reading the 1st ed. of the Camel book, which covered contexts in depth). Just recently I read japhy's nice article on lists and arrays, and it made me feel like a newbie. It's ironic that a feature that is meant to make Perl more human-language-like trips me most often. I must have Vulcan blood or something.

        the lowliest monk

Re: Test::More::is_deeply apparently ignoring an argument?
by blokhead (Monsignor) on Apr 03, 2005 at 15:17 UTC
    In your first snippet, you are calling the adjust_to_bounding_box method in list context (argument list of a function). In the second snippet, you are calling it in scalar context (assigning to $got). The adjust_to_bounding_box method probably exits with a bare return statement, which gives different values depending on the context it is called from. You can use scalar to force scalar context on the method call otherwise in list context.

    This is an annoying problem many people encounter with CGI's param method as well.

    blokhead