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

I'm seeing ... "interesting" behavior with some new development. My (so far) last test:
# -*- perl -*- # t/001_load.t - check module loading and create testing directory use lib qw# /plugins #; use UtcleDB; use Data::Dumper; use Test::More qw( no_plan ); my $dbh = UtcleDB->connect(); BEGIN { use_ok( 'ConferenceNameBuilder' ); } my $object = ConferenceNameBuilder->new( DBH => $dbh ); isa_ok ($object, 'ConferenceNameBuilder'); can_ok ($object, 'foundation_list'); my $list = $object->foundation_list(); eq_array( $list );
the
$object->foundation_list returns, and this may be important, an arrayr +ef containing the list elements. that's been verified (with Data::Du +mper ... it's an array of hashrefs intended to be passed into some op +tions menus.) <p> but ... the 'eq_array' test is failing .. sort of: <code> [me --> ]$ make test PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_h +arness(0, 'blib/lib', 'blib/arch')" t/*.t t/001_load.........ok + t/002_functions....ok + t/003_names........ok + t/004_list.........ok + t/005_listall......ok 1/0Use of uninitialized value in string eq at /u +sr/lib/perl5/5.8.7/Test/More.pm line 1126. Use of uninitialized value in string at /usr/lib/perl5/5.8.7/Test/More +.pm line 1132. t/005_listall......ok + All tests successful. Files=5, Tests=17, 0 wallclock secs ( 0.28 cusr + 0.06 csys = 0.34 +CPU)
it's not a critical warning, but it's a touch annoying. it's also not from passing the arrayref ...

i'd rather not have an 'misleading' test result. any ideas?

UPDATE: what i wanted was really three tests:

ok( ref( $list ) eq 'ARRAY', 'returned a list' ); ok( scalar( @$list ), 'returned a populated list' ); ok( ref( $list->[0] ) eq 'HASH', 'first element is a hashref ...' );
thanks!

Replies are listed 'Best First'.
Re: Test::More and is_array
by tlm (Prior) on Jun 09, 2005 at 20:24 UTC
      ah ... that makes some sense, but i would expect the test to fail if i only passed in one array ...

      i'll have to build another test then. i just want to make sure i'm returning a list.

        As stvn already pointed out, eq_array is not a test like ok, but a convenience function to use in such a test, e.g.

        ok( eq_array( $list, $expected ), 'some test' );

        the lowliest monk

Re: Test::More and is_array
by stvn (Monsignor) on Jun 09, 2005 at 20:25 UTC

    IIRC, eq_array is not actually a test function, but instead returns a boolean which you pass manually to ok().

    That said, the nested structure tests in Test::More are so-so at best (although they seem to be getting a little better). I would suggest using Test::Deep instead.

    -stvn
      Test::Deep just to verify that the list is, in fact, a list? I'll have to look into it.
        Test::Deep just to verify that the list is, in fact, a list? I'll have to look into it.

        You seem to want to do more than just that. You updated the OP to say that you wanted this:

        ok( ref( $list ) eq 'ARRAY', 'returned a list' ); ok( scalar( @$list ), 'returned a populated list' ); ok( ref( $list->[0] ) eq 'HASH', 'first element is a hashref ...' );
        But that IMO is only minimally testing your structure. With Test::Deep you could do this:
        cmp_deeply( $list, all( isa('ARRAY'), array_each( all( isa('HASH'), { option => re(...), menu => ignore() } ) ) ) );
        This code would test that $list is an ARRAY reference, then that all the elements in it are HASH references, then it will check the structure of the hash (this part I made up as an example), to confirm that it has a key 'option' whose value matches a given reg-exp, and that it has a 'menu' key, but you can safely ignore the value in the test.

        With Test::Deep you can test the entire structure while only go as deeply as needed in the specifics of that structure. It's really powerful stuff IMO :)

        -stvn