forked from mikemccabe/json-patch-php
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_tests.php
235 lines (205 loc) · 5.44 KB
/
run_tests.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
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
<?
// This is a simple test jig for testing JsonPatch.inc against
// the tests in json-patch-tests.
// Lots of cleanup needed!
require_once('JsonPatch.inc');
function json_format($json)
{
$tab = " ";
$new_json = "";
$indent_level = 0;
$in_string = false;
$json_obj = json_decode($json);
if($json_obj === false)
return false;
$json = json_encode($json_obj);
$len = strlen($json);
for($c = 0; $c < $len; $c++)
{
$char = $json[$c];
switch($char)
{
case '{':
case '[':
if(!$in_string)
{
$new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
$indent_level++;
}
else
{
$new_json .= $char;
}
break;
case '}':
case ']':
if(!$in_string)
{
$indent_level--;
$new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
}
else
{
$new_json .= $char;
}
break;
case ',':
if(!$in_string)
{
$new_json .= ",\n" . str_repeat($tab, $indent_level);
}
else
{
$new_json .= $char;
}
break;
case ':':
if(!$in_string)
{
$new_json .= ": ";
}
else
{
$new_json .= $char;
}
break;
case '"':
if($c > 0 && $json[$c-1] != '\\')
{
$in_string = !$in_string;
}
default:
$new_json .= $char;
break;
}
}
return $new_json;
}
// 'Recursive ksort' - prepare a php array s.t. json_encode might produce
// a canonical string.
function rksort($array = null) {
if ($array === null || !is_array($array))
{
return $array;
}
ksort($array);
foreach (array_keys($array) as $key) {
$array[$key] = rksort($array[$key]);
}
return $array;
}
function do_test($test) {
// Allow 'comment-only' test records
if (!(isset($test['doc']) && isset($test['patch'])))
return true;
try {
$patched = JsonPatch::patch($test['doc'], $test['patch']);
if (isset($test['error'])) {
print("test failed: expected error didn't occur\n");
print(json_format(json_encode($test)));
print("\n");
print("found: ");
print json_encode($patched);
print("\n");
}
if (!isset($test['expected'])) {
return true;
}
// XXX positive test here re: if error happens, and no exception, fail!
if (is_array($patched)) $patched = rksort($patched);
if (is_array($test['expected'])) $test['expected'] = rksort($test['expected']);
if (json_encode($patched) !== json_encode($test['expected'])) {
print("test failed:\n");
print(json_format(json_encode($test)));
print("\n");
print("found: " . json_encode($patched) . "\n");
print("expected: " . json_encode($test['expected']) . "\n\n");
return false;
} else {
return true;
}
} catch (Exception $ex) {
if (!isset($test['error'])) {
print("test failed with exception: " . $ex->getMessage() . "\n\n");
print(json_format(json_encode($test)));
return false;
} else {
/* print("$testindex: caught expected error: " . $ex->getMessage() . "\n"); */
/* print("expected: " . $test['error'] . "\n\n"); */
return true;
}
}
}
// Piggyback on patch tests to test diff as well -
// use 'doc' and 'expected' from testcases.
// Generate a diff, apply it, and check that it matches the target -
// in both directions.
function do_diff_test($test, $testindex) {
// Allow 'comment-only' test records
if (!(isset($test['doc']) && isset($test['expected'])))
return true;
try {
$doc1 = $test['doc']; // copy, in case sort/patch alters
$doc2 = $test['expected'];
$patch = JsonPatch::diff($doc1, $doc2);
$patched = JsonPatch::patch($doc1, $patch);
if (is_array($patched)) $patched = rksort($patched);
if (is_array($doc2)) $doc2 = rksort($doc2);
if (json_encode($patched) !== json_encode($doc2)) {
print("$testindex failed:\n");
print("diff: " . json_encode($patch) . "\n");
print("found: " . json_encode($patched) . "\n");
print("expected: " . json_encode($doc2) . "\n\n");
return false;
}
// reverse order
$doc1 = $test['expected']; // copy, in case sort/patch alters
$doc2 = $test['doc'];
$patch = JsonPatch::diff($doc1, $doc2);
$patched = JsonPatch::patch($doc1, $patch);
if (is_array($patched)) $patched = rksort($patched);
if (is_array($doc2)) $doc2 = rksort($doc2);
if (json_encode($patched) !== json_encode($doc2)) {
print("$testindex failed:\n");
print("diff: " . json_encode($patch) . "\n");
print("found: " . json_encode($patched) . "\n");
print("expected: " . json_encode($doc2) . "\n\n");
return false;
}
} catch (Exception $ex) {
print("$testindex: caught exception ".$ex->getMessage()."\n");
return false;
}
}
function main()
{
$testfile = file_get_contents("json-patch-tests/tests.json");
if (!$testfile)
{
print("Couldn't find test file json-patch-tests/tests.json\n");
return false;
}
$tests = json_decode($testfile, 1);
if (is_null($tests)) {
print("Error json-decoding test file\n");
return false;
}
$success = true;
foreach ($tests as $test) {
if (isset($test['disabled']))
continue;
if (!do_test($test))
{
$success = false;
}
}
/* print("Running diff tests\n\n"); */
return $success;
}
if (!main())
{
exit(1);
}
else {
exit(0);
}