简体   繁体   中英

Perl Unit Testing Data Structures

I'm looking for something like is_deeply or Test::Deep's cmp_deeply, but that just checks the keys/types of a data structure, not the values. For example, I care that a key is an array ref of scalars, but not what the values are.

Anyone have any ideas? I'm sure I am not the first one who has had to make sense of varying data structures. I want to test to make sure the "signature" of the data structure is intact, but I care less about the data in it or matching stuff with regex etc.

You can write your own test functions fairly easily, using the functions provided in Test::Builder and Test::More .

I have written the test assuming you mean an arrayref of non-references , since the only thing you can store in an array is a scalar. You may want to make adjustments.

use Test::Builder;
use Test::More 0.81_01;

sub is_arrayref_of_nonrefs
{
    my $value = shift;

    local $Test::Builder::Level = $Test::Builder::Level + 1;

    return Test::More::ok(0, 'value is an arrayref')
        if not ref $value or ref $value ne 'ARRAY';

    # fail if any references are found in the arrayref
    Test::More::ok((grep { ref } @$value), 'value is an arrayref of non-references');
}

普通测试::更多 isa_ok方法适用于参考:

isa_ok( $array_ref, 'ARRAY' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM