11local qjson = require (" qjson" )
22local cjson = require (" cjson" )
33
4- describe (" qjson vs lua-cjson" , function ()
4+ local function read_file (path )
5+ local f = assert (io.open (path , " rb" ))
6+ local s = f :read (" *a" )
7+ f :close ()
8+ return s
9+ end
10+
11+ local function deep_equal (a , b )
12+ if a == b then
13+ return true
14+ end
15+ if type (a ) ~= type (b ) then
16+ return false
17+ end
18+ if type (a ) ~= " table" then
19+ return false
20+ end
21+ for k , v in pairs (a ) do
22+ if not deep_equal (v , b [k ]) then
23+ return false
24+ end
25+ end
26+ for k in pairs (b ) do
27+ if a [k ] == nil then
28+ return false
29+ end
30+ end
31+ return true
32+ end
33+
34+ local function assert_materializes_like_lua_cjson (src )
35+ assert .is_true (deep_equal (qjson .materialize (qjson .decode (src )), cjson .decode (src )))
36+ end
37+
38+ local function assert_encodes_like_lua_cjson (src )
39+ local out = qjson .encode (qjson .decode (src ))
40+ assert .is_true (deep_equal (cjson .decode (out ), cjson .decode (src )))
41+ end
42+
43+ local function assert_equivalent_json (src )
44+ assert_materializes_like_lua_cjson (src )
45+ assert_encodes_like_lua_cjson (src )
46+ end
47+
48+ local function assert_fixture_paths (paths )
49+ for _ , path in ipairs (paths ) do
50+ local p = path
51+
52+ it (" materializes like lua-cjson for fixture " .. p , function ()
53+ assert_materializes_like_lua_cjson (read_file (p ))
54+ end )
55+
56+ it (" encodes a lua-cjson-equivalent value for fixture " .. p , function ()
57+ assert_encodes_like_lua_cjson (read_file (p ))
58+ end )
59+ end
60+ end
61+
62+ describe (" qjson lua-cjson compatibility smoke" , function ()
563 it (" agrees on simple string field" , function ()
664 local s = ' {"a":"x"}'
765 assert .are .equal (cjson .decode (s ).a , qjson .parse (s ):get_str (" a" ))
@@ -27,3 +85,40 @@ describe("qjson vs lua-cjson", function()
2785 assert .are .equal (cjson .decode (s ).body .model , qjson .parse (s ):get_str (" body.model" ))
2886 end )
2987end )
88+
89+ describe (" qjson cJSON upstream fixtures" , function ()
90+ assert_fixture_paths ({
91+ " tests/vendor/cJSON/tests/inputs/test1" ,
92+ " tests/vendor/cJSON/tests/inputs/test2" ,
93+ " tests/vendor/cJSON/tests/inputs/test3" ,
94+ " tests/vendor/cJSON/tests/inputs/test4" ,
95+ " tests/vendor/cJSON/tests/inputs/test5" ,
96+ " tests/vendor/cJSON/tests/inputs/test7" ,
97+ " tests/vendor/cJSON/tests/inputs/test8" ,
98+ " tests/vendor/cJSON/tests/inputs/test9" ,
99+ " tests/vendor/cJSON/tests/inputs/test10" ,
100+ " tests/vendor/cJSON/tests/inputs/test11" ,
101+ " tests/vendor/cJSON/tests/json-patch-tests/cjson-utils-tests.json" ,
102+ " tests/vendor/cJSON/tests/json-patch-tests/package.json" ,
103+ " tests/vendor/cJSON/tests/json-patch-tests/spec_tests.json" ,
104+ " tests/vendor/cJSON/tests/json-patch-tests/tests.json" ,
105+ })
106+ end )
107+
108+ describe (" qjson simdjson upstream fixtures" , function ()
109+ assert_fixture_paths ({
110+ " tests/vendor/simdjson/jsonexamples/citm_catalog.json" ,
111+ " tests/vendor/simdjson/jsonexamples/example_config.json" ,
112+ " tests/vendor/simdjson/jsonexamples/twitter.json" ,
113+ })
114+
115+ it (" materializes and encodes each simdjson NDJSON record like lua-cjson" , function ()
116+ local src = read_file (" tests/vendor/simdjson/jsonexamples/amazon_cellphones.ndjson" )
117+ local records = 0
118+ for line in src :gmatch (" ([^\r\n ]+)" ) do
119+ records = records + 1
120+ assert_equivalent_json (line )
121+ end
122+ assert .is_true (records >= 793 )
123+ end )
124+ end )
0 commit comments