3 # Copyright (C) 2009 Jason Woofenden
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # This file is for running unit tests. Either on wfpl or your own code.
21 require_once('code/wfpl/template.php');
22 require_once('code/wfpl/messages.php');
24 # call this to declare that a unit test has passed
25 function unit_test_passed($msg) {
26 if(isset($GLOBALS['unit_tests_verbose']) && $GLOBALS['unit_tests_verbose'] > 1) {
27 message("PASSED: $msg");
29 $GLOBALS['unit_tests_passed'] += 1;
32 # call this to declare that a unit test has failed
33 function unit_test_failed($msg) {
34 message("FAILED: $msg");
35 $GLOBALS['unit_tests_failed'] += 1;
38 # use this function to unit-test a function
39 # real prototype: ($name, $args..., $correct)
40 function unit_test_func() {
41 $args = func_get_args();
42 $function = array_shift($args);
43 $correct = array_pop($args);
44 $result = call_user_func_array($function, $args);
45 $message = "$function(" . join(', ', $args) . ") returned $result";
46 if($result == $correct) {
47 unit_test_passed($message);
49 unit_test_failed($message . " instead of $correct");
54 function file_run_unit_tests($filename) {
55 require_once($filename);
56 $func = basename($filename, '.php') . '_unit_tests_main';
58 if(function_exists($func)) {
63 # Run many unit tests. Pass a directory containing php files with tests in
64 # them, and a space-separated list of their basenames. Each of those files is
65 # expected to define a function <basename>_unit_tests_main which will run all
66 # tests in that file. It should print a message() about each test that failed,
67 # and return the number of tests that failed.
68 function run_unit_tests($directory, $basenames) {
69 $GLOBALS['unit_tests_passed'] = 0;
70 $GLOBALS['unit_tests_failed'] = 0;
71 $basenames = explode(' ', $basenames);
72 foreach($basenames as $basename) {
73 $filename = "$directory/$basename.php";
74 message("running tests in $filename");
75 file_run_unit_tests($filename);
77 $passed = $GLOBALS['unit_tests_passed'];
78 $failed = $GLOBALS['unit_tests_failed'];
79 message("tests finished with $passed test" . enc_s($passed) . " passed and $GLOBALS[unit_tests_failed] test" . enc_s($failed) . " failed");
82 # Call this to unit test wfpl. By default it tests everything, or you can pass
83 # a space-separated list of the basenames of the files in code/wfpl/unit_tests/
84 # that you'd like to run.
85 function unit_test_wfpl($basenames = 'format db misc') {
86 tem_load('code/wfpl/unit_tests/template.html');
87 run_unit_tests('code/wfpl/unit_tests', $basenames);