JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added more unit tests
authorJason Woofenden <jason@jasonwoof.com>
Sat, 27 Mar 2010 06:44:01 +0000 (02:44 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Sat, 27 Mar 2010 06:46:34 +0000 (02:46 -0400)
format.php
unit_tests.php
unit_tests/format.php
unit_tests/template.html

index 0058a44..85a90a2 100644 (file)
@@ -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) {
index 89b54dd..c63e4cd 100644 (file)
@@ -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
index 705440b..261ed65 100644 (file)
@@ -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;
 }
index 42e1063..55b242f 100644 (file)
 <body>
   <h1>wfpl unit tests</h1>
   
-  <!--~message_container start~-->
-    <!--~message_box start~-->
-      <div class="row_~row.evenodd~">~message_text.htmlbrnbsp~</div>
-    <!--~end~-->
-  <!--~end~-->
+  <!--~wfpl_messages {~-->
+      <div class="row_~row evenodd~">~data htmlbrnbsp~</div>
+  <!--~}~-->
 </body>
 </html>