aboutsummaryrefslogtreecommitdiffstats
path: root/lib/testmore.php
blob: aa0616188ca868f45b19ca3668bacde89bc92cda (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php

/*******************************************************************\
*                        PROJECT INFORMATION                        *
*                                                                   *
*  Project:  Apache-Test                                            *
*  URL:      http://perl.apache.org/Apache-Test/                    *
*  Notice:   Copyright (c) 2006 The Apache Software Foundation      *
*                                                                   *
*********************************************************************
*                        LICENSE INFORMATION                        *
*                                                                   *
*  Licensed under the Apache License, Version 2.0 (the "License");  *
*  you may not use this file except in compliance with the          *
*  License. You may obtain a copy of the License at:                *
*                                                                   *
*  http://www.apache.org/licenses/LICENSE-2.0                       *
*                                                                   *
*  Unless required by applicable law or agreed to in writing,       *
*  software distributed under the License is distributed on an "AS  *
*  IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either  *
*  express or implied. See the License for the specific language    *
*  governing permissions and limitations under the License.         *
*                                                                   *
*********************************************************************
*                        MODULE INFORMATION                         *
*                                                                   *
*  This is a PHP implementation of Test::More:                      *
*                                                                   *
*  http://search.cpan.org/dist/Test-Simple/lib/Test/More.pm         *
*                                                                   *
*********************************************************************
*                              CREDITS                              *
*                                                                   *
*  Originally inspired by work from Andy Lester. Written and        *
*  maintained by Chris Shiflett. For contact information, see:      *
*                                                                   *
*  http://shiflett.org/contact                                      *
*                                                                   *
\*******************************************************************/

header('Content-Type: text/plain');
register_shutdown_function('_test_end');

$_no_plan = FALSE;
$_num_failures = 0;
$_num_skips = 0;
$_test_num = 0;

function plan($plan)
{
    /*
    plan('no_plan');
    plan('skip_all');
    plan(array('skip_all' => 'My reason is...'));
    plan(23);
    */

    global $_no_plan;
    global $_skip_all;

    switch ($plan) {
        case 'no_plan':
            $_no_plan = TRUE;
            break;

        case 'skip_all':
            echo "1..0\n";
            break;

        default:
            if (is_array($plan)) {
                echo "1..0 # Skip {$plan['skip_all']}\n";
                exit;
            }

            echo "1..$plan\n";
            break;
    }
}

function ok($pass, $test_name = '')
{
    global $_test_num;
    global $_num_failures;
    global $_num_skips;

    $_test_num++; 

    if ($_num_skips) {
        $_num_skips--;
        return TRUE;
    }

    if (!empty($test_name) && $test_name[0] != '#') {
        $test_name = "- $test_name";
    }

    if ($pass) {
        echo "ok $_test_num $test_name\n";
    } else {
        echo "not ok $_test_num $test_name\n";

        $_num_failures++;
        $caller = debug_backtrace();

        if (strstr($caller['0']['file'], $_SERVER['PHP_SELF'])) {
            $file = $caller['0']['file'];
            $line = $caller['0']['line'];
        } else {
            $file = $caller['1']['file'];
            $line = $caller['1']['line'];
        }

        if (isset($_SERVER['SERVER_ROOT'])){
            $file = str_replace($_SERVER['SERVER_ROOT'], 't', $file);
        }

        diag("    Failed test ($file at line $line)");
    }

    return $pass;
}

function is($this, $that, $test_name = '')
{
    $pass = ($this == $that);

    ok($pass, $test_name);

    if (!$pass) {
        diag("         got: '$this'");
        diag("    expected: '$that'");
    }

    return $pass;
}

function isnt($this, $that, $test_name = '')
{
    $pass = ($this != $that);

    ok($pass, $test_name);

    if (!$pass) {
        diag("    '$this'");
        diag('        !=');
        diag("    '$that'");
    }

    return $pass;
}

function like($string, $pattern, $test_name = '')
{
    $pass = preg_match($pattern, $string);

    ok($pass, $test_name);

    if (!$pass) {
        diag("                  '$string'");
        diag("    doesn't match '$pattern'");
    }

    return $pass;
}

function unlike($string, $pattern, $test_name = '')
{
    $pass = !preg_match($pattern, $string);

    ok($pass, $test_name);

    if (!$pass) {
        diag("                  '$string'");
        diag("          matches '$pattern'");
    }

    return $pass;
}

function cmp_ok($this, $operator, $that, $test_name = '')
{
    eval("\$pass = (\$this $operator \$that);");

    ob_start();
    var_dump($this);
    $_this = trim(ob_get_clean());

    ob_start();
    var_dump($that);
    $_that = trim(ob_get_clean());

    ok($pass, $test_name);

    if (!$pass) {
        diag("         got: $_this");
        diag("    expected: $_that");
    }

    return $pass;
}

function can_ok($object, $methods)
{
    $pass = TRUE;
    $errors = array();

    foreach ($methods as $method) {
        if (!method_exists($object, $method)) {
            $pass = FALSE;
            $errors[] = "    method_exists(\$object, $method) failed";
        }
    }

    if ($pass) {
        ok(TRUE, "method_exists(\$object, ...)");
    } else {
        ok(FALSE, "method_exists(\$object, ...)");
        diag($errors);
    }

    return $pass;
}

function isa_ok($object, $expected_class, $object_name = 'The object')
{
    $got_class = get_class($object);

    if (version_compare(phpversion(), '5', '>=')) {
        $pass = ($got_class == $expected_class);
    } else {
        $pass = ($got_class == strtolower($expected_class));
    }

    if ($pass) {
        ok(TRUE, "$object_name isa $expected_class");
    } else {
        ok(FALSE, "$object_name isn't a '$expected_class' it's a '$got_class'");
    }

    return $pass;
}

function pass($test_name = '')
{
    return ok(TRUE, $test_name);
}

function fail($test_name = '')
{
    return ok(FALSE, $test_name);
}

function diag($message)
{
    if (is_array($message)) {
        foreach($message as $current) {
            echo "# $current\n";
        }
    } else {
        echo "# $message\n";
    }
}

function include_ok($module)
{
    $pass = ((include $module) == 1);
    return ok($pass);
}

function require_ok($module)
{
    $pass = ((require $module) == 1);
    return ok($pass);
} 

function skip($message, $num)
{
    global $_num_skips;

    if ($num < 0) {
        $num = 0;
    }

    for ($i = 0; $i < $num; $i++) {
        pass("# SKIP $message");
    }

    $_num_skips = $num;
}

function is_deeply($got_struct, $expected_struct, $test_name = '')
{
    $got_flat = var_export($got_struct, true);
    $expected_flat = var_export($expected_struct, true);

    is($got_flat, $expected_flat, $test_name);
}


/*

TODO:

function todo()
{
}

function todo_skip()
{
}

function eq_array()
{
}

function eq_hash()
{
}

function eq_set()
{
}

*/

function _test_end()
{
    global $_no_plan;
    global $_num_failures;
    global $_test_num;

    if ($_no_plan) {
        echo "1..$_test_num\n";
    }

    if ($_num_failures) {
        diag("Looks like you failed $_num_failures tests of $_test_num.");
    }
}

?>