Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Test-Deep CPAN distribution.

Report information
The Basics
Id: 78254
Status: resolved
Priority: 0/
Queue: Test-Deep

People
Owner: Nobody in particular
Requestors: ether [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Preliminary thoughts re new new feature: comparison function for comparing an arrayref with ordering as if it were a hashref
I'm opening this ticket so I don't forget this thought.. need to discuss with rjbs later, and I don't have the time currently to code this up in a pull request. new comparison function for testing the contents of an arrayref, as if it were a hashref (that is, the ordering of *pairs* of elements is insignificant). This would be useful for testing the 2nd element of a PSGI response (which contains header information, as an arrayref). This works as-is, but can made nicer by folding into its own testing function within Test::Deep: use Test::Deep 'cmp_details'; sub _hash_as_arrayref(@) { my %expected_hash = @_; return ( all( supersetof(), # first check we have an arrayref. code(sub { # now compare contents as a hash my $got = shift; return cmp_details({@$got}, \%expected_hash); }, ), ), ); } my @result = ( 404, [ 'Cache-Control' => 'no-cache', 'Content-Type' => 'text/plain', ], [ 'foo' ], ); cmp_deeply( \@result, [ 404, _hash_as_arrayref( 'Cache-Control' => 'no-cache', 'Content-Type' => 'text/plain', ), [ 'foo' ], ], );
Updated version: handles modifiers like superhashof; also properly renders diagnostics. # we take a hashref so we accept modifiers like superhashof(). sub _hashref_as_arrayref { my ($expected_hash) = @_; return ( all( supersetof(), # first check we have an arrayref. code(sub { my $got = shift; return (0, 'odd number of elements') if @$got % 2; my ($ok, $stack) = cmp_details({@$got}, $expected_hash); return $ok if $ok; return ($ok, deep_diag($stack)); }, ), ), ); }
Subject: Re: [rt.cpan.org #78254] Preliminary thoughts re new new feature: comparison function for comparing an arrayref with ordering as if it were a hashref
Date: Wed, 22 May 2013 12:34:04 +0900
To: bug-Test-Deep [...] rt.cpan.org
From: Fergal Daly <fergald [...] gmail.com>
Out of curiosity, could you have leveraged Test::Deep::Hash to save a lot of code here? Something like: sub descend { my ($self, $got) = @_; my $exp = $self->{val}; %got = @$got; %exp = @$exp; return Test::Deep::Hash->new(\%exp)->got(\%got); } perhaps with some extra code in there to ensure that there really were an even number of elements and also something in render to indicate that things are being compared hashwise, F
On 2013-05-21 20:34:22, fergal@esatclear.ie wrote: Show quoted text
> Out of curiosity, could you have leveraged Test::Deep::Hash to save a lot > of code here?
I actually did that in the initial version: https://metacpan.org/source/ETHER/Test-Deep-UnorderedPairs-0.001/lib/Test/Deep/UnorderedPairs.pm But that doesn't work if there are two pairs with the same key: cmp_deeply( [ foo => 1, foo => 2, bar => 3 ], tuples( foo => 1, foo => 2, foo => 3, ), ); https://metacpan.org/source/ETHER/Test-Deep-UnorderedPairs-0.003/t/02-repeated-keys.t
Subject: Re: [rt.cpan.org #78254] Preliminary thoughts re new new feature: comparison function for comparing an arrayref with ordering as if it were a hashref
Date: Thu, 23 May 2013 07:52:20 +0900
To: bug-Test-Deep [...] rt.cpan.org
From: Fergal Daly <fergald [...] gmail.com>
Your documentation says "for comparing lists as if they were hashes" and samehash(...) makes me think that I'm testing whether these 2 things would make the same hash if I assigned them to a hash. If that's not what you're testing then I think you should make that very clear in the docs e.g. by dropping use of "hash". So if you really want an unordered comparison of tuples then you should transform the lists in to a list that looks like [["foo", 1], ["foo", 2]] and the descend into a the bag comparator. http://cpansearch.perl.org/src/RJBS/Test-Deep-0.110/lib/Test/Deep/Set.pm has pretty much the same comparison loop as you have in your code, I think, F On 23 May 2013 02:13, Karen Etheridge via RT <bug-Test-Deep@rt.cpan.org>wrote: Show quoted text
> Queue: Test-Deep > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=78254 > > > On 2013-05-21 20:34:22, fergal@esatclear.ie wrote:
> > Out of curiosity, could you have leveraged Test::Deep::Hash to save a lot > > of code here?
> > I actually did that in the initial version: > > > https://metacpan.org/source/ETHER/Test-Deep-UnorderedPairs-0.001/lib/Test/Deep/UnorderedPairs.pm > > But that doesn't work if there are two pairs with the same key: > > cmp_deeply( > [ foo => 1, foo => 2, bar => 3 ], > tuples( > foo => 1, > foo => 2, > foo => 3, > ), > ); > > > https://metacpan.org/source/ETHER/Test-Deep-UnorderedPairs-0.003/t/02-repeated-keys.t >
Subject: Re: [rt.cpan.org #78254] Preliminary thoughts re new new feature: comparison function for comparing an arrayref with ordering as if it were a hashref
Date: Wed, 22 May 2013 20:03:55 -0700
To: Fergal Daly via RT <bug-Test-Deep [...] rt.cpan.org>
From: Karen Etheridge <ether [...] cpan.org>
On Wed, May 22, 2013 at 06:52:45PM -0400, Fergal Daly via RT wrote: Show quoted text
> and samehash(...) makes me think that I'm testing whether these 2 things > would make the same hash if I assigned them to a hash. If that's not what > you're testing then I think you should make that very clear in the docs > e.g. by dropping use of "hash".
samehash() is there because that's what rjbs called the hypothetical function in his Router::Dumb tests that was crying out for this. There's a note in the docs that the names are weird and subject to change; it's quite possible that samehash will be removed. Show quoted text
> So if you really want an unordered comparison of tuples then you should > transform the lists in to a list that looks like > > [["foo", 1], ["foo", 2]] > > and the descend into a the bag comparator.
I considered that, but the diagnostic message on error was confusing to the user, so a bit of custom jiggery is needed no matter what.
Subject: Re: [rt.cpan.org #78254] Preliminary thoughts re new new feature: comparison function for comparing an arrayref with ordering as if it were a hashref
Date: Thu, 23 May 2013 13:00:39 +0900
To: bug-Test-Deep [...] rt.cpan.org
From: Fergal Daly <fergald [...] gmail.com>
Yeah diagnostics are going to be a bit odd but I really think you should change the main description. It currently says "Test::Deep::UnorderedPairs - A Test::Deep plugin for comparing lists as if they were hashes" which is not the case, F On 23 May 2013 12:04, Karen Etheridge via RT <bug-Test-Deep@rt.cpan.org>wrote: Show quoted text
> Queue: Test-Deep > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=78254 > > > On Wed, May 22, 2013 at 06:52:45PM -0400, Fergal Daly via RT wrote:
> > and samehash(...) makes me think that I'm testing whether these 2 things > > would make the same hash if I assigned them to a hash. If that's not what > > you're testing then I think you should make that very clear in the docs > > e.g. by dropping use of "hash".
> > samehash() is there because that's what rjbs called the hypothetical > function in his Router::Dumb tests that was crying out for this. > There's a note in the docs that the names are weird and subject to change; > it's quite possible that samehash will be removed. >
> > So if you really want an unordered comparison of tuples then you should > > transform the lists in to a list that looks like > > > > [["foo", 1], ["foo", 2]] > > > > and the descend into a the bag comparator.
> > I considered that, but the diagnostic message on error was confusing to the > user, so a bit of custom jiggery is needed no matter what. > > >