diff options
author | Bram Moolenaar <Bram@vim.org> | 2016-02-07 19:19:53 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2016-02-07 19:19:53 +0100 |
commit | 595e64e259faefb330866852e1b9f6168544572a (patch) | |
tree | 87986bc108647e7c597195cea325ca130db69a40 /src/testdir/test_json.vim | |
parent | 55fab439a6f3bba6dbe780ac034b84d5822a1a96 (diff) | |
download | vim-595e64e259faefb330866852e1b9f6168544572a.zip |
patch 7.4.1279
Problem: jsonencode() is not producing strict JSON.
Solution: Add jsencode() and jsdecode(). Make jsonencode() and jsondecode()
strict.
Diffstat (limited to 'src/testdir/test_json.vim')
-rw-r--r-- | src/testdir/test_json.vim | 126 |
1 files changed, 121 insertions, 5 deletions
diff --git a/src/testdir/test_json.vim b/src/testdir/test_json.vim index 52cffc825..2534de113 100644 --- a/src/testdir/test_json.vim +++ b/src/testdir/test_json.vim @@ -32,9 +32,12 @@ let l3 = [1, 2] let s:varl3 = [l3, l3] let s:jsond1 = '{"a":1,"b":"bee","c":[1,2]}' +let s:jsd1 = '{a:1,b:"bee",c:[1,2]}' let s:vard1 = {"a": 1, "b": "bee","c": [1,2]} let s:jsond2 = '{"1":1,"2":{"a":"aa","b":{},"c":"cc"},"3":3}' +let s:jsd2 = '{"1":1,"2":{a:"aa",b:{},c:"cc"},"3":3}' let s:jsond2s = " { \"1\" : 1 , \"2\" :\n{ \"a\"\r: \"aa\" , \"b\" : {\<Tab>} , \"c\" : \"cc\" } , \"3\" : 3 }\r\n" +let s:jsd2s = " { \"1\" : 1 , \"2\" :\n{ a\r: \"aa\" , b : {\<Tab>} , c : \"cc\" } , \"3\" : 3 }\r\n" let s:vard2 = {"1": 1, "2": 2, "3": 3} let d2 = {"a": "aa", "b": s:vard2, "c": "cc"} let s:vard2["2"] = d2 @@ -42,11 +45,16 @@ let s:vard2x = {"1": 1, "2": {"a": "aa", "b": {}, "c": "cc"}, "3": 3} let d3 = {"a": 1, "b": 2} let s:vard3 = {"x": d3, "y": d3} let s:jsond3 = '{"x":{"a":1,"b":2},"y":{"a":1,"b":2}}' +let s:jsd3 = '{x:{a:1,b:2},y:{a:1,b:2}}' +let s:vard4 = {"key": v:none} +let s:vard4x = {"key": v:null} +let s:jsond4 = '{"key":null}' +let s:jsd4 = '{key:null}' -let s:jsonvals = '[true,false,,null]' -let s:varvals = [v:true, v:false, v:none, v:null] +let s:jsonvals = '[true,false,null,null]' +let s:varvals = [v:true, v:false, v:null, v:null] -func Test_encode() +func Test_json_encode() call assert_equal(s:json1, jsonencode(s:var1)) call assert_equal(s:json2, jsonencode(s:var2)) call assert_equal(s:json3, jsonencode(s:var3)) @@ -69,18 +77,18 @@ func Test_encode() call assert_equal(s:jsond1, jsonencode(s:vard1)) call assert_equal(s:jsond2, jsonencode(s:vard2)) call assert_equal(s:jsond3, jsonencode(s:vard3)) + call assert_equal(s:jsond4, jsonencode(s:vard4)) call assert_equal(s:jsonvals, jsonencode(s:varvals)) call assert_fails('echo jsonencode(function("tr"))', 'E474:') call assert_fails('echo jsonencode([function("tr")])', 'E474:') - call assert_fails('echo jsonencode({"key":v:none})', 'E474:') silent! let res = jsonencode(function("tr")) call assert_equal("", res) endfunc -func Test_decode() +func Test_json_decode() call assert_equal(s:var1, jsondecode(s:json1)) call assert_equal(s:var2, jsondecode(s:json2)) call assert_equal(s:var3, jsondecode(s:json3)) @@ -103,7 +111,9 @@ func Test_decode() call assert_equal(s:vard1, jsondecode(s:jsond1)) call assert_equal(s:vard2x, jsondecode(s:jsond2)) + call assert_equal(s:vard2x, jsondecode(s:jsond2s)) call assert_equal(s:vard3, jsondecode(s:jsond3)) + call assert_equal(s:vard4x, jsondecode(s:jsond4)) call assert_equal(s:varvals, jsondecode(s:jsonvals)) @@ -134,4 +144,110 @@ func Test_decode() call assert_fails('call jsondecode("[1")', "E474:") call assert_fails('call jsondecode("[1,")', "E474:") call assert_fails('call jsondecode("[1 2]")', "E474:") + + call assert_fails('call jsondecode("[1,,2]")', "E474:") +endfunc + +let s:jsl5 = '[7,,,]' +let s:varl5 = [7, v:none, v:none] + +func Test_js_encode() + call assert_equal(s:json1, jsencode(s:var1)) + call assert_equal(s:json2, jsencode(s:var2)) + call assert_equal(s:json3, jsencode(s:var3)) + call assert_equal(s:json4, jsencode(s:var4)) + call assert_equal(s:json5, jsencode(s:var5)) + + if has('multi_byte') + call assert_equal(s:jsonmb, jsencode(s:varmb)) + endif + + call assert_equal(s:jsonnr, jsencode(s:varnr)) + if has('float') + call assert_equal(s:jsonfl, jsencode(s:varfl)) + endif + + call assert_equal(s:jsonl1, jsencode(s:varl1)) + call assert_equal(s:jsonl2, jsencode(s:varl2)) + call assert_equal(s:jsonl3, jsencode(s:varl3)) + + call assert_equal(s:jsd1, jsencode(s:vard1)) + call assert_equal(s:jsd2, jsencode(s:vard2)) + call assert_equal(s:jsd3, jsencode(s:vard3)) + call assert_equal(s:jsd4, jsencode(s:vard4)) + + call assert_equal(s:jsonvals, jsencode(s:varvals)) + + call assert_fails('echo jsencode(function("tr"))', 'E474:') + call assert_fails('echo jsencode([function("tr")])', 'E474:') + + silent! let res = jsencode(function("tr")) + call assert_equal("", res) + + call assert_equal(s:jsl5, jsencode(s:varl5)) +endfunc + +func Test_js_decode() + call assert_equal(s:var1, jsdecode(s:json1)) + call assert_equal(s:var2, jsdecode(s:json2)) + call assert_equal(s:var3, jsdecode(s:json3)) + call assert_equal(s:var4, jsdecode(s:json4)) + call assert_equal(s:var5, jsdecode(s:json5)) + + if has('multi_byte') + call assert_equal(s:varmb, jsdecode(s:jsonmb)) + endif + + call assert_equal(s:varnr, jsdecode(s:jsonnr)) + if has('float') + call assert_equal(s:varfl, jsdecode(s:jsonfl)) + endif + + call assert_equal(s:varl1, jsdecode(s:jsonl1)) + call assert_equal(s:varl2x, jsdecode(s:jsonl2)) + call assert_equal(s:varl2x, jsdecode(s:jsonl2s)) + call assert_equal(s:varl3, jsdecode(s:jsonl3)) + + call assert_equal(s:vard1, jsdecode(s:jsond1)) + call assert_equal(s:vard1, jsdecode(s:jsd1)) + call assert_equal(s:vard2x, jsdecode(s:jsond2)) + call assert_equal(s:vard2x, jsdecode(s:jsd2)) + call assert_equal(s:vard2x, jsdecode(s:jsond2s)) + call assert_equal(s:vard2x, jsdecode(s:jsd2s)) + call assert_equal(s:vard3, jsdecode(s:jsond3)) + call assert_equal(s:vard3, jsdecode(s:jsd3)) + call assert_equal(s:vard4x, jsdecode(s:jsond4)) + call assert_equal(s:vard4x, jsdecode(s:jsd4)) + + call assert_equal(s:varvals, jsdecode(s:jsonvals)) + + call assert_equal(v:true, jsdecode('true')) + call assert_equal(type(v:true), type(jsdecode('true'))) + call assert_equal(v:none, jsdecode('')) + call assert_equal(type(v:none), type(jsdecode(''))) + call assert_equal("", jsdecode('""')) + + call assert_equal({'n': 1}, jsdecode('{"n":1,}')) + + call assert_fails('call jsdecode("\"")', "E474:") + call assert_fails('call jsdecode("blah")', "E474:") + call assert_fails('call jsdecode("true blah")', "E474:") + call assert_fails('call jsdecode("<foobar>")', "E474:") + + call assert_fails('call jsdecode("{")', "E474:") + call assert_fails('call jsdecode("{foobar}")', "E474:") + call assert_fails('call jsdecode("{\"n\",")', "E474:") + call assert_fails('call jsdecode("{\"n\":")', "E474:") + call assert_fails('call jsdecode("{\"n\":1")', "E474:") + call assert_fails('call jsdecode("{\"n\":1,")', "E474:") + call assert_fails('call jsdecode("{\"n\",1}")', "E474:") + call assert_fails('call jsdecode("{-}")', "E474:") + + call assert_fails('call jsdecode("[foobar]")', "E474:") + call assert_fails('call jsdecode("[")', "E474:") + call assert_fails('call jsdecode("[1")', "E474:") + call assert_fails('call jsdecode("[1,")', "E474:") + call assert_fails('call jsdecode("[1 2]")', "E474:") + + call assert_equal(s:varl5, jsdecode(s:jsl5)) endfunc |