C4::Boolean has had its' exception changed to warns, the test was erroring falsely
[koha.git] / t / Boolean.t
1 #!/usr/bin/perl
2 #
3
4 use strict;
5 use C4::Boolean;
6
7 use vars qw( @tests );
8 use vars qw( $loaded );
9
10 sub f ($) {
11    my($x) = @_;
12    my $it;
13    # Returns either the value returned prefixed with 'OK:',
14    # or the caught exception (string expected)
15    local($@);
16    eval {
17       $it = 'OK:' . C4::Boolean::true_p($x);
18    };
19    if ($@) {
20       $it = $@;
21       $it =~ s/ at \S+ line \d+\.\n//s;
22    }
23    return $it;
24 }
25
26 BEGIN {
27    @tests = (
28    [
29       'control',
30       sub { C4::Boolean::INVALID_BOOLEAN_STRING_EXCEPTION },
31       'The given value does not seem to be interpretable as a Boolean value',
32       undef
33
34    # False strings
35    ], [
36       '"0"',     \&f, 'OK:0', '0'
37    ], [
38       '"false"', \&f, 'OK:0', 'false'
39    ], [
40       '"off"',   \&f, 'OK:0', 'off'
41    ], [
42       '"no"',    \&f, 'OK:0', 'no'
43
44    # True strings
45    ], [
46       '"1"',     \&f, 'OK:1', '1'
47    ], [
48       '"true"',  \&f, 'OK:1', 'true'
49    ], [
50       '"on"',    \&f, 'OK:1', 'on'
51    ], [
52       '"yes"',   \&f, 'OK:1', 'yes'
53    ], [
54       '"YES"',   \&f, 'OK:1', 'YES'     # verify case insensitivity
55
56    # Illegal strings
57 #   ], [
58 #      'undef',   \&f, C4::Boolean::INVALID_BOOLEAN_STRING_EXCEPTION, undef
59 #   ], [
60 #      '"foo"',   \&f, C4::Boolean::INVALID_BOOLEAN_STRING_EXCEPTION, 'foo'
61    ],
62 );
63 }
64
65 BEGIN { $| = 1; printf "1..%d\n", scalar(@tests); }
66 END {print "not ok 1\n" unless $loaded;}
67 $loaded = 1;
68
69
70 # Run all tests in sequence
71 for (my $i = 1; $i <= scalar @tests; $i += 1) {
72    my $test = $tests[$i - 1];
73    my($title, $f, $expected, $input) = @$test;
74    die "not ok $i (malformed test case)\n"
75       unless @$test == 4 && ref $f eq 'CODE';
76
77    my $output = &$f($input);
78    if (
79          (!defined $output && !defined $expected)
80       || (defined $output && defined $expected && $output eq $expected)
81    ) {
82       print "ok $i - $title\n";
83    } else {
84       print "not ok $i - $title: got ",
85             (defined $output? "\"$output\"": 'undef'),
86             ', expected ',
87             (defined $expected? "\"$expected\"": 'undef'),
88             "\n";
89    }
90 }
91
92
93
94
95