[Koha-devel] Perl advice for checking hash content

Jonathan Druart jonathan.druart at bugs.koha-community.org
Mon Aug 30 11:07:56 CEST 2021


Keep it simple, why not the following?

my $canbe = CanItemBeReserved( $patron->borrowernumber, $itemnumber );
is_deeply(
     [$canbe->{status}, $canbe->{limit}]
     ['tooManyReservesToday', 0],
     'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
);

Le sam. 28 août 2021 à 09:38, Victor Grousset/tuxayo
<victor at tuxayo.net> a écrit :
>
> Hi :)
>
> For a development, I'm adding additional keys to
> C4::Reserves::CanItemBeReserved returned hash.
>
> But there are a bunch of tests that match the whole return value and
> thus don't expect the new keys in the hash.
>
> Here is the best way so far I've found to adapt it.
>
>
> is_deeply(
>      CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
>      { status => 'tooManyReservesToday', limit => 0 },
>      'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
> );
>
> ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
>
> my @status_and_limit = @{CanItemBeReserved( $patron->borrowernumber,
> $itemnumber_2 )}{"status", "limit"};
> is_deeply(
>      \@status_and_limit,
>      ['tooManyReservesToday', 0],
>      'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
> );
>
>
> How could this be better?
> Since it took me more than two hours only to have something that works
> and without warnings. There are surely other ways to do it™, and
> hopefully a nicer one.
> Most of the time was reading and learning by trial and error about
> referencing and dereferencing and the specifics or Perl hashes and
> array. It's odd to have been working 3.5 years on Koha closer to the
> code than to the librarian stuff and only have to really start learning
> Perl now ^^
> Away, this is why advice is needed to have something that fit the good
> practices :)
>
> Key/Value Hash Slices would allow to not change the expected param of
> is_deeply() but that doesn't really look different for the readability.
> And isn't worth having Koha bump minimal Perl version from 5.14 (2011)
> to 5.20 (2014). Ok it's just for running the tests and not production
> but still. -> very minor or inexistent readability improvement.
> https://perldoc.perl.org/perldata#Key/Value-Hash-Slices
>
> Having two assertions (is() calls) for status and limit would allow a
> simpler syntax but add duplication.
>
>
> Cheers,
>
> --
> Victor Grousset/tuxayo
> _______________________________________________
> Koha-devel mailing list
> Koha-devel at lists.koha-community.org
> https://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-devel
> website : https://www.koha-community.org/
> git : https://git.koha-community.org/
> bugs : https://bugs.koha-community.org/


More information about the Koha-devel mailing list