JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added more unit tests
[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 function file_run_unit_tests($filename) {
25         require_once($filename);
26         $func = basename($filename, '.php') . '_unit_tests_main';
27
28         if(function_exists($func)) {
29                 return $func();
30         }
31 }
32
33 # Run many unit tests. Pass a directory containing php files with tests in
34 # them, and a space-separated list of their basenames. Each of those files is
35 # expected to define a function <basename>_unit_tests_main which will run all
36 # tests in that file. It should print a message() about each test that failed,
37 # and return the number of tests that failed.
38 function run_unit_tests($directory, $basenames) {
39         $errors = 0;
40         $basenames = explode(' ', $basenames);
41         foreach($basenames as $basename) {
42                 $filename = "$directory/$basename.php";
43                 message("running tests in $filename");
44                 $errors += file_run_unit_tests($filename);
45         }
46         message("tests finished with $errors error" . enc_s($errors));
47 }
48
49 # Call this to unit test wfpl. By default it tests everything, or you can pass
50 # a space-separated list of the basenames of the files in code/wfpl/unit_tests/
51 # that you'd like to run.
52 function unit_test_wfpl($basenames = 'format') {
53         tem_load('code/wfpl/unit_tests/template.html');
54         run_unit_tests('code/wfpl/unit_tests', $basenames);
55         display_messages();
56         tem_output();
57         exit();
58 }