Bugzilla – Attachment 129007 Details for
Bug 29746
Add a handy Koha::Result::Boolean class
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 29746: (it-doesn't-hurt follow-up) More tests
Bug-29746-it-doesnt-hurt-follow-up-More-tests.patch (text/plain), 3.86 KB, created by
Tomás Cohen Arazi (tcohen)
on 2022-01-04 19:22:55 UTC
(
hide
)
Description:
Bug 29746: (it-doesn't-hurt follow-up) More tests
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2022-01-04 19:22:55 UTC
Size:
3.86 KB
patch
obsolete
>From ab719c4734bdcd28ebea2cd9a83356e30c2f61bc Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >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 <tomascohen@theke.io> >--- > 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.34.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 29746
:
128830
|
128831
|
128834
|
128835
|
128882
|
128883
|
128997
|
128998
| 129007