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 <tomascohen@theke.io>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Tomas Cohen Arazi 2022-01-04 15:58:54 -03:00 committed by Fridolin Somers
parent a8ab17f823
commit 367a90ca18
2 changed files with 46 additions and 12 deletions

View File

@ -20,7 +20,9 @@ package Koha::Result::Boolean;
use Modern::Perl;
use overload bool => \&as_bool;
use overload
bool => \&as_bool,
'==' => \&equals;
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

View File

@ -23,17 +23,35 @@ use_ok('Koha::Result::Boolean');
subtest 'new() tests' => sub {
plan tests => 4;
plan tests => 2;
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 '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 {