3 * Small test for the minijson encoder/decoder routines
5 * Copyright © 2011, 2012
6 * Thorsten Glaser <mirabilos@evolvis.org>
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once 'PHPUnit/Framework/TestCase.php';
26 require_once dirname(__FILE__) . '/../../../src/common/include/minijson.php';
28 class Minijson_Tests extends PHPUnit_Framework_TestCase
30 /****************************************************************/
31 /* $s_orig [parse] print_r->$s_printr [encode] $s_ecompact or $s_epadded */
32 /* $s_e* [decode] print_r ->$s_printrs (due to Object key sorting) */
35 "JSON Test Pattern pass1",
36 {"object with 1 member":["array with 1 element"]},
44 "integer": 1234567890,
54 "controls": "\\b\\f\\n\\r\\t",
56 "alpha": "abcdefghijklmnopqrstuvwyz",
57 "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
58 "digit": "0123456789",
59 "0123456789": "digit",
60 "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",
61 "hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",
67 "address": "50 St. James Street",
68 "url": "http://www.JSON.org/",
69 "comment": "// /* <!-- --",
71 " s p a c e d " :[1,2 , 3
75 4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
76 "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",
77 "quotes": "" \\u0022 %22 0x22 034 "",
78 "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"
79 : "A key can be any string"
93 var $s_printr = 'Array
95 [0] => JSON Test Pattern pass1
98 [object with 1 member] => Array
100 [0] => array with 1 element
119 [integer] => 1234567890
120 [real] => -9876.54321
121 [e] => 1.23456789E-13
122 [E] => 1.23456789E+34
123 [] => 2.3456789012E+76
133 [alpha] => abcdefghijklmnopqrstuvwyz
134 [ALPHA] => ABCDEFGHIJKLMNOPQRSTUVWYZ
135 [digit] => 0123456789
136 [0123456789] => digit
137 [special] => `1~!@#$%^&*()_+-={\':[,]}|;.</>?
150 [address] => 50 St. James Street
151 [url] => http://www.JSON.org/
152 [comment] => // /* <!-- --
154 [ s p a c e d ] => Array
176 [jsontext] => {"object with 1 member":["array with 1 element"]}
177 [quotes] => " " %22 0x22 034 "
180 `1~!@#$%^&*()_+-=[]{}|;:\',./<>?] => A key can be any string
197 var $s_printrs = 'Array
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
238 `1~!@#$%^&*()_+-=[]{}|;:\',./<>?] => A key can be any string
239 [0123456789] => digit
240 [ALPHA] => ABCDEFGHIJKLMNOPQRSTUVWYZ
241 [E] => 1.23456789E+34
242 [address] => 50 St. James Street
243 [alpha] => abcdefghijklmnopqrstuvwyz
249 [comment] => // /* <!-- --
264 [digit] => 0123456789
265 [e] => 1.23456789E-13
268 [integer] => 1234567890
269 [jsontext] => {"object with 1 member":["array with 1 element"]}
277 [quotes] => " " %22 0x22 034 "
278 [real] => -9876.54321
281 [special] => `1~!@#$%^&*()_+-={\':[,]}|;.</>?
283 [url] => http://www.JSON.org/
301 var $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"]';
303 "JSON Test Pattern pass1",
305 "object with 1 member": [
306 "array with 1 element"
320 "": 2.3456789012E+76,
331 "/\\\\\\"쫾몾ꮘﳞ볚\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?": "A key can be any string",
332 "0123456789": "digit",
333 "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
335 "address": "50 St. James Street",
336 "alpha": "abcdefghijklmnopqrstuvwyz",
341 "comment": "// /* <!-- --",
351 "controls": "\\b\\f\\n\\r\\t",
352 "digit": "0123456789",
356 "integer": 1234567890,
357 "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",
364 "quotes": "" \\" %22 0x22 034 "",
365 "real": -9.87654321E+3,
368 "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",
370 "url": "http://www.JSON.org/",
386 /****************************************************************/
388 public function testMiniJson()
391 $presult = minijson_decode($this->s_orig, $parsed);
392 $this->assertTrue($presult);
393 $this->assertEquals('array', gettype($parsed), "FAIL parse-basic");
395 $printrd = print_r($parsed, true);
396 $this->assertEquals($this->s_printr, $printrd, "parsed");
398 $encoded = minijson_encode($parsed, false);
399 $this->assertEquals($this->s_ecompact, $encoded, "encode-compact");
401 $presult = minijson_decode($encoded, $reparsed);
402 $this->assertEquals(true, $presult, "can-reparse-compact");
403 $this->assertEquals('array', gettype($reparsed), "FAIL reparse-compact-basic");
405 $printrd = print_r($reparsed, true);
406 $this->assertEquals($this->s_printrs, $printrd, "reparsed-compact");
408 $encoded = minijson_encode($parsed);
409 $this->assertEquals($this->s_epadded, $encoded, "encode-padded");
411 $presult = minijson_decode($encoded, $reparsed);
412 $this->assertEquals(true, $presult, "can-reparse-padded");
413 $this->assertEquals('array', gettype($reparsed), "FAIL reparse-padded-basic");
415 $printrd = print_r($reparsed, true);
416 $this->assertEquals($this->s_printrs, $printrd, "reparsed-padded");