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