22
33namespace HJSON \Tests ;
44
5+ require_once ('src/HJSON/HJSONParser.php ' );
6+ require_once ('src/HJSON/HJSONStringifier.php ' );
7+ require_once ('src/HJSON/HJSONException.php ' );
8+ require_once ('src/HJSON/HJSONUtils.php ' );
9+
510use HJSON \HJSONParser ;
611use HJSON \HJSONStringifier ;
712use HJSON \HJSONException ;
8- use PHPUnit \Framework \TestCase ;
913
10- class HJSONParserTest extends TestCase
14+ class HJSONParserTest
1115{
1216
13- protected function setUp ()
17+ public function assertEquals ($ a , $ b )
18+ {
19+ if ($ a !== $ b ) {
20+ echo "\n\n" ;
21+ $ a2 = preg_split ('/\r\n|\r|\n/ ' , $ a );
22+ $ b2 = preg_split ('/\r\n|\r|\n/ ' , $ b );
23+ $ indexA = $ indexB = 0 ;
24+ for (; $ indexB < count ($ b2 ); $ indexA ++, $ indexB ++) {
25+ if ($ indexB >= count ($ a2 ) || $ b2 [$ indexB ] !== $ a2 [$ indexB ]) {
26+ $ indexA = $ indexB ;
27+ echo "Expected ( $ this ->lastFilename , line $ indexB): \n\n" ;
28+ while ($ indexB < count ($ b2 ) && (
29+ $ indexB >= count ($ a2 ) ||
30+ $ b2 [$ indexB ] !== $ a2 [$ indexB ])) {
31+ echo "| " .$ b2 [$ indexB ++]."| \n" ;
32+ }
33+
34+ echo "\n\nGot: \n\n" ;
35+ while ($ indexA < count ($ a2 ) && $ indexA < $ indexB ) {
36+ echo "| " .$ a2 [$ indexA ++]."| \n" ;
37+ }
38+
39+ echo "\n\n" ;
40+ }
41+ }
42+
43+ if ($ indexA < count ($ a2 )) {
44+ echo "\n\nGot trailing lines vs $ this ->lastFilename : \n\n" ;
45+ while ($ indexA < count ($ a2 )) {
46+ echo "| " .$ a2 [$ indexA ++]."| \n" ;
47+ }
48+ echo "\n\n" ;
49+ }
50+
51+ throw new HJSONException ();
52+ }
53+ }
54+
55+ public function setUp ()
1456 {
15- parent ::setUp ();
1657 $ this ->rootDir = dirname (__FILE__ ).DIRECTORY_SEPARATOR ."assets " ;
58+ $ this ->lastFilename = '' ;
1759 }
1860
1961 private function load ($ file , $ cr )
2062 {
63+ $ this ->lastFilename = $ file ;
2164 $ text = file_get_contents ($ this ->rootDir .DIRECTORY_SEPARATOR .$ file );
2265 $ std = mb_ereg_replace ('/\r/ ' , "" , $ text ); // make sure we have unix style text regardless of the input
2366 return $ cr ? mb_ereg_replace ("\n" , "\r\n" , $ std ) : $ std ;
@@ -37,6 +80,13 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
3780 $ this ->assertEquals ($ arrayData , json_decode (json_encode ($ data ), true ));
3881
3982 if (!$ shouldFail ) {
83+ if ($ isJson ) {
84+ // compare Hjson parse to JSON parse
85+ $ json1 = json_encode ($ data );
86+ $ json2 = json_encode (json_decode ($ text ));
87+ $ this ->assertEquals ($ json1 , $ json2 );
88+ }
89+
4090 $ text1 = json_encode ($ data , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
4191 $ stringifier = new HJSONStringifier ();
4292 $ hjson1 = $ stringifier ->stringify ($ data , [
@@ -46,15 +96,9 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
4696 ]);
4797 $ result = json_decode ($ this ->load ("{$ name }_result.json " , $ inputCr ));
4898 $ text2 = json_encode ($ result , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
49- $ hjson2 = $ this ->load ("{$ name }_result.hjson " , $ outputCr );
5099 $ this ->assertEquals ($ text1 , $ text2 );
100+ $ hjson2 = $ this ->load ("{$ name }_result.hjson " , $ outputCr );
51101 $ this ->assertEquals ($ hjson1 , $ hjson2 );
52- if ($ isJson ) {
53- // also compare Hjson parse to JSON parse
54- $ json1 = json_encode ($ data );
55- $ json2 = json_encode (json_decode ($ text ));
56- $ this ->assertEquals ($ json1 , $ json2 );
57- }
58102 } else {
59103 $ this ->markTestIncomplete ('This test succeeded on data that should fail. ' );
60104 }
@@ -67,6 +111,7 @@ private function runEach($name, $file, $isJson, $inputCr, $outputCr)
67111
68112 public function testAll ()
69113 {
114+ $ hasFailure = false ;
70115 $ files = array_diff (scandir ($ this ->rootDir ), ['.. ' , '. ' ]);
71116 foreach ($ files as $ file ) {
72117 $ name = explode ('_test. ' , $ file );
@@ -81,10 +126,34 @@ public function testAll()
81126 continue ;
82127 }
83128
84- $ this ->runEach ($ name , $ file , $ isJson , false , false );
85- $ this ->runEach ($ name , $ file , $ isJson , false , true );
86- $ this ->runEach ($ name , $ file , $ isJson , true , false );
87- $ this ->runEach ($ name , $ file , $ isJson , true , true );
129+ try {
130+ $ this ->runEach ($ name , $ file , $ isJson , false , false );
131+ } catch (HJSONException $ e ) {
132+ $ hasFailure = true ;
133+ }
134+ try {
135+ $ this ->runEach ($ name , $ file , $ isJson , false , true );
136+ } catch (HJSONException $ e ) {
137+ $ hasFailure = true ;
138+ }
139+ try {
140+ $ this ->runEach ($ name , $ file , $ isJson , true , false );
141+ } catch (HJSONException $ e ) {
142+ $ hasFailure = true ;
143+ }
144+ try {
145+ $ this ->runEach ($ name , $ file , $ isJson , true , true );
146+ } catch (HJSONException $ e ) {
147+ $ hasFailure = true ;
148+ }
149+ }
150+
151+ if ($ hasFailure ) {
152+ exit (1 );
88153 }
89154 }
90155}
156+
157+ $ tester = new HJSONParserTest ;
158+ $ tester ->setUp ();
159+ $ tester ->testAll ();
0 commit comments