From d0e10623d1c7660c216a1bc5a1050b4507ff560f Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Sat, 27 Mar 2010 02:44:01 -0400 Subject: [PATCH] added more unit tests --- format.php | 2 + unit_tests.php | 2 +- unit_tests/format.php | 142 ++++++++++++++++++++++++++++++++++++++++++++++ unit_tests/template.html | 8 +-- 4 files changed, 148 insertions(+), 6 deletions(-) diff --git a/format.php b/format.php index 0058a44..85a90a2 100644 --- a/format.php +++ b/format.php @@ -45,6 +45,7 @@ function format_int($str) { return ereg_replace('^0*([0-9])', '\1', $str); } +# format the digits after the decimal point function format_decimals($str) { $str = ereg_replace('[^0-9]', '', $str); if(strlen($str)) { @@ -99,6 +100,7 @@ function format_int_0($str) { return $str; } +# USA zip codes function format_zip($str) { $str = ereg_replace('[^0-9]', '', $str); if(strlen($str) > 5) { diff --git a/unit_tests.php b/unit_tests.php index 89b54dd..c63e4cd 100644 --- a/unit_tests.php +++ b/unit_tests.php @@ -43,7 +43,7 @@ function run_unit_tests($directory, $basenames) { message("running tests in $filename"); $errors += file_run_unit_tests($filename); } - message("tests finished with $errors errors"); + message("tests finished with $errors error" . enc_s($errors)); } # Call this to unit test wfpl. By default it tests everything, or you can pass diff --git a/unit_tests/format.php b/unit_tests/format.php index 705440b..261ed65 100644 --- a/unit_tests/format.php +++ b/unit_tests/format.php @@ -66,8 +66,150 @@ function test_format_money() { return $errors; } +# returns number of errors +function _test_format_misc($name, $arg, $correct) { + $function = "format_$name"; + $ret = $function($arg); + if($ret !== $correct) { + message("$function($arg) returned \"$ret\" instead of \"$correct\""); + return 1; + } + return 0; +} + +# run tests where int, int_0, decimal, positive_decimal should return the same result +function _test_format_int($arg, $correct) { + if($correct === '') { + $correct_0 = '0'; + } else { + $correct_0 = $correct; + } + $errons = 0; + $errors += _test_format_misc('int', $arg, $correct); + $errors += _test_format_misc('int_0', $arg, $correct_0); + $errors += _test_format_misc('positive_decimal', $arg, $correct); + $errors += _test_format_misc('decimal', $arg, $correct); + return $errors; +} + +# returns number of errors +function test_format_misc() { + $errors = 0; + + $errors += _test_format_misc('caption', 'something', 'Something'); + $errors += _test_format_misc('caption', 'two_words', 'Two Words'); + $errors += _test_format_misc('caption', 'email_special_case', 'E-mail Special Case'); + $errors += _test_format_misc('caption', 'BIG', 'BIG'); + $errors += _test_format_misc('caption', '1,2.3%^&', '1,2.3%^&'); + + # test int, int_0, positive_decimal, decimal + $errors += _test_format_int('', ''); + $errors += _test_format_int('abcdef', ''); + $errors += _test_format_int('0', '0'); + $errors += _test_format_int('00', '0'); + $errors += _test_format_int('0000', '0'); + $errors += _test_format_int('1', '1'); + $errors += _test_format_int('100000000000000000000000000', '100000000000000000000000000'); + $errors += _test_format_int('10000 0000000000000000000000', '100000000000000000000000000'); + $errors += _test_format_int(' 1a2b3c%^&', '123'); + + $errors += _test_format_misc('int', '-1', '1'); + $errors += _test_format_misc('int', '+1', '1'); + $errors += _test_format_misc('int_0', '-1', '1'); + $errors += _test_format_misc('int_0', '+1', '1'); + $errors += _test_format_misc('positive_decimal', '-1', '1'); + $errors += _test_format_misc('positive_decimal', '+1', '1'); + $errors += _test_format_misc('decimal', '-1', '-1'); + $errors += _test_format_misc('decimal', '+1', '1'); + + $errors += _test_format_misc('int', '-1.0', '10'); # FIXME? + $errors += _test_format_misc('int', '+1.0', '10'); # FIXME? + $errors += _test_format_misc('int_0', '-1.0', '10'); # FIXME? + $errors += _test_format_misc('int_0', '+1.0', '10'); # FIXME? + $errors += _test_format_misc('positive_decimal', '-1.0', '1.0'); + $errors += _test_format_misc('positive_decimal', '+1.0', '1.0'); + $errors += _test_format_misc('decimal', '-1.0', '-1.0'); + $errors += _test_format_misc('decimal', '+1.0', '1.0'); + + $errors += _test_format_misc('positive_decimal', '1.0', '1.0'); + $errors += _test_format_misc('decimal', '1.0', '1.0'); + + # extra zeros after the decimal point + $errors += _test_format_misc('positive_decimal', '-1.00', '1.0'); + $errors += _test_format_misc('positive_decimal', '+1.00', '1.0'); + $errors += _test_format_misc('decimal', '-1.00', '-1.0'); + $errors += _test_format_misc('decimal', '+1.00', '1.0'); + $errors += _test_format_misc('positive_decimal', '-1.000', '1.0'); + $errors += _test_format_misc('positive_decimal', '+1.000', '1.0'); + $errors += _test_format_misc('decimal', '-1.000', '-1.0'); + $errors += _test_format_misc('decimal', '+1.000', '1.0'); + # important zeros after the decimal point + $errors += _test_format_misc('positive_decimal', '-1.020', '1.02'); + $errors += _test_format_misc('positive_decimal', '+1.020', '1.02'); + $errors += _test_format_misc('decimal', '-1.020', '-1.02'); + $errors += _test_format_misc('decimal', '+1.020', '1.02'); + $errors += _test_format_misc('positive_decimal', '-1.0020', '1.002'); + $errors += _test_format_misc('positive_decimal', '+1.0020', '1.002'); + $errors += _test_format_misc('decimal', '-1.0020', '-1.002'); + $errors += _test_format_misc('decimal', '+1.0020', '1.002'); + # signs in weird places + $errors += _test_format_misc('positive_decimal', '-1.-0020', '1.002'); + $errors += _test_format_misc('positive_decimal', '+1.-0020', '1.002'); + $errors += _test_format_misc('decimal', '-1.-0020', '-1.002'); + $errors += _test_format_misc('decimal', '+1.-0020', '1.002'); + $errors += _test_format_misc('positive_decimal', '-1.0-020', '1.002'); + $errors += _test_format_misc('positive_decimal', '+1.0-020', '1.002'); + $errors += _test_format_misc('decimal', '-1.0-020', '-1.002'); + $errors += _test_format_misc('decimal', '+1.0-020', '1.002'); + $errors += _test_format_misc('positive_decimal', '-1-.0020', '1.002'); + $errors += _test_format_misc('positive_decimal', '+1-.0020', '1.002'); + $errors += _test_format_misc('decimal', '-1-.0020', '-1.002'); + $errors += _test_format_misc('decimal', '+1-.0020', '1.002'); + $errors += _test_format_misc('positive_decimal', '--1.0020', '1.002'); + $errors += _test_format_misc('positive_decimal', '+-1.0020', '1.002'); + $errors += _test_format_misc('decimal', '--1.0020', '-1.002'); + $errors += _test_format_misc('decimal', '+-1.0020', '-1.002'); + + + $errors += _test_format_misc('zip', '', ''); + $errors += _test_format_misc('zip', '12345', '12345'); + $errors += _test_format_misc('zip', 'snt3nvwm4vm2zvw0%^&3-', '34203'); + $errors += _test_format_misc('zip', '123456', '12345-6'); + $errors += _test_format_misc('zip', '1234567890', '12345-67890'); + + + /* FIXME test these + format_options($str, $name) + format_filename($str, $allow_uppercase = false) + format_path($str, $allow_uppercase = false) + client_path_to_filename($path) + format_image_w_h($str) + format_image_w_h_thumb_w_h($str) + format_varname($str) + format_oneline($str) + format_unix($str) + format_bool($str) + format_yesno($str) + format_email($str) + format_url($str) + strplusone($str) + format_money($str, $display_cents = true) + format_dollars($str) + format_mdy_to_ymd($str) + format_ymd($str) + format_hours_minutes($str) + format_hours($str) + format_12hr_to_hours($str) + format_phone($str) + */ + + return $errors; +} + +# returns number of errors function format_unit_tests_main() { $errors = 0; $errors += test_format_money(); + $errors += test_format_misc(); return $errors; } diff --git a/unit_tests/template.html b/unit_tests/template.html index 42e1063..55b242f 100644 --- a/unit_tests/template.html +++ b/unit_tests/template.html @@ -24,10 +24,8 @@

wfpl unit tests

- - -
~message_text.htmlbrnbsp~
- - + +
~data htmlbrnbsp~
+ -- 1.7.10.4