From 367a90ca189b0c3f49a7b3a87ce023070a7f05ea Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 4 Jan 2022 15:58:54 -0300 Subject: [PATCH] Bug 29746: (it-doesn't-hurt follow-up) More tests In an attempt to add (even) more tests for this library, we wanted to add tests for the return values initialization and then hit a wall when trying to add tests: Test::More tries to compare in string context first [1], and when you force integer context (by using cmp_ok + '==') it tells '==' is not overridden for the class. So this patch adds those tests, and also the overloaded '==' operator that is required for such tests. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/Koha/Result/Boolean.t => SUCCESS: Tests pass 3. Sign off :-D [1] https://metacpan.org/pod/Test::More#Overloaded-objects Signed-off-by: Tomas Cohen Arazi Signed-off-by: Fridolin Somers --- Koha/Result/Boolean.pm | 22 +++++++++++++++++++--- t/Koha/Result/Boolean.t | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/Koha/Result/Boolean.pm b/Koha/Result/Boolean.pm index f670861766..fab76ab288 100644 --- a/Koha/Result/Boolean.pm +++ b/Koha/Result/Boolean.pm @@ -20,7 +20,9 @@ package Koha::Result::Boolean; use Modern::Perl; -use overload bool => \&as_bool; +use overload + bool => \&as_bool, + '==' => \= use Koha::Object::Message; @@ -115,14 +117,28 @@ sub add_message { =head3 as_bool -Internal method that exposes the boolean value of the object +Internal method that exposes the boolean value of the object as a scalar. =cut sub as_bool { my ($self) = @_; - return $self->{value}; + return $self->{value} + 0; +} + +=head3 equals + +Internal method implementing equality comparison in scalar context. + +=cut + +sub equals { + my ( $first, $second, $flipped ) = @_; + + return ($flipped) + ? $first == $second->as_bool + : $first->as_bool == $second; } =head1 AUTHORS diff --git a/t/Koha/Result/Boolean.t b/t/Koha/Result/Boolean.t index 49223f4aeb..8a1a9c9429 100755 --- a/t/Koha/Result/Boolean.t +++ b/t/Koha/Result/Boolean.t @@ -23,17 +23,35 @@ use_ok('Koha::Result::Boolean'); subtest 'new() tests' => sub { - plan tests => 4; - - ok( Koha::Result::Boolean->new, - 'Defaults to true if initialized without the parameter' ); - ok( Koha::Result::Boolean->new('Martin'), - 'Evals to true in boolean context if set an expression that evals to true' ); - ok( !Koha::Result::Boolean->new(0), - 'Evals to false in boolean context if set a false expression' ); - ok( !Koha::Result::Boolean->new(""), - 'Evals to false in boolean context if set a false expression' ); - + plan tests => 2; + + subtest 'bool context' => sub { + + plan tests => 4; + + ok( Koha::Result::Boolean->new, + 'Defaults to true if initialized without the parameter' ); + ok( Koha::Result::Boolean->new('Martin'), + 'Evals to true in boolean context if set an expression that evals to true' ); + ok( !Koha::Result::Boolean->new(0), + 'Evals to false in boolean context if set a false expression' ); + ok( !Koha::Result::Boolean->new(""), + 'Evals to false in boolean context if set a false expression' ); + }; + + subtest '== context' => sub { + + plan tests => 4; + + cmp_ok( Koha::Result::Boolean->new, '==', 1, + 'Defaults 1 if initialized without the parameter' ); + cmp_ok( Koha::Result::Boolean->new('Martin'), '==', 1, + 'Evals 1 if set an expression that evals to true' ); + cmp_ok( Koha::Result::Boolean->new(0), '==', 0, + 'Evals 0 if set a false expression' ); + cmp_ok( Koha::Result::Boolean->new(""), '==', 0, + 'Evals 0 if set a false expression' ); + }; }; subtest 'set_value() tests' => sub { -- 2.39.5