Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Test::Deep::noclass propagation

by choroba (Archbishop)
on Sep 22, 2020 at 22:57 UTC ( #11122101=perlquestion: print w/replies, xml ) Need Help??

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

Let's pretend we have an object:
my $o = bless { x => bless ['y'], 'My::Value' }, 'My::Class';

I want to verify that the object when cursed recursively will consist of

{ x => [$v], # where $v is 'y' or 'z' a => 'b' } # if a exists at all

I thought Test::Deep's noclass should be able to tell me.

cmp_deeply $o, noclass(subhashof({ a => 'b', x => subbagof('y', 'z') } +));

But it doesn't work:

# Failed test at ... # Comparing $data->{"x"} as a SubBag # got : My::Value=ARRAY(0x561fbd1fe3a8) # expect : An array to use as a Bag

Without an object, it works nicely:

cmp_deeply { x => ['y'] }, subhashof({ a => 'b', x => subbagof('y', 'z') });

I asked about it in #freenode and was pointed towards Test2::Tools::Compare. It was the first time I played with it, so maybe my code is a bit clumsy, but I was able to write a test that passes:

use Test2::Tools::Compare qw{ like hash item field in_set DNE bag }; my $o = bless { x => bless ['y'], 'My::Value' }, 'My::Class'; like({ x => ['y'] }, hash { field a => in_set(DNE(), 'b'); field x => bag { item in_set('y', 'z') }; } ); like $o, hash { field a => in_set(DNE(), 'b'); field x => [ in_set('y', 'z') ]; };

How to do the comparison with Test::Deep? I tried adding another noclass to the value, or even noclass(useclass(...)), but it always failed similarly.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re: Test::Deep::noclass propagation
by choroba (Archbishop) on Sep 22, 2020 at 23:02 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11122101]
Approved by LanX
Front-paged by LanX
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2022-01-18 12:51 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (53 votes). Check out past polls.