From: Jason Woofenden Date: Mon, 25 Apr 2011 05:52:46 +0000 (-0400) Subject: unit_test_func requires that type matches X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=commitdiff_plain;h=f920d084acbf8c53c996a6fee26530a41e7e0559 unit_test_func requires that type matches --- diff --git a/unit_tests.php b/unit_tests.php index d092962..62f7d6b 100644 --- a/unit_tests.php +++ b/unit_tests.php @@ -35,6 +35,19 @@ function unit_test_failed($msg) { $GLOBALS['unit_tests_failed'] += 1; } +function backslashinate($str) { + return '"' . addcslashes($str, "\0..\37\"\\\177..\377") . '"'; +} + +function backslashinate_each($a) { + foreach($a as &$v) { + $v = backslashinate($v); + } + + return $a; +} + + # use this function to unit-test a function # real prototype: ($name, $args..., $correct) function unit_test_func() { @@ -42,11 +55,11 @@ function unit_test_func() { $function = array_shift($args); $correct = array_pop($args); $result = call_user_func_array($function, $args); - $message = "$function(" . join(', ', $args) . ") returned $result"; - if($result == $correct) { + $message = "$function(" . join(', ', backslashinate_each($args)) . ") returned " . backslashinate($result); + if($result === $correct) { unit_test_passed($message); } else { - unit_test_failed($message . " instead of $correct"); + unit_test_failed($message . " instead of " . backslashinate($correct)); } } @@ -82,7 +95,7 @@ function run_unit_tests($directory, $basenames) { # Call this to unit test wfpl. By default it tests everything, or you can pass # a space-separated list of the basenames of the files in code/wfpl/unit_tests/ # that you'd like to run. -function unit_test_wfpl($basenames = 'format db misc') { +function unit_test_wfpl($basenames = 'format db misc encode') { tem_load('code/wfpl/unit_tests/template.html'); run_unit_tests('code/wfpl/unit_tests', $basenames); display_messages(); diff --git a/unit_tests/encode.php b/unit_tests/encode.php new file mode 100644 index 0000000..615a898 --- /dev/null +++ b/unit_tests/encode.php @@ -0,0 +1,65 @@ +. + + +# This file contains tests for functions in code/wfpl/format.php +# +# See code/wfpl/test.php for details on how to run or create tests + + +require_once('code/wfpl/encode.php'); + +# returns number of errors +function _test_encode_pulled($field, $value, $correct) { + unit_test_func('enc_pulled', $value, $field, $correct); +} + +function test_encode_pulled() { + pulldown('array', array('one', 'two', "symbols&'\"")); + _test_encode_pulled('array', 'one', 'one'); + _test_encode_pulled('array', 'One', 'One'); # not found is unchanged + _test_encode_pulled('array', 'two', 'two'); + _test_encode_pulled('array', 'symbols&\'"', 'symbols&\'"'); + + pulldown('mixed', array(array('one', 'Eno'), 'two', array('symbols&\'"', 'Slobmys'))); + _test_encode_pulled('mixed', 'one', 'Eno'); + _test_encode_pulled('mixed', 'One', 'One'); # not found is unchanged + _test_encode_pulled('mixed', 'two', 'two'); + _test_encode_pulled('mixed', 'symbols&\'"', 'symbols&\'"'); # not found is unchanged + _test_encode_pulled('mixed', 'symbols&\'"', 'Slobmys'); + + pulldown('array_2d', array(array('one', 'Eno'), array('two', 'Owt'), array('symbols&\'"', 'Slobmys'))); + _test_encode_pulled('array_2d', 'one', 'Eno'); + _test_encode_pulled('array_2d', 'One', 'One'); # not found in unchanged + _test_encode_pulled('array_2d', 'two', 'Owt'); + _test_encode_pulled('array_2d', 'symbols&\'"', 'symbols&\'"'); # not found in unchanged +} + +function test_encode_misc() { + unit_test_func('enc_tab', '', ''); + unit_test_func('enc_tab', "\n", "\t\n\t"); + unit_test_func('enc_tab', "a", "\ta"); + unit_test_func('enc_tab', "a\n", "\ta\n\t"); + unit_test_func('enc_tab', "\t", "\t\t"); + unit_test_func('enc_tab', "\n\na", "\t\n\t\n\ta"); + unit_test_func('enc_tab', "\t\n\t\n\ta", "\t\t\n\t\t\n\t\ta"); +} + +function encode_unit_tests_main() { + test_encode_pulled(); + test_encode_misc(); +} diff --git a/unit_tests/format.php b/unit_tests/format.php index 0869a7e..9264418 100644 --- a/unit_tests/format.php +++ b/unit_tests/format.php @@ -183,47 +183,9 @@ function test_format_misc() { format_12hr_to_hours($str) format_phone($str) */ - - return $errors; -} - -# returns number of errors -function _test_format_options($field, $value, $correct) { - $ret = format_options($value, $field); - if($ret !== $correct) { - message("format_options($value, $field) returned \"$ret\" instead of \"$correct\""); - return 1; - } - return 0; } - -function test_format_options() { - $errors = 0; - pulldown('array', array('one', 'two', "symbols&'\""), PULLDOWN_ARRAY); - $errors +=_test_format_options('array', 'one', 'one'); - $errors +=_test_format_options('array', 'One', ''); - $errors +=_test_format_options('array', 'two', 'two'); - $errors +=_test_format_options('array', 'symbols&\'"', 'symbols&\'"'); - pulldown('hash', array('one' => 'Eno', 'two' => 'Owt', 'symbols&\'"' => 'Slobmys'), PULLDOWN_HASH); - $errors +=_test_format_options('hash', 'one', 'one'); - $errors +=_test_format_options('hash', 'One', ''); - $errors +=_test_format_options('hash', 'two', 'two'); - $errors +=_test_format_options('hash', 'symbols&\'"', ''); - pulldown('array_2d', array(array('one', 'Eno'), array('two', 'Owt'), array('symbols&\'"', 'Slobmys')), PULLDOWN_2D); - $errors +=_test_format_options('array_2d', 'one', 'one'); - $errors +=_test_format_options('array_2d', 'One', ''); - $errors +=_test_format_options('array_2d', 'two', 'two'); - $errors +=_test_format_options('array_2d', 'symbols&\'"', ''); - - return $errors; -} - -# returns number of errors function format_unit_tests_main() { - $errors = 0; - $errors += test_format_money(); - $errors += test_format_misc(); - $errors += test_format_options(); - return $errors; + test_format_money(); + test_format_misc(); }