JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
unit_test_func requires that type matches
[wfpl.git] / unit_tests.php
1 <?php
2
3 #  Copyright (C) 2009 Jason Woofenden
4 #
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.
9 #  
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.
14 #  
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/>.
17
18
19 # This file is for running unit tests. Either on wfpl or your own code.
20
21 require_once('code/wfpl/template.php');
22 require_once('code/wfpl/messages.php');
23
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");
28         }
29         $GLOBALS['unit_tests_passed'] += 1;
30 }
31
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;
36 }
37
38 function backslashinate($str) {
39         return '"' . addcslashes($str, "\0..\37\"\\\177..\377") . '"';
40 }
41
42 function backslashinate_each($a) {
43         foreach($a as &$v) {
44                 $v = backslashinate($v);
45         }
46
47         return $a;
48 }
49         
50
51 # use this function to unit-test a function
52 # real prototype: ($name, $args..., $correct)
53 function unit_test_func() {
54         $args = func_get_args();
55         $function = array_shift($args);
56         $correct = array_pop($args);
57         $result = call_user_func_array($function, $args);
58         $message = "$function(" . join(', ', backslashinate_each($args)) . ") returned " . backslashinate($result);
59         if($result === $correct) {
60                 unit_test_passed($message);
61         } else {
62                 unit_test_failed($message . " instead of " . backslashinate($correct));
63         }
64 }
65
66
67 function file_run_unit_tests($filename) {
68         require_once($filename);
69         $func = basename($filename, '.php') . '_unit_tests_main';
70
71         if(function_exists($func)) {
72                 return $func();
73         }
74 }
75
76 # Run many unit tests. Pass a directory containing php files with tests in
77 # them, and a space-separated list of their basenames. Each of those files is
78 # expected to define a function <basename>_unit_tests_main which will run all
79 # tests in that file. It should print a message() about each test that failed,
80 # and return the number of tests that failed.
81 function run_unit_tests($directory, $basenames) {
82         $GLOBALS['unit_tests_passed'] = 0;
83         $GLOBALS['unit_tests_failed'] = 0;
84         $basenames = explode(' ', $basenames);
85         foreach($basenames as $basename) {
86                 $filename = "$directory/$basename.php";
87                 message("running tests in $filename");
88                 file_run_unit_tests($filename);
89         }
90         $passed = $GLOBALS['unit_tests_passed'];
91         $failed = $GLOBALS['unit_tests_failed'];
92         message("tests finished with $passed test" . enc_s($passed) . " passed and $GLOBALS[unit_tests_failed] test" . enc_s($failed) . " failed");
93 }
94
95 # Call this to unit test wfpl. By default it tests everything, or you can pass
96 # a space-separated list of the basenames of the files in code/wfpl/unit_tests/
97 # that you'd like to run.
98 function unit_test_wfpl($basenames = 'format db misc encode') {
99         tem_load('code/wfpl/unit_tests/template.html');
100         run_unit_tests('code/wfpl/unit_tests', $basenames);
101         display_messages();
102         tem_output();
103         exit();
104 }