Koha/t/Boolean.t
Galen Charlton 5290ec5596 test suite cleanup
Many of the tests were failing or putting warnings
because a valid systempreferences table is
usaully absent by the time 'make test' is run.

Fortunately, only a few modules try to invoke
C4::Context->preference during module initialization,
so added to the test suite override_context_prefs.pm,
which replaces preference() with a sub to return
testing values for three variables: 'dateformat',
'marcflavour', and 'LibraryName'.

Also fixed bug in t/Boolean.t

With this patch and the patch to move the DB-dependent
tests off to the side for the moment, 'make test'
now runs cleanly, at least on Debian.

Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
2008-01-08 14:08:02 -06:00

95 lines
2 KiB
Perl
Executable file

#!/usr/bin/perl
#
use strict;
use C4::Boolean;
use vars qw( @tests );
use vars qw( $loaded );
sub f ($) {
my($x) = @_;
my $it;
# Returns either the value returned prefixed with 'OK:',
# or the caught exception (string expected)
local($@);
eval {
$it = 'OK:' . C4::Boolean::true_p($x);
};
if ($@) {
$it = $@;
$it =~ s/ at \S+ line \d+\.\n//s;
}
return $it;
}
BEGIN {
@tests = (
[
'control',
sub { C4::Boolean::INVALID_BOOLEAN_STRING_EXCEPTION },
'The given value does not seem to be interpretable as a Boolean value',
undef
# False strings
], [
'"0"', \&f, 'OK:0', '0'
], [
'"false"', \&f, 'OK:0', 'false'
], [
'"off"', \&f, 'OK:0', 'off'
], [
'"no"', \&f, 'OK:0', 'no'
# True strings
], [
'"1"', \&f, 'OK:1', '1'
], [
'"true"', \&f, 'OK:1', 'true'
], [
'"on"', \&f, 'OK:1', 'on'
], [
'"yes"', \&f, 'OK:1', 'yes'
], [
'"YES"', \&f, 'OK:1', 'YES' # verify case insensitivity
# Illegal strings
], [
'undef', \&f, C4::Boolean::INVALID_BOOLEAN_STRING_EXCEPTION, undef
], [
'"foo"', \&f, C4::Boolean::INVALID_BOOLEAN_STRING_EXCEPTION, 'foo'
],
);
}
BEGIN { $| = 1; printf "1..%d\n", scalar(@tests); }
END {print "not ok 1\n" unless $loaded;}
$loaded = 1;
# Run all tests in sequence
for (my $i = 1; $i <= scalar @tests; $i += 1) {
my $test = $tests[$i - 1];
my($title, $f, $expected, $input) = @$test;
die "not ok $i (malformed test case)\n"
unless @$test == 4 && ref $f eq 'CODE';
my $output = &$f($input);
if (
(!defined $output && !defined $expected)
|| (defined $output && defined $expected && $output eq $expected)
) {
print "ok $i - $title\n";
} else {
print "not ok $i - $title: got ",
(defined $output? "\"$output\"": 'undef'),
', expected ',
(defined $expected? "\"$expected\"": 'undef'),
"\n";
}
}