summaryrefslogtreecommitdiff
path: root/Test.php
blob: bad931a40c573c7aa0a8540736e64635ebcce3e9 (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
<?php

/*

=head1 NAME

Test.php - L<Test::More> for PHP

=head1 SYNOPSIS

  require 'Test.php';

  plan( $num ); # plan $num tests
  # or
  plan( 'no_plan' ); # We don't know how many
  # or
  plan( 'skip_all' ); # Skip all tests
  # or
  plan( 'skip_all', $reason ); # Skip all tests with a reason

  diag( 'message in test output' ) # Trailing \n not required

  # $test_name is always optional and should be a short description of
  # the test, e.g. "some_function() returns an integer"

  # Various ways to say "ok"
  ok( $got == $expected, $test_name );
  
  # Compare with == and !=
  is( $got, $expected, $test_name );
  isnt( $got, $expected, $test_name );

  # Run a preg match on some data
  like( $got, $regex, $test_name );
  unlike( $got, $regex, $test_name );

  # Compare something with a given comparison operator
  cmp_ok( $got, '==', $expected, $test_name );
  # Compare something with a comparison function (should return bool)
  cmp_ok( $got, $func, $expected, $test_name );

  # Recursively check datastructures for equalness
  is_deeply( $got, $expected, $test_name );

  # Always pass or fail a test under an optional name
  pass( $test_name );
  fail( $test_name );

=head1 DESCRIPTION

F<Test.php> is an implementation of Perl's L<Test::More> and Pugs's B<Test> for
PHP. Like those two modules it produces TAP output (see L<TAP>) which
can then be gathered, formatted and summarized by a program that
understands TAP such as L<prove(1)>.

=cut

*/

register_shutdown_function('test_ends');

$Test = array(
	  'run'       => 0,
	  'failed'    => 0,
	  'badpass'   => 0,
	  'planned'   => null
);

function plan( $plan, $why = '' )
{
	global $Test;

	$Test['planned'] = true;

	switch ( $plan )
	{
	case 'no_plan':
		$Test['planned'] = false;
		break;
	case 'skip_all';
		printf( "1..0%s\n", $why ? " # Skip $why" : '' );
		exit;
	default:
		printf( "1..%d\n", $plan );
		break;
	}
}

function pass( $desc = '' )
{
	return proclaim(true, $desc);
}

function fail( $desc = '' )
{
	return proclaim( false, $desc );
}

function ok( $cond, $desc = '' ) {
	return proclaim( $cond, $desc );
}

function is( $got, $expected, $desc = '' ) {
	$pass = $got == $expected;
	return proclaim( $pass, $desc, /* todo */ false, $got, $expected );
}

function isnt( $got, $expected, $desc = '' ) {
	$pass = $got != $expected;
	return proclaim( $pass, $desc, /* todo */ false, $got, $expected, /* negated */ true );
}

function like( $got, $expected, $desc = '' ) {
	$pass = preg_match( $expected, $got );
	return proclaim( $pass, $desc,  /* todo */ false, $got, $expected );
}

function unlike( $got, $expected, $desc = '' ) {
	$pass = ! preg_match( $expected, $got );
	return proclaim( $pass, $desc,  /* todo */ false, $got, $expected, /* negated */ true );
}

function cmp_ok($got, $op, $expected, $desc = '')
{
	$pass = null;

	/* See http://www.php.net/manual/en/language.operators.comparison.php */
	switch ($op)
	{
	case '==':
		$pass = $got == $expected;
		break;
	case '===':
		$pass = $got === $expected;
		break;
	case '!=':
	case '<>':
		$pass = $got != $expected;
		break;
	case '!==':
		$pass = $got !== $expected;
		break;
	case '<':
		$pass = $got < $expected;
		break;
	case '>':
		$pass = $got > $expected;
		break;
	case '<=':
		$pass = $got <= $expected;
		break;
	case '>=':
		$pass = $got >= $expected;
		break;
	default:
		if ( function_exists( $op ) ) {
			$pass = $op( $got, $expected );
		} else {
			die("No such operator or function $op\n");
		}
	}

	return proclaim( $pass, $desc, /* todo */ false, $got, "$op $expected" );
}

function diag($message)
{
    if (is_array($message))
	{
	    $message = implode("\n", $message);
	}

    $messages = explode("\n", $message);

    foreach ($messages as $msg)
	{
        echo "# $msg\n";
    }
}

function include_ok( $file, $desc = '' )
{
    $pass = include $file;
    return proclaim( $pass, $desc == '' ? "include $file" : $desc );
}

function require_ok( $file, $desc = '' )
{
    $pass = require $file;
    return proclaim( $pass, $desc == '' ? "require $file" : $desc );
} 

function is_deeply( $got, $expected, $desc = '' )
{
    // hack
    $s_got = serialize( $got );
	$s_exp = serialize( $expected );

	$pass = $s_got == $s_exp;

	proclaim( $pass, $desc, /* todo */ false, $got, $expected );
}

function isa_ok( $obj, $expected, $desc = '' ) {
	$name = get_class( $obj );
	$pass = $name == $expected;
	proclaim( $pass, $desc, /* todo */ false, $name, $expected );
} 

function proclaim(
	$cond, // bool
	$desc = '',
	$todo = false,
	$got = null,
	$expected = null,
	$negate = false ) {

	global $Test;

	$Test['run'] += 1;

	# TODO: force_todo

	# Everything after the first # is special, so escape user-supplied messages
	$desc = str_replace( '#', '\\#', $desc );
	$desc = str_replace( "\n", '\\n', $desc );

	$ok = $cond ? "ok" : "not ok";
	$directive = $todo === false ? '' : '# TODO aoeu';

	printf( "%s %d %s%s\n", $ok, $Test['run'], $desc, $directive );

	if ( ! $cond ) {
		report_failure( $desc, $got, $expected, $negate, $todo );
	}

	return $cond;
}

function report_failure( $desc, $got, $expected, $negate, $todo ) {
	# Every public function in this file proclaim which then calls
    #  this function, so our culprit is the third item in the stack
	$caller = debug_backtrace();
	$call = $caller['2'];

	diag(
		sprintf( " Failed%stest '%s'\n in %s at line %d\n       got: %s\n  expected: %s",
			$todo ? ' TODO ' : ' ',
			$desc,
			$call['file'],
			$call['line'],
			$got,
			$expected
		)
	);
}

function test_ends ()
{
	global $Test;

	if ( $Test['planned'] === false ) {
		printf( "1..%d\n", $Test['run'] );
	}
}

/*

=head1 TODO

=over

=item * Fully document this file

=item *

Add TODO support, maybe via C<ok(0, "foo # TODO fix this")>
C<ok(1, "foo", array( 'todo' => 'fix this'))>.

=back

=head1 SEE ALSO

=over

=item L<TAP> - The TAP protocol

=item L<Test::More> 

=item Pugs's Test.pm

=back

=head1 AUTHOR

Ævar Arnfjörð Bjarmason <avarab@gmail.com>

=head1 LICENSING

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

*/

?>