View | Details | Raw Unified | Return to bug 29746
Collapse All | Expand All

(-)a/Koha/Result/Boolean.pm (-3 / +19 lines)
Lines 20-26 package Koha::Result::Boolean; Link Here
20
20
21
use Modern::Perl;
21
use Modern::Perl;
22
22
23
use overload bool => \&as_bool;
23
use overload
24
   bool => \&as_bool,
25
   '==' => \=
24
26
25
use Koha::Object::Message;
27
use Koha::Object::Message;
26
28
Lines 115-128 sub add_message { Link Here
115
117
116
=head3 as_bool
118
=head3 as_bool
117
119
118
Internal method that exposes the boolean value of the object
120
Internal method that exposes the boolean value of the object as a scalar.
119
121
120
=cut
122
=cut
121
123
122
sub as_bool {
124
sub as_bool {
123
    my ($self) = @_;
125
    my ($self) = @_;
124
126
125
    return $self->{value};
127
    return $self->{value} + 0;
128
}
129
130
=head3 equals
131
132
Internal method implementing equality comparison in scalar context.
133
134
=cut
135
136
sub equals {
137
    my ( $first, $second, $flipped ) = @_;
138
139
    return ($flipped)
140
      ? $first == $second->as_bool
141
      : $first->as_bool == $second;
126
}
142
}
127
143
128
=head1 AUTHORS
144
=head1 AUTHORS
(-)a/t/Koha/Result/Boolean.t (-12 / +29 lines)
Lines 23-39 use_ok('Koha::Result::Boolean'); Link Here
23
23
24
subtest 'new() tests' => sub {
24
subtest 'new() tests' => sub {
25
25
26
    plan tests => 4;
26
    plan tests => 2;
27
27
28
    ok( Koha::Result::Boolean->new,
28
    subtest 'bool context' => sub {
29
        'Defaults to true if initialized without the parameter' );
29
30
    ok( Koha::Result::Boolean->new('Martin'),
30
        plan tests => 4;
31
        'Evals to true in boolean context if set an expression that evals to true' );
31
32
    ok( !Koha::Result::Boolean->new(0),
32
        ok( Koha::Result::Boolean->new,
33
        'Evals to false in boolean context if set a false expression' );
33
            'Defaults to true if initialized without the parameter' );
34
    ok( !Koha::Result::Boolean->new(""),
34
        ok( Koha::Result::Boolean->new('Martin'),
35
        'Evals to false in boolean context if set a false expression' );
35
            'Evals to true in boolean context if set an expression that evals to true' );
36
36
        ok( !Koha::Result::Boolean->new(0),
37
            'Evals to false in boolean context if set a false expression' );
38
        ok( !Koha::Result::Boolean->new(""),
39
            'Evals to false in boolean context if set a false expression' );
40
    };
41
42
    subtest '== context' => sub {
43
44
        plan tests => 4;
45
46
        cmp_ok( Koha::Result::Boolean->new, '==', 1,
47
            'Defaults 1 if initialized without the parameter' );
48
        cmp_ok( Koha::Result::Boolean->new('Martin'), '==', 1,
49
            'Evals 1 if set an expression that evals to true' );
50
        cmp_ok( Koha::Result::Boolean->new(0), '==', 0,
51
            'Evals 0 if set a false expression' );
52
        cmp_ok( Koha::Result::Boolean->new(""), '==', 0,
53
            'Evals 0 if set a false expression' );
54
    };
37
};
55
};
38
56
39
subtest 'set_value() tests' => sub {
57
subtest 'set_value() tests' => sub {
40
- 

Return to bug 29746