$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() {
$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));
}
}
# 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();
--- /dev/null
+<?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&'\""));
+ _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&amp;\'"', 'symbols&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();
+}
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&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();
}