Fix a warning in log
[koha.git] / t / Boolean.t
blobf0d2cc1c871cade76b45624537a52d3ab1cee9dd
1 #!/usr/bin/perl
4 use strict;
5 use C4::Boolean;
7 use vars qw( @tests );
8 use vars qw( $loaded );
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);
19 if ($@) {
20 $it = $@;
21 $it =~ s/ at \S+ line \d+\.\n//s;
23 return $it;
26 BEGIN {
27 @tests = (
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
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'
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
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'
65 BEGIN { $| = 1; printf "1..%d\n", scalar(@tests); }
66 END {print "not ok 1\n" unless $loaded;}
67 $loaded = 1;
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';
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";