@@ -, +, @@ $ kshell k$ prove t/Koha/Result/Boolean.t --- Koha/Result/Boolean.pm | 22 +++++++++++++++++++--- t/Koha/Result/Boolean.t | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 14 deletions(-) --- a/Koha/Result/Boolean.pm +++ a/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 --- a/t/Koha/Result/Boolean.t +++ a/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 { --