forked from anthonybishopric/php-inferno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertions.php
More file actions
98 lines (85 loc) · 1.83 KB
/
assertions.php
File metadata and controls
98 lines (85 loc) · 1.83 KB
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
<?php
function task($message)
{
throw new AssertException(sprintf('You may delete this task once you %s', $message));
}
function fail($message)
{
throw new AssertException($message);
}
function assert_true($t)
{
if ($t !== true)
{
throw new AssertException(inferno_sprintf("%s was not true", $t));
}
}
function assert_that($t)
{
return new That($t);
}
class That
{
public function __construct($th)
{
$this->th = $th;
}
public function is_equal_to($other)
{
if ($this->th != $other || $other === __)
{
throw new AssertException(inferno_sprintf("Expected %s but was %s", $other, $this->th));
}
}
public function is_identical_to($other)
{
if ($this->th !== $other)
{
throw new AssertException(inferno_sprintf("Expected %s to be identical to %s, but it wasn't", $other, $this->th));
}
}
public function is_instance_of($other)
{
if (!is_a($this->th, $other))
{
throw new AssertException(inferno_sprintf("Expected %s to be an instance of %s but was %s", $this->th, $other, get_class($this->th)));
}
}
public function contains_string($other)
{
if (strpos($this->th, $other) === false)
{
throw new AssertException(inferno_sprintf("Expected '%s' to contain the string %s but it didn't", $this->th, $other));
}
}
}
class AssertException extends Exception
{
}
function inferno_sprintf($message/*, $arg1, $arg2 */)
{
$args = func_get_args();
$formatted_args = array();
array_shift($args);
foreach ($args as $argument)
{
if ($argument === null)
{
$formatted_args[] = '<null>';
}
else if ($argument === false)
{
$formatted_args[] = '<false>';
}
else if ($argument === true)
{
$formatted_args[] = '<true>';
}
else
{
$formatted_args[] = print_r($argument, true);
}
}
array_unshift($formatted_args, $message);
return call_user_func_array('sprintf', $formatted_args);
}