-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWTestSet.class.php
82 lines (69 loc) · 1.52 KB
/
WTestSet.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
define('_T',' ');
$_t = _T;
class WTestSet {
var $log;
function WTestSet() {
$this->clearLog();
}
function clearLog() {
$this->log = array();
}
function run($indent=_T) {
$testMethods = array();
foreach(get_class_methods(get_class($this)) as $method) {
$matches = array();
if(preg_match('/^test_?(.*)$/',$method,$matches)) {
$testMethods[$matches[1]] = $method;
}
}
$testMethodCount = count($testMethods);
echo $testMethodCount," test methods\n";
$passed = 0; $failed = 0; $i = 0;
foreach($testMethods as $testName => $methodName) {
$i++;
echo $indent,"#[$i/$testMethodCount] $testName: ";
$this->clearLog();
try {
if($this->$methodName()) {
$passed++;
echo "pass\n";
} else {
$failed++;
echo "fail (",$this->log2str(', '),")\n";
}
} catch(Exception $e) {
$failed++;
echo "fail (",$e->getMessage(),")\n";
}
}
echo $indent,"pass: $passed fail: $failed total: $testMethodCount\n";
return ($passed/$testMethodCount);
}
function assert($condition,$failmsg) {
if(!$condition) {
$this->log($failmsg);
return false;
} else
return $this;
}
function assertSame($namea,$a,$nameb,$b) {
if($a != $b) {
$this->log(
"\n\t$namea: ",var_export($a,true),
"\n\tis not",
"\n\t$nameb: ",var_export($b,true)
);
return false;
} else
return $this;
}
function log() {
$args = func_get_args();
$this->log[] = implode(null,$args);
}
function log2str($sep="\n") {
return implode($sep,$this->log);
}
}
?>