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 (Cardinal) on Sep 22, 2020 at 23:02 UTC |
Back to
Seekers of Perl Wisdom