3 * Small test for the minijson encoder/decoder routines
5 * Copyright © 2011, 2012, 2017
6 * mirabilos <t.glaser@tarent.de>
8 * Roland Mas <lolando@debian.org>
9 * Alain Peyrat <aljeux@free.fr>
11 * Provided that these terms and disclaimer and all copyright notices
12 * are retained or reproduced in an accompanying document, permission
13 * is granted to deal in this work without restriction, including un‐
14 * limited rights to use, publicly perform, distribute, sell, modify,
15 * merge, give away, or sublicence.
17 * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
18 * the utmost extent permitted by applicable law, neither express nor
19 * implied; without malicious intent or gross negligence. In no event
20 * may a licensor, author or contributor be held liable for indirect,
21 * direct, other damage, loss, or other issues arising in any way out
22 * of dealing in the work, even if advised of the possibility of such
23 * damage or existence of a defect, except proven that it results out
24 * of said person’s immediate fault when using the work as intended.
27 @include_once '/usr/local/share/php/vendor/autoload.php';
29 class minijsonTest extends PHPUnit\Framework\TestCase {
30 public function testMiniJson() {
31 $s = error_reporting(-1);
32 require_once(dirname(__FILE__) . '/../../../src/common/include/minijson.php');
34 /****************************************************************/
35 /* $s_orig [parse] print_r->$s_printr [encode] $s_ecompact or $s_epadded */
36 /* $s_e* [decode] print_r ->$s_printrs (due to Object key sorting) */
39 "JSON Test Pattern pass1",
40 {"object with 1 member":["array with 1 element"]},
48 "integer": 1234567890,
58 "controls": "\\b\\f\\n\\r\\t",
60 "alpha": "abcdefghijklmnopqrstuvwyz",
61 "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
62 "digit": "0123456789",
63 "0123456789": "digit",
64 "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",
65 "hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",
71 "address": "50 St. James Street",
72 "url": "http://www.JSON.org/",
73 "comment": "// /* <!-- --",
75 " s p a c e d " :[1,2 , 3
79 4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
80 "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",
81 "quotes": "" \\u0022 %22 0x22 034 "",
82 "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"
83 : "A key can be any string"
99 [0] => JSON Test Pattern pass1
102 [object with 1 member] => Array
104 [0] => array with 1 element
123 [integer] => 1234567890
124 [real] => -9876.54321
125 [e] => 1.23456789E-13
126 [E] => 1.23456789E+34
127 [] => 2.3456789012E+76
133 [controls] => ' . "\x08\x0C\x0A\x0D\x09" . '
135 [alpha] => abcdefghijklmnopqrstuvwyz
136 [ALPHA] => ABCDEFGHIJKLMNOPQRSTUVWYZ
137 [digit] => 0123456789
138 [0123456789] => digit
139 [special] => `1~!@#$%^&*()_+-={\':[,]}|;.</>?
152 [address] => 50 St. James Street
153 [url] => http://www.JSON.org/
154 [comment] => // /* <!-- --
155 [# -- --> */] => ' . '
156 [ s p a c e d ] => Array
178 [jsontext] => {"object with 1 member":["array with 1 element"]}
179 [quotes] => " " %22 0x22 034 "
180 [/\\"쫾몾ꮘﳞ볚
\b\f' . "\x0A\x0D\x09" . '`1~!@#$%^&*()_+-=[]{}|;:\',./<>?] => A key can be any string
199 [0] => JSON Test Pattern pass1
202 [object with 1 member] => Array
204 [0] => array with 1 element
223 [] => 2.3456789012E+76
224 [ s p a c e d ] => Array
235 [# -- --> */] => ' . '
236 [/\\"쫾몾ꮘﳞ볚' . "\x08\x0C\x0A\x0D\x09" . '`1~!@#$%^&*()_+-=[]{}|;:\',./<>?] => A key can be any string
237 [0123456789] => digit
238 [ALPHA] => ABCDEFGHIJKLMNOPQRSTUVWYZ
239 [E] => 1.23456789E+34
240 [address] => 50 St. James Street
241 [alpha] => abcdefghijklmnopqrstuvwyz
247 [comment] => // /* <!-- --
259 [controls] => ' . "\x08\x0C\x0A\x0D\x09" . '
260 [digit] => 0123456789
261 [e] => 1.23456789E-13
264 [integer] => 1234567890
265 [jsontext] => {"object with 1 member":["array with 1 element"]}
273 [quotes] => " " %22 0x22 034 "
274 [real] => -9876.54321
277 [special] => `1~!@#$%^&*()_+-={\':[,]}|;.</>?
279 [url] => http://www.JSON.org/
297 $s_ecompact = '["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},[],[],-42,true,false,null,{"":2.3456789012E+76," s p a c e d ":[1,2,3,4,5,6,7],"# -- --> */":" ","/\\\\\\"쫾몾ꮘﳞ볚\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?":"A key can be any string","0123456789":"digit","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","E":1.23456789E+34,"address":"50 St. James Street","alpha":"abcdefghijklmnopqrstuvwyz","array":[],"backslash":"\\\\","comment":"// /* <!-- --","compact":[1,2,3,4,5,6,7],"controls":"\\b\\f\\n\\r\\t","digit":"0123456789","e":1.23456789E-13,"false":false,"hex":"ģ䕧覫췯ꯍ","integer":1234567890,"jsontext":"{\\"object with 1 member\\":[\\"array with 1 element\\"]}","null":null,"object":[],"one":1,"quote":"\\"","quotes":"" \\" %22 0x22 034 "","real":-9.87654321E+3,"slash":"/ & /","space":" ","special":"`1~!@#$%^&*()_+-={\':[,]}|;.</>?","true":true,"url":"http://www.JSON.org/","zero":0},5.0E-1,9.86E+1,9.944E+1,1066,1.0E+1,1.0,1.0E-1,1.0,2.0,2.0,"rosebud"]';
299 "JSON Test Pattern pass1",
301 "object with 1 member": [
302 "array with 1 element"
312 "": 2.3456789012E+76,
323 "/\\\\\\"쫾몾ꮘﳞ볚\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?": "A key can be any string",
324 "0123456789": "digit",
325 "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
327 "address": "50 St. James Street",
328 "alpha": "abcdefghijklmnopqrstuvwyz",
331 "comment": "// /* <!-- --",
341 "controls": "\\b\\f\\n\\r\\t",
342 "digit": "0123456789",
346 "integer": 1234567890,
347 "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",
352 "quotes": "" \\" %22 0x22 034 "",
353 "real": -9.87654321E+3,
356 "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",
358 "url": "http://www.JSON.org/",
374 /****************************************************************/
377 $presult = minijson_decode($s_orig, $parsed);
378 $this->assertTrue($presult);
379 $this->assertEquals('array', gettype($parsed), 'FAIL parse-basic');
381 $printrd = print_r($parsed, true);
382 $this->assertEquals($s_printr, $printrd, 'parsed');
384 $encoded = minijson_encode($parsed, false);
385 $this->assertEquals($s_ecompact, $encoded, 'encode-compact');
387 $presult = minijson_decode($encoded, $reparsed);
388 $this->assertTrue($presult, 'can-reparse-compact');
389 $this->assertEquals('array', gettype($reparsed), 'FAIL reparse-compact-basic');
391 $printrd = print_r($reparsed, true);
392 $this->assertEquals($s_printrs, $printrd, 'reparsed-compact');
394 $encoded = minijson_encode($parsed);
395 $this->assertEquals($s_epadded, $encoded, 'encode-padded');
397 $presult = minijson_decode($encoded, $reparsed);
398 $this->assertTrue($presult, 'can-reparse-padded');
399 $this->assertEquals('array', gettype($reparsed), 'FAIL reparse-padded-basic');
401 $printrd = print_r($reparsed, true);
402 $this->assertEquals($s_printrs, $printrd, 'reparsed-padded');
406 $presult = minijson_decode($encoded, $reparsed);
407 $this->assertTrue($presult, 'can-parse-0');
408 $this->assertEquals(0, $reparsed);