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

I asked about this in the CB but I messed up the question so I'm asking it here again, this time (hopefully) coherently. Plus, since I asked that question I found something that works but I'd like to know if it's correct or I'm doing it all wrong
My overall goal is just learning about Test::Simple and Test::More.

My module:

#!/usr/bin/perl package Mymodule; use strict; use warnings; require Exporter; our @ISA=qw(Exporter); our @EXPORT_OK = qw( values ); sub values { my $val1 = $_[0]; my $val2 = $_[1]; return ($val1, $val2); } 1;
My test:
#!/usr/bin/perl use strict; use warnings; use Mymodule qw( values ); use Test::More qw(no_plan); BEGIN { use_ok('Mymodule', qw( values )) }; my $pig = 3; my $cow = 4; is( values($pig, $cow ), (3 and 4), "checking values" ) ;
The results look good:
ok 1 - use Mymodule; ok 2 - checking values 1..2
The question is whether or not using the  (3 and 4) in the $expected position is the proper way to do this?
Here's how I thought it should work:
is( values($pig, $cow ), (3, 4), "checking values" ) ;
but fails:
ok 1 - use Mymodule; Useless use of a constant in void context at ./mymodule.t line 12. ok 2 - checking values 1..2
Any advice? Maybe stop worrying and be happy it works and move on. :)

Replies are listed 'Best First'.
Re: Checking two scalars in Test::More $expected. It works, but is it proper?
by Bloodnok (Vicar) on Mar 29, 2009 at 00:44 UTC
    You're attempting to compare 2 lists, so try converting the lists to anon. arrays and using the is_deeply() sub as in...
    is_deeply( [ values($pig, $cow) ], [ qw/3 4/ ], "checking values" ) ;

    A user level that continues to overstate my experience :-))
      Thanks Bloodnok. That works.
      One of the responses in the CB directed me to using  is_deeply but I didn't understand. When I look at the examples for Test::Simple and Test::More it all looks so simple but my searching didn't lead me to an example of testing subroutines. I would have never known to use anonymous arrays. Am I going about testing subroutines wrong? Or maybe I should ask if there's a simpler/newbie way to test subroutines?

      UPDATE:
      OK I think I understand now. The part I was missing was that I need to run the code in the test file and then compare the results. Like this I believe:

      #!/usr/bin/perl use strict; use warnings; use Mymodule qw( values ); use Test::More qw(no_plan); BEGIN { use_ok('Mymodule', qw( values )) }; my $pig = 3; my $cow = 4; my ($animals1, $animals2) = values($pig, $cow); is ($animals1, 3, "Checking animals1"); is ($animals2, 4, "Checking animals2");
      Doh!
        Bloodnok's solution has the added benefit of checking if values returned exactly two values.
Re: Checking two scalars in Test::More $expected. It works, but is it proper?
by afoken (Chancellor) on Mar 29, 2009 at 12:13 UTC

    You wrote:

    use Mymodule qw( values ); use Test::More qw(no_plan); BEGIN { use_ok('Mymodule', qw( values )) };

    Guess what happens if Mymodule causes an error, like returning false or dieing. Your test script will die, because it can't load Mymodule. It won't execute the use_ok test. Drop the explicit use Mymodule line and let use_ok do the work.

    BTW: using Test::More without a plan is not a good plan.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      afoken, thank-you for that tip.
      That was another obvious thing that I didn't realize until you pointed it out. I wish there were some good test scripts I could look at that were not so far above my skill level. I've looked at a few from the modules I have installed but they're quite a ways above my skill level. I'm afraid if I spend the time that's needed to understand them I will have lost track of what my original project is. Which is really just learning Perl, but nonetheless I'm trying to stay focused.
Re: Checking two scalars in Test::More $expected. It works, but is it proper?
by chromatic (Archbishop) on Mar 29, 2009 at 22:27 UTC
    The question is whether or not using the (3 and 4) in the $expected position is the proper way to do this?

    It's not, but what do you expect Perl to do with the expression (3 and 4)? What part of what Perl did do with it surprised you?

      The docs for Test::Simple explain that all tests run in scalar context. My subroutine returns two scalars so I was trying to compare the results to (3 and 4) in scalar context as well.

        Yes, I know that. What do you think (3 and 4) means to Perl? Is it an array? A list? A subroutine call? What did you intend for that code to produce?

        I know what it does. I have no idea why you used it. (Part of the problem is that I know what it does, and I have no idea why what it does would be useful to you in this context.)