0337d704 |
1 | <?php |
2 | /** |
3 | * base include file for SimpleTest |
4 | * @package SimpleTest |
5 | * @subpackage UnitTester |
6 | * @version $Id: expectation.php,v 1.31 2004/08/10 00:05:26 lastcraft Exp $ |
7 | */ |
8 | |
9 | /**#@+ |
10 | * include other SimpleTest class files |
11 | */ |
12 | require_once(dirname(__FILE__) . '/dumper.php'); |
13 | require_once(dirname(__FILE__) . '/options.php'); |
14 | /**#@-*/ |
15 | |
16 | /** |
17 | * Assertion that can display failure information. |
18 | * Also includes various helper methods. |
19 | * @package SimpleTest |
20 | * @subpackage UnitTester |
21 | * @abstract |
22 | */ |
23 | class SimpleExpectation { |
24 | var $_dumper; |
25 | var $_message; |
26 | |
27 | /** |
28 | * Creates a dumper for displaying values and sets |
29 | * the test message. |
30 | * @param string $message Customised message on failure. |
31 | */ |
32 | function SimpleExpectation($message = '%s') { |
33 | $this->_dumper = &new SimpleDumper(); |
34 | $this->_message = $message; |
35 | } |
36 | |
37 | /** |
38 | * Tests the expectation. True if correct. |
39 | * @param mixed $compare Comparison value. |
40 | * @return boolean True if correct. |
41 | * @access public |
42 | * @abstract |
43 | */ |
44 | function test($compare) { |
45 | } |
46 | |
47 | /** |
48 | * Returns a human readable test message. |
49 | * @param mixed $compare Comparison value. |
50 | * @return string Description of success |
51 | * or failure. |
52 | * @access public |
53 | * @abstract |
54 | */ |
55 | function testMessage($compare) { |
56 | } |
57 | |
58 | /** |
59 | * Overlays the generated message onto the stored user |
60 | * message. |
61 | * @param mixed $compare Comparison value. |
62 | * @return string Description of success |
63 | * or failure. |
64 | * @access public |
65 | */ |
66 | function overlayMessage($compare) { |
67 | return sprintf($this->_message, $this->testMessage($compare)); |
68 | } |
69 | |
70 | /** |
71 | * Accessor for the dumper. |
72 | * @return SimpleDumper Current value dumper. |
73 | * @access protected |
74 | */ |
75 | function &_getDumper() { |
76 | return $this->_dumper; |
77 | } |
78 | } |
79 | |
80 | /** |
81 | * Test for equality. |
82 | * @package SimpleTest |
83 | * @subpackage UnitTester |
84 | */ |
85 | class EqualExpectation extends SimpleExpectation { |
86 | var $_value; |
87 | |
88 | /** |
89 | * Sets the value to compare against. |
90 | * @param mixed $value Test value to match. |
91 | * @param string $message Customised message on failure. |
92 | * @access public |
93 | */ |
94 | function EqualExpectation($value, $message = '%s') { |
95 | $this->SimpleExpectation($message); |
96 | $this->_value = $value; |
97 | } |
98 | |
99 | /** |
100 | * Tests the expectation. True if it matches the |
101 | * held value. |
102 | * @param mixed $compare Comparison value. |
103 | * @return boolean True if correct. |
104 | * @access public |
105 | */ |
106 | function test($compare) { |
107 | return (($this->_value == $compare) && ($compare == $this->_value)); |
108 | } |
109 | |
110 | /** |
111 | * Returns a human readable test message. |
112 | * @param mixed $compare Comparison value. |
113 | * @return string Description of success |
114 | * or failure. |
115 | * @access public |
116 | */ |
117 | function testMessage($compare) { |
118 | if ($this->test($compare)) { |
119 | return "Equal expectation [" . $this->_dumper->describeValue($this->_value) . "]"; |
120 | } else { |
121 | return "Equal expectation fails " . |
122 | $this->_dumper->describeDifference($this->_value, $compare); |
123 | } |
124 | } |
125 | |
126 | /** |
127 | * Accessor for comparison value. |
128 | * @return mixed Held value to compare with. |
129 | * @access protected |
130 | */ |
131 | function _getValue() { |
132 | return $this->_value; |
133 | } |
134 | } |
135 | |
136 | /** |
137 | * Test for inequality. |
138 | * @package SimpleTest |
139 | * @subpackage UnitTester |
140 | */ |
141 | class NotEqualExpectation extends EqualExpectation { |
142 | |
143 | /** |
144 | * Sets the value to compare against. |
145 | * @param mixed $value Test value to match. |
146 | * @param string $message Customised message on failure. |
147 | * @access public |
148 | */ |
149 | function NotEqualExpectation($value, $message = '%s') { |
150 | $this->EqualExpectation($value, $message); |
151 | } |
152 | |
153 | /** |
154 | * Tests the expectation. True if it differs from the |
155 | * held value. |
156 | * @param mixed $compare Comparison value. |
157 | * @return boolean True if correct. |
158 | * @access public |
159 | */ |
160 | function test($compare) { |
161 | return ! parent::test($compare); |
162 | } |
163 | |
164 | /** |
165 | * Returns a human readable test message. |
166 | * @param mixed $compare Comparison value. |
167 | * @return string Description of success |
168 | * or failure. |
169 | * @access public |
170 | */ |
171 | function testMessage($compare) { |
172 | $dumper = &$this->_getDumper(); |
173 | if ($this->test($compare)) { |
174 | return "Not equal expectation passes " . |
175 | $dumper->describeDifference($this->_getValue(), $compare); |
176 | } else { |
177 | return "Not equal expectation fails [" . |
178 | $dumper->describeValue($this->_getValue()) . |
179 | "] matches"; |
180 | } |
181 | } |
182 | } |
183 | |
184 | /** |
185 | * Test for identity. |
186 | * @package SimpleTest |
187 | * @subpackage UnitTester |
188 | */ |
189 | class IdenticalExpectation extends EqualExpectation { |
190 | |
191 | /** |
192 | * Sets the value to compare against. |
193 | * @param mixed $value Test value to match. |
194 | * @param string $message Customised message on failure. |
195 | * @access public |
196 | */ |
197 | function IdenticalExpectation($value, $message = '%s') { |
198 | $this->EqualExpectation($value, $message); |
199 | } |
200 | |
201 | /** |
202 | * Tests the expectation. True if it exactly |
203 | * matches the held value. |
204 | * @param mixed $compare Comparison value. |
205 | * @return boolean True if correct. |
206 | * @access public |
207 | */ |
208 | function test($compare) { |
209 | return SimpleTestCompatibility::isIdentical($this->_getValue(), $compare); |
210 | } |
211 | |
212 | /** |
213 | * Returns a human readable test message. |
214 | * @param mixed $compare Comparison value. |
215 | * @return string Description of success |
216 | * or failure. |
217 | * @access public |
218 | */ |
219 | function testMessage($compare) { |
220 | $dumper = &$this->_getDumper(); |
221 | if ($this->test($compare)) { |
222 | return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . "]"; |
223 | } else { |
224 | return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . |
225 | "] fails with [" . |
226 | $this->_dumper->describeValue($compare) . "] " . |
227 | $this->_dumper->describeDifference( |
228 | $this->_getValue(), |
229 | $compare, |
230 | TYPE_MATTERS); |
231 | } |
232 | } |
233 | } |
234 | |
235 | /** |
236 | * Test for non-identity. |
237 | * @package SimpleTest |
238 | * @subpackage UnitTester |
239 | */ |
240 | class NotIdenticalExpectation extends IdenticalExpectation { |
241 | |
242 | /** |
243 | * Sets the value to compare against. |
244 | * @param mixed $value Test value to match. |
245 | * @param string $message Customised message on failure. |
246 | * @access public |
247 | */ |
248 | function NotIdenticalExpectation($value, $message = '%s') { |
249 | $this->IdenticalExpectation($value, $message); |
250 | } |
251 | |
252 | /** |
253 | * Tests the expectation. True if it differs from the |
254 | * held value. |
255 | * @param mixed $compare Comparison value. |
256 | * @return boolean True if correct. |
257 | * @access public |
258 | */ |
259 | function test($compare) { |
260 | return ! parent::test($compare); |
261 | } |
262 | |
263 | /** |
264 | * Returns a human readable test message. |
265 | * @param mixed $compare Comparison value. |
266 | * @return string Description of success |
267 | * or failure. |
268 | * @access public |
269 | */ |
270 | function testMessage($compare) { |
271 | $dumper = &$this->_getDumper(); |
272 | if ($this->test($compare)) { |
273 | return "Not identical expectation passes " . |
274 | $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS); |
275 | } else { |
276 | return "Not identical expectation [" . $dumper->describeValue($this->_getValue()) . "] matches"; |
277 | } |
278 | } |
279 | } |
280 | |
281 | /** |
282 | * Test for a pattern using Perl regex rules. |
283 | * @package SimpleTest |
284 | * @subpackage UnitTester |
285 | */ |
286 | class WantedPatternExpectation extends SimpleExpectation { |
287 | var $_pattern; |
288 | |
289 | /** |
290 | * Sets the value to compare against. |
291 | * @param string $pattern Pattern to search for. |
292 | * @param string $message Customised message on failure. |
293 | * @access public |
294 | */ |
295 | function WantedPatternExpectation($pattern, $message = '%s') { |
296 | $this->SimpleExpectation($message); |
297 | $this->_pattern = $pattern; |
298 | } |
299 | |
300 | /** |
301 | * Accessor for the pattern. |
302 | * @return string Perl regex as string. |
303 | * @access protected |
304 | */ |
305 | function _getPattern() { |
306 | return $this->_pattern; |
307 | } |
308 | |
309 | /** |
310 | * Tests the expectation. True if the Perl regex |
311 | * matches the comparison value. |
312 | * @param string $compare Comparison value. |
313 | * @return boolean True if correct. |
314 | * @access public |
315 | */ |
316 | function test($compare) { |
317 | return (boolean)preg_match($this->_getPattern(), $compare); |
318 | } |
319 | |
320 | /** |
321 | * Returns a human readable test message. |
322 | * @param mixed $compare Comparison value. |
323 | * @return string Description of success |
324 | * or failure. |
325 | * @access public |
326 | */ |
327 | function testMessage($compare) { |
328 | if ($this->test($compare)) { |
329 | return $this->_decribePatternMatch($this->_getPattern(), $compare); |
330 | } else { |
331 | $dumper = &$this->_getDumper(); |
332 | return "Pattern [" . $this->_getPattern() . |
333 | "] not detected in [" . |
334 | $dumper->describeValue($compare) . "]"; |
335 | } |
336 | } |
337 | |
338 | /** |
339 | * Describes a pattern match including the string |
340 | * found and it's position. |
341 | * @param string $pattern Regex to match against. |
342 | * @param string $subject Subject to search. |
343 | * @access protected |
344 | */ |
345 | function _decribePatternMatch($pattern, $subject) { |
346 | preg_match($pattern, $subject, $matches); |
347 | $position = strpos($subject, $matches[0]); |
348 | $dumper = &$this->_getDumper(); |
349 | return "Pattern [$pattern] detected at [$position] in [" . |
350 | $dumper->describeValue($subject) . "] as [" . |
351 | $matches[0] . "] in region [" . |
352 | $dumper->clipString($subject, 40, $position) . "]"; |
353 | } |
354 | } |
355 | |
356 | /** |
357 | * Fail if a pattern is detected within the |
358 | * comparison. |
359 | * @package SimpleTest |
360 | * @subpackage UnitTester |
361 | */ |
362 | class UnwantedPatternExpectation extends WantedPatternExpectation { |
363 | |
364 | /** |
365 | * Sets the reject pattern |
366 | * @param string $pattern Pattern to search for. |
367 | * @param string $message Customised message on failure. |
368 | * @access public |
369 | */ |
370 | function UnwantedPatternExpectation($pattern, $message = '%s') { |
371 | $this->WantedPatternExpectation($pattern, $message); |
372 | } |
373 | |
374 | /** |
375 | * Tests the expectation. False if the Perl regex |
376 | * matches the comparison value. |
377 | * @param string $compare Comparison value. |
378 | * @return boolean True if correct. |
379 | * @access public |
380 | */ |
381 | function test($compare) { |
382 | return ! parent::test($compare); |
383 | } |
384 | |
385 | /** |
386 | * Returns a human readable test message. |
387 | * @param string $compare Comparison value. |
388 | * @return string Description of success |
389 | * or failure. |
390 | * @access public |
391 | */ |
392 | function testMessage($compare) { |
393 | if ($this->test($compare)) { |
394 | $dumper = &$this->_getDumper(); |
395 | return "Pattern [" . $this->_getPattern() . |
396 | "] not detected in [" . |
397 | $dumper->describeValue($compare) . "]"; |
398 | } else { |
399 | return $this->_decribePatternMatch($this->_getPattern(), $compare); |
400 | } |
401 | } |
402 | } |
403 | |
404 | /** |
405 | * Tests either type or class name if it's an object. |
406 | * @package SimpleTest |
407 | * @subpackage UnitTester |
408 | */ |
409 | class IsAExpectation extends SimpleExpectation { |
410 | var $_type; |
411 | |
412 | /** |
413 | * Sets the type to compare with. |
414 | * @param string $type Type or class name. |
415 | * @param string $message Customised message on failure. |
416 | * @access public |
417 | */ |
418 | function IsAExpectation($type, $message = '%s') { |
419 | $this->SimpleExpectation($message); |
420 | $this->_type = $type; |
421 | } |
422 | |
423 | /** |
424 | * Accessor for type to check against. |
425 | * @return string Type or class name. |
426 | * @access protected |
427 | */ |
428 | function getType() { |
429 | return $this->_type; |
430 | } |
431 | |
432 | /** |
433 | * Tests the expectation. True if the type or |
434 | * class matches the string value. |
435 | * @param string $compare Comparison value. |
436 | * @return boolean True if correct. |
437 | * @access public |
438 | */ |
439 | function test($compare) { |
440 | if (is_object($compare)) { |
441 | return SimpleTestCompatibility::isA($compare, $this->_type); |
442 | } else { |
443 | return (strtolower(gettype($compare)) == $this->_canonicalType($this->_type)); |
444 | } |
445 | } |
446 | |
447 | /** |
448 | * Coerces type name into a gettype() match. |
449 | * @param string $type User type. |
450 | * @return string Simpler type. |
451 | * @access private |
452 | */ |
453 | function _canonicalType($type) { |
454 | $type = strtolower($type); |
455 | $map = array( |
456 | 'bool' => 'boolean', |
457 | 'float' => 'double', |
458 | 'real' => 'double', |
459 | 'int' => 'integer'); |
460 | if (isset($map[$type])) { |
461 | $type = $map[$type]; |
462 | } |
463 | return $type; |
464 | } |
465 | |
466 | /** |
467 | * Returns a human readable test message. |
468 | * @param mixed $compare Comparison value. |
469 | * @return string Description of success |
470 | * or failure. |
471 | * @access public |
472 | */ |
473 | function testMessage($compare) { |
474 | $dumper = &$this->_getDumper(); |
475 | return "Value [" . $dumper->describeValue($compare) . |
476 | "] should be type [" . $this->_type . "]"; |
477 | } |
478 | } |
479 | |
480 | /** |
481 | * Tests either type or class name if it's an object. |
482 | * Will succeed if the type does not match. |
483 | * @package SimpleTest |
484 | * @subpackage UnitTester |
485 | */ |
486 | class NotAExpectation extends IsAExpectation { |
487 | var $_type; |
488 | |
489 | /** |
490 | * Sets the type to compare with. |
491 | * @param string $type Type or class name. |
492 | * @param string $message Customised message on failure. |
493 | * @access public |
494 | */ |
495 | function NotAExpectation($type, $message = '%s') { |
496 | $this->IsAExpectation($type, $message); |
497 | } |
498 | |
499 | /** |
500 | * Tests the expectation. False if the type or |
501 | * class matches the string value. |
502 | * @param string $compare Comparison value. |
503 | * @return boolean True if different. |
504 | * @access public |
505 | */ |
506 | function test($compare) { |
507 | return ! parent::test($compare); |
508 | } |
509 | |
510 | /** |
511 | * Returns a human readable test message. |
512 | * @param mixed $compare Comparison value. |
513 | * @return string Description of success |
514 | * or failure. |
515 | * @access public |
516 | */ |
517 | function testMessage($compare) { |
518 | $dumper = &$this->_getDumper(); |
519 | return "Value [" . $dumper->describeValue($compare) . |
520 | "] should not be type [" . $this->_getType() . "]"; |
521 | } |
522 | } |
523 | |
524 | /** |
525 | * Tests for existance of a method in an object |
526 | * @package SimpleTest |
527 | * @subpackage UnitTester |
528 | */ |
529 | class MethodExistsExpectation extends SimpleExpectation { |
530 | var $_method; |
531 | |
532 | /** |
533 | * Sets the value to compare against. |
534 | * @param string $method Method to check. |
535 | * @param string $message Customised message on failure. |
536 | * @access public |
537 | * @return void |
538 | */ |
539 | function MethodExistsExpectation($method, $message = '%s') { |
540 | $this->SimpleExpectation($message); |
541 | $this->_method = &$method; |
542 | } |
543 | |
544 | /** |
545 | * Tests the expectation. True if the method exists in the test object. |
546 | * @param string $compare Comparison method name. |
547 | * @return boolean True if correct. |
548 | * @access public |
549 | */ |
550 | function test($compare) { |
551 | return (boolean)(is_object($compare) && method_exists($compare, $this->_method)); |
552 | } |
553 | |
554 | /** |
555 | * Returns a human readable test message. |
556 | * @param mixed $compare Comparison value. |
557 | * @return string Description of success |
558 | * or failure. |
559 | * @access public |
560 | */ |
561 | function testMessage($compare) { |
562 | $dumper = &$this->_getDumper(); |
563 | if (! is_object($compare)) { |
564 | return 'No method on non-object [' . $dumper->describeValue($compare) . ']'; |
565 | } |
566 | $method = $this->_method; |
567 | return "Object [" . $dumper->describeValue($compare) . |
568 | "] should contain method [$method]"; |
569 | } |
570 | } |
571 | ?> |