JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
unit_test_func requires that type matches
authorJason Woofenden <jason@jasonwoof.com>
Mon, 25 Apr 2011 05:52:46 +0000 (01:52 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Mon, 25 Apr 2011 05:52:46 +0000 (01:52 -0400)
unit_tests.php
unit_tests/encode.php [new file with mode: 0644]
unit_tests/format.php

index d092962..62f7d6b 100644 (file)
@@ -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 (file)
index 0000000..615a898
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+
+#  Copyright (C) 2010 Jason Woofenden
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#  
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#  
+#  You should have received a copy of the GNU General Public License
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+# 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&amp;'\""));
+       _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&amp;\'"', 'symbols&amp;\'"');
+
+       pulldown('mixed', array(array('one', 'Eno'), 'two', array('symbols&amp;\'"', '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&amp;\'"', 'Slobmys');
+
+       pulldown('array_2d', array(array('one', 'Eno'), array('two', 'Owt'), array('symbols&amp;\'"', '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&amp;amp;\'"', 'symbols&amp;amp;\'"'); # 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();
+}
index 0869a7e..9264418 100644 (file)
@@ -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&amp;'\""), 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&amp;\'"', 'symbols&amp;\'"');
-       pulldown('hash', array('one' => 'Eno', 'two' => 'Owt', 'symbols&amp;\'"' => '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&amp;\'"', '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&amp;amp;\'"', '');
-
-       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();
 }