Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 90 lines (79 sloc) 2.006 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#!/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";
   }
}
Something went wrong with that request. Please try again.