X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=t%2FBoolean.t;h=2c5434bd3f8d86a33ad64bac24a71f9f9682b0d8;hb=32fc535b8a71483b401b964b273cb280758ea8f4;hp=eba01eb0bff2914c58bda1ecd7a5771d3dc35c60;hpb=a1fe32d2f81d6275d5a653bd19dae82c5a1ac231;p=koha.git diff --git a/t/Boolean.t b/t/Boolean.t index eba01eb0bf..2c5434bd3f 100755 --- a/t/Boolean.t +++ b/t/Boolean.t @@ -1,95 +1,39 @@ -#!/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"; - } -} - - - +use Modern::Perl; + +use Test::More tests => 22; +use Test::Warn; + +BEGIN { use_ok( 'C4::Boolean', qw( true_p ) ); } + +is( true_p('0'), '0', 'recognizes \'0\' as false' ); +is( true_p('nil'), '0', 'recognizes \'nil\' as false' ); +is( true_p('false'), '0', 'recognizes \'false\' as false' ); +is( true_p('off'), '0', 'recognizes \'off\' as false' ); +is( true_p('no'), '0', 'recognizes \'no\' as false' ); +is( true_p('n'), '0', 'recognizes \'n\' as false' ); +is( true_p('NO'), '0', 'verified case insensitivity' ); + +is( true_p('1'), '1', 'recognizes \'1\' as true' ); +is( true_p('-1'), '1', 'recognizes \'-1\' as true' ); +is( true_p('t'), '1', 'recognizes \'t\' as true' ); +is( true_p('true'), '1', 'recognizes \'true\' as true' ); +is( true_p('on'), '1', 'recognizes \'on\' as true' ); +is( true_p('yes'), '1', 'recognizes \'yes\' as true' ); +is( true_p('y'), '1', 'recognizes \'y\' as true' ); +is( true_p('YES'), '1', 'verified case insensitivity' ); + +my $result; +warning_like { $result = true_p(undef) } + qr/^The given value does not seem to be interpretable as a Boolean value/, + 'Invalid boolean (undef) raises warning'; +is( $result, undef, 'recognizes undefined as not boolean' ); +warning_like { $result = true_p('foo') } + qr/^The given value does not seem to be interpretable as a Boolean value/, + 'Invalid boolean (\'foo\') raises warning'; +is( $result, undef, 'recognizes \'foo\' as not boolean' ); +warning_like { $result = true_p([]) } + qr/^The given value does not seem to be interpretable as a Boolean value/, + 'Invalid boolean (reference) raises warning'; +is( $result, undef, 'recognizes a reference as not a boolean' );