From 68e58444b48fc34c9a7c262883750778fbd935d7 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 30 Jan 2016 22:25:21 +0300 Subject: eval: Add jsonencode() function Ref #3471 --- test/functional/eval/json_functions_spec.lua | 213 +++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 test/functional/eval/json_functions_spec.lua (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua new file mode 100644 index 0000000000..5e1c6a3984 --- /dev/null +++ b/test/functional/eval/json_functions_spec.lua @@ -0,0 +1,213 @@ +local helpers = require('test.functional.helpers') +local clear = helpers.clear +local funcs = helpers.funcs +local eq = helpers.eq +local eval = helpers.eval +local execute = helpers.execute +local exc_exec = helpers.exc_exec + +describe('jsonencode() function', function() + before_each(clear) + + it('dumps strings', function() + eq('"Test"', funcs.jsonencode('Test')) + eq('""', funcs.jsonencode('')) + eq('"\\t"', funcs.jsonencode('\t')) + eq('"\\n"', funcs.jsonencode('\n')) + eq('"\\u001B"', funcs.jsonencode('\27')) + end) + + it('dumps numbers', function() + eq('0', funcs.jsonencode(0)) + eq('10', funcs.jsonencode(10)) + eq('-10', funcs.jsonencode(-10)) + end) + + it('dumps floats', function() + eq('0.0', eval('jsonencode(0.0)')) + eq('10.5', funcs.jsonencode(10.5)) + eq('-10.5', funcs.jsonencode(-10.5)) + eq('-1.0e-5', funcs.jsonencode(-1e-5)) + eq('1.0e50', eval('jsonencode(1.0e50)')) + end) + + it('dumps lists', function() + eq('[]', funcs.jsonencode({})) + eq('[[]]', funcs.jsonencode({{}})) + eq('[[], []]', funcs.jsonencode({{}, {}})) + end) + + it('dumps dictionaries', function() + eq('{}', eval('jsonencode({})')) + eq('{"d": []}', funcs.jsonencode({d={}})) + eq('{"d": [], "e": []}', funcs.jsonencode({d={}, e={}})) + end) + + it('cannot dump generic mapping with generic mapping keys and values', + function() + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}') + execute('let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}') + execute('let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}') + execute('call add(todump._VAL, [todumpv1, todumpv2])') + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + end) + + it('cannot dump generic mapping with ext key', function() + execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}') + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + end) + + it('cannot dump generic mapping with array key', function() + execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}') + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + end) + + it('cannot dump generic mapping with UINT64_MAX key', function() + execute('let todump = {"_TYPE": v:msgpack_types.integer}') + execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]') + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + end) + + it('cannot dump generic mapping with floating-point key', function() + execute('let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}') + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + end) + + it('can dump generic mapping with STR special key and NUL', function() + execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n"]}') + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') + eq('{"\\u0000": 1}', eval('jsonencode(todump)')) + end) + + it('can dump generic mapping with BIN special key and NUL', function() + execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n"]}') + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') + eq('{"\\u0000": 1}', eval('jsonencode(todump)')) + end) + + it('can dump STR special mapping with NUL and NL', function() + execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n", ""]}') + eq('"\\u0000\\n"', eval('jsonencode(todump)')) + end) + + it('can dump BIN special mapping with NUL and NL', function() + execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n", ""]}') + eq('"\\u0000\\n"', eval('jsonencode(todump)')) + end) + + it('cannot dump special ext mapping', function() + execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}') + eq('Vim(call):E474: Unable to convert EXT string to JSON', exc_exec('call jsonencode(todump)')) + end) + + it('can dump special array mapping', function() + execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}') + eq('[5, [""]]', eval('jsonencode(todump)')) + end) + + it('can dump special UINT64_MAX mapping', function() + execute('let todump = {"_TYPE": v:msgpack_types.integer}') + execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]') + eq('18446744073709551615', eval('jsonencode(todump)')) + end) + + it('can dump special INT64_MIN mapping', function() + execute('let todump = {"_TYPE": v:msgpack_types.integer}') + execute('let todump._VAL = [-1, 2, 0, 0]') + eq('-9223372036854775808', eval('jsonencode(todump)')) + end) + + it('can dump special BOOLEAN true mapping', function() + execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 1}') + eq('true', eval('jsonencode(todump)')) + end) + + it('can dump special BOOLEAN false mapping', function() + execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 0}') + eq('false', eval('jsonencode(todump)')) + end) + + it('can dump special NIL mapping', function() + execute('let todump = {"_TYPE": v:msgpack_types.nil, "_VAL": 0}') + eq('null', eval('jsonencode(todump)')) + end) + + it('fails to dump a function reference', function() + eq('Vim(call):E474: Error while dumping encode_tv2json() argument, itself: attempt to dump function reference', + exc_exec('call jsonencode(function("tr"))')) + end) + + it('fails to dump a function reference in a list', function() + eq('Vim(call):E474: Error while dumping encode_tv2json() argument, index 0: attempt to dump function reference', + exc_exec('call jsonencode([function("tr")])')) + end) + + it('fails to dump a recursive list', function() + execute('let todump = [[[]]]') + execute('call add(todump[0][0], todump)') + eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', + exc_exec('call jsonencode(todump)')) + end) + + it('fails to dump a recursive dict', function() + execute('let todump = {"d": {"d": {}}}') + execute('call extend(todump.d.d, {"d": todump})') + eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', + exc_exec('call jsonencode([todump])')) + end) + + it('can dump dict with two same dicts inside', function() + execute('let inter = {}') + execute('let todump = {"a": inter, "b": inter}') + eq('{"a": {}, "b": {}}', eval('jsonencode(todump)')) + end) + + it('can dump list with two same lists inside', function() + execute('let inter = []') + execute('let todump = [inter, inter]') + eq('[[], []]', eval('jsonencode(todump)')) + end) + + it('fails to dump a recursive list in a special dict', function() + execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}') + execute('call add(todump._VAL, todump)') + eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', + exc_exec('call jsonencode(todump)')) + end) + + it('fails to dump a recursive (val) map in a special dict', function() + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}') + execute('call add(todump._VAL, ["", todump])') + eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', + exc_exec('call jsonencode([todump])')) + end) + + it('fails to dump a recursive (val) map in a special dict, _VAL reference', function() + execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [["", []]]}') + execute('call add(todump._VAL[0][1], todump._VAL)') + eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', + exc_exec('call jsonencode(todump)')) + end) + + it('fails to dump a recursive (val) special list in a special dict', + function() + execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}') + execute('call add(todump._VAL, ["", todump._VAL])') + eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', + exc_exec('call jsonencode(todump)')) + end) + + it('fails when called with no arguments', function() + eq('Vim(call):E119: Not enough arguments for function: jsonencode', + exc_exec('call jsonencode()')) + end) + + it('fails when called with two arguments', function() + eq('Vim(call):E118: Too many arguments for function: jsonencode', + exc_exec('call jsonencode(["", ""], 1)')) + end) +end) -- cgit From e213ba150665328bae2b532491de5e12f72bc9ca Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 1 Feb 2016 21:22:07 +0300 Subject: eval: Add jsondecode() function --- test/functional/eval/json_functions_spec.lua | 232 +++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 5e1c6a3984..f979a6dd7c 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -6,6 +6,238 @@ local eval = helpers.eval local execute = helpers.execute local exc_exec = helpers.exc_exec +describe('jsondecode() function', function() + before_each(clear) + + it('accepts readfile()-style list', function() + eq({Test=1}, funcs.jsondecode({ + '{', + '\t"Test": 1', + '}', + })) + end) + + it('accepts strings with newlines', function() + eq({Test=1}, funcs.jsondecode([[ + { + "Test": 1 + } + ]])) + end) + + it('parses null, true, false', function() + eq(nil, funcs.jsondecode('null')) + eq(true, funcs.jsondecode('true')) + eq(false, funcs.jsondecode('false')) + end) + + it('fails to parse incomplete null, true, false', function() + eq('Vim(call):E474: Expected null: n', + exc_exec('call jsondecode("n")')) + eq('Vim(call):E474: Expected null: nu', + exc_exec('call jsondecode("nu")')) + eq('Vim(call):E474: Expected null: nul', + exc_exec('call jsondecode("nul")')) + eq('Vim(call):E474: Expected null: nul\n\t', + exc_exec('call jsondecode("nul\\n\\t")')) + + eq('Vim(call):E474: Expected true: t', + exc_exec('call jsondecode("t")')) + eq('Vim(call):E474: Expected true: tr', + exc_exec('call jsondecode("tr")')) + eq('Vim(call):E474: Expected true: tru', + exc_exec('call jsondecode("tru")')) + eq('Vim(call):E474: Expected true: tru\t\n', + exc_exec('call jsondecode("tru\\t\\n")')) + + eq('Vim(call):E474: Expected false: f', + exc_exec('call jsondecode("f")')) + eq('Vim(call):E474: Expected false: fa', + exc_exec('call jsondecode("fa")')) + eq('Vim(call):E474: Expected false: fal', + exc_exec('call jsondecode("fal")')) + eq('Vim(call):E474: Expected false: fal <', + exc_exec('call jsondecode(" fal <")')) + eq('Vim(call):E474: Expected false: fals', + exc_exec('call jsondecode("fals")')) + end) + + it('parses integer numbers', function() + eq(100000, funcs.jsondecode('100000')) + eq(-100000, funcs.jsondecode('-100000')) + eq(100000, funcs.jsondecode(' 100000 ')) + eq(-100000, funcs.jsondecode(' -100000 ')) + end) + + it('fails to parse +numbers', function() + eq('Vim(call):E474: Unidentified byte: +1000', + exc_exec('call jsondecode("+1000")')) + end) + + it('fails to parse negative numbers with space after -', function() + eq('Vim(call):E474: Missing number after minus sign: - 1000', + exc_exec('call jsondecode("- 1000")')) + end) + + it('fails to parse -', function() + eq('Vim(call):E474: Missing number after minus sign: -', + exc_exec('call jsondecode("-")')) + end) + + it('parses floating-point numbers', function() + eq('100000.0', eval('string(jsondecode("100000.0"))')) + eq(100000.5, funcs.jsondecode('100000.5')) + eq(-100000.5, funcs.jsondecode('-100000.5')) + eq(-100000.5e50, funcs.jsondecode('-100000.5e50')) + eq(100000.5e50, funcs.jsondecode('100000.5e50')) + eq(100000.5e50, funcs.jsondecode('100000.5e+50')) + eq(-100000.5e-50, funcs.jsondecode('-100000.5e-50')) + eq(100000.5e-50, funcs.jsondecode('100000.5e-50')) + end) + + it('fails to parse incomplete floating-point numbers', function() + eq('Vim(call):E474: Missing number after decimal dot: 0.', + exc_exec('call jsondecode("0.")')) + eq('Vim(call):E474: Missing exponent: 0.0e', + exc_exec('call jsondecode("0.0e")')) + eq('Vim(call):E474: Missing exponent: 0.0e+', + exc_exec('call jsondecode("0.0e+")')) + eq('Vim(call):E474: Missing exponent: 0.0e-', + exc_exec('call jsondecode("0.0e-")')) + end) + + it('fails to parse floating-point numbers with spaces inside', function() + eq('Vim(call):E474: Missing number after decimal dot: 0. ', + exc_exec('call jsondecode("0. ")')) + eq('Vim(call):E474: Missing number after decimal dot: 0. 0', + exc_exec('call jsondecode("0. 0")')) + eq('Vim(call):E474: Missing exponent: 0.0e 1', + exc_exec('call jsondecode("0.0e 1")')) + eq('Vim(call):E474: Missing exponent: 0.0e+ 1', + exc_exec('call jsondecode("0.0e+ 1")')) + eq('Vim(call):E474: Missing exponent: 0.0e- 1', + exc_exec('call jsondecode("0.0e- 1")')) + end) + + it('fails to parse "," and ":"', function() + eq('Vim(call):E474: Comma not inside container: , ', + exc_exec('call jsondecode(" , ")')) + eq('Vim(call):E474: Colon not inside container: : ', + exc_exec('call jsondecode(" : ")')) + end) + + it('parses empty containers', function() + eq({}, funcs.jsondecode('[]')) + eq('[]', eval('string(jsondecode("[]"))')) + end) + + it('fails to parse "[" and "{"', function() + eq('Vim(call):E474: Unexpected end of input: {', + exc_exec('call jsondecode("{")')) + eq('Vim(call):E474: Unexpected end of input: [', + exc_exec('call jsondecode("[")')) + end) + + it('fails to parse "}" and "]"', function() + eq('Vim(call):E474: No container to close: ]', + exc_exec('call jsondecode("]")')) + eq('Vim(call):E474: No container to close: }', + exc_exec('call jsondecode("}")')) + end) + + it('fails to parse containers which are closed by different brackets', + function() + eq('Vim(call):E474: Closing dictionary with bracket: ]', + exc_exec('call jsondecode("{]")')) + eq('Vim(call):E474: Closing list with figure brace: }', + exc_exec('call jsondecode("[}")')) + end) + + it('fails to parse containers with leading comma or colon', function() + eq('Vim(call):E474: Leading comma: ,}', + exc_exec('call jsondecode("{,}")')) + eq('Vim(call):E474: Leading comma: ,]', + exc_exec('call jsondecode("[,]")')) + eq('Vim(call):E474: Using colon not in dictionary: :]', + exc_exec('call jsondecode("[:]")')) + eq('Vim(call):E474: Unexpected colon: :}', + exc_exec('call jsondecode("{:}")')) + end) + + it('fails to parse containers with trailing comma', function() + eq('Vim(call):E474: Trailing comma: ]', + exc_exec('call jsondecode("[1,]")')) + eq('Vim(call):E474: Trailing comma: }', + exc_exec('call jsondecode("{\\"1\\": 2,}")')) + end) + + it('fails to parse dictionaries with missing value', function() + eq('Vim(call):E474: Expected value after colon: }', + exc_exec('call jsondecode("{\\"1\\":}")')) + eq('Vim(call):E474: Expected value: }', + exc_exec('call jsondecode("{\\"1\\"}")')) + end) + + it('fails to parse containers with two commas or colons', function() + eq('Vim(call):E474: Duplicate comma: , "2": 2}', + exc_exec('call jsondecode("{\\"1\\": 1,, \\"2\\": 2}")')) + eq('Vim(call):E474: Duplicate comma: , "2", 2]', + exc_exec('call jsondecode("[\\"1\\", 1,, \\"2\\", 2]")')) + eq('Vim(call):E474: Duplicate colon: : 2}', + exc_exec('call jsondecode("{\\"1\\": 1, \\"2\\":: 2}")')) + eq('Vim(call):E474: Comma after colon: , 2}', + exc_exec('call jsondecode("{\\"1\\": 1, \\"2\\":, 2}")')) + eq('Vim(call):E474: Unexpected colon: : "2": 2}', + exc_exec('call jsondecode("{\\"1\\": 1,: \\"2\\": 2}")')) + eq('Vim(call):E474: Unexpected colon: :, "2": 2}', + exc_exec('call jsondecode("{\\"1\\": 1:, \\"2\\": 2}")')) + end) + + it('fails to parse concat of two values', function() + eq('Vim(call):E474: Trailing characters: []', + exc_exec('call jsondecode("{}[]")')) + end) + + it('parses containers', function() + eq({1}, funcs.jsondecode('[1]')) + eq({nil, 1}, funcs.jsondecode('[null, 1]')) + eq({['1']=2}, funcs.jsondecode('{"1": 2}')) + eq({['1']=2, ['3']={{['4']={['5']={{}, 1}}}}}, + funcs.jsondecode('{"1": 2, "3": [{"4": {"5": [[], 1]}}]}')) + end) + + it('fails to parse incomplete strings', function() + eq('Vim(call):E474: Expected string end: \t"', + exc_exec('call jsondecode("\\t\\"")')) + eq('Vim(call):E474: Expected string end: \t"abc', + exc_exec('call jsondecode("\\t\\"abc")')) + eq('Vim(call):E474: Unfinished escape sequence: \t"abc\\', + exc_exec('call jsondecode("\\t\\"abc\\\\")')) + eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u', + exc_exec('call jsondecode("\\t\\"abc\\\\u")')) + eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u0', + exc_exec('call jsondecode("\\t\\"abc\\\\u0")')) + eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u00', + exc_exec('call jsondecode("\\t\\"abc\\\\u00")')) + eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u000', + exc_exec('call jsondecode("\\t\\"abc\\\\u000")')) + eq('Vim(call):E474: Expected string end: \t"abc\\u0000', + exc_exec('call jsondecode("\\t\\"abc\\\\u0000")')) + end) + + it('fails to parse unknown escape sequnces', function() + eq('Vim(call):E474: Unknown escape sequence: \\a"', + exc_exec('call jsondecode("\\t\\"\\\\a\\"")')) + end) + + it('parses strings properly', function() + eq('\n', funcs.jsondecode('"\\n"')) + eq('', funcs.jsondecode('""')) + eq('\\/"\t\b\n\r\f', funcs.jsondecode([["\\\/\"\t\b\n\r\f"]])) + eq('/a', funcs.jsondecode([["\/a"]])) + end) +end) + describe('jsonencode() function', function() before_each(clear) -- cgit From ea82270d30eef2dd716cd158d989f96fbd503ba6 Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 3 Feb 2016 21:01:21 +0300 Subject: eval/decode: Fail on control and invalid unicode characters --- test/functional/eval/json_functions_spec.lua | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index f979a6dd7c..9167cb2fef 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -235,6 +235,67 @@ describe('jsondecode() function', function() eq('', funcs.jsondecode('""')) eq('\\/"\t\b\n\r\f', funcs.jsondecode([["\\\/\"\t\b\n\r\f"]])) eq('/a', funcs.jsondecode([["\/a"]])) + -- Unicode characters: 2-byte, 3-byte, 4-byte + eq({ + '«', + 'ફ', + '\xF0\x90\x80\x80', + }, funcs.jsondecode({ + '[', + '"«",', + '"ફ",', + '"\xF0\x90\x80\x80"', + ']', + })) + end) + + it('fails on strings with invalid bytes', function() + eq('Vim(call):E474: Only UTF-8 strings allowed: \255"', + exc_exec('call jsondecode("\\t\\"\\xFF\\"")')) + eq('Vim(call):E474: ASCII control characters cannot be present inside string: ', + exc_exec('call jsondecode(["\\"\\n\\""])')) + -- 0xC2 starts 2-byte unicode character + eq('Vim(call):E474: Only UTF-8 strings allowed: \194"', + exc_exec('call jsondecode("\\t\\"\\xC2\\"")')) + -- 0xE0 0xAA starts 3-byte unicode character + eq('Vim(call):E474: Only UTF-8 strings allowed: \224"', + exc_exec('call jsondecode("\\t\\"\\xE0\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \224\170"', + exc_exec('call jsondecode("\\t\\"\\xE0\\xAA\\"")')) + -- 0xF0 0x90 0x80 starts 4-byte unicode character + eq('Vim(call):E474: Only UTF-8 strings allowed: \240"', + exc_exec('call jsondecode("\\t\\"\\xF0\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144"', + exc_exec('call jsondecode("\\t\\"\\xF0\\x90\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144\128"', + exc_exec('call jsondecode("\\t\\"\\xF0\\x90\\x80\\"")')) + -- 0xF9 0x80 0x80 0x80 starts 5-byte unicode character + eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9"', + exc_exec('call jsondecode("\\t\\"\\xF9\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80"', + exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80"', + exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80\x80"', + exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\x80\\"")')) + -- 0xFC 0x90 0x80 0x80 0x80 starts 6-byte unicode character + eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC"', + exc_exec('call jsondecode("\\t\\"\\xFC\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90"', + exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80"', + exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80"', + exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\"")')) + eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80\x80"', + exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\"")')) + -- Specification does not allow unquoted characters above 0x10FFFF + eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xF9\x80\x80\x80\x80"', + exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\x80\\x80\\"")')) + eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xFC\x90\x80\x80\x80\x80"', + exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\x80\\"")')) + -- '"\xF9\x80\x80\x80\x80"', + -- '"\xFC\x90\x80\x80\x80\x80"', end) end) -- cgit From 5814e29cdbe370a417d654dbd18620849aa00a09 Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 3 Feb 2016 21:46:01 +0300 Subject: eval/decode: Fix surrogate pairs processing --- test/functional/eval/json_functions_spec.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 9167cb2fef..651f6c27b3 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -297,6 +297,24 @@ describe('jsondecode() function', function() -- '"\xF9\x80\x80\x80\x80"', -- '"\xFC\x90\x80\x80\x80\x80"', end) + + it('parses surrogate pairs properly', function() + eq('\xF0\x90\x80\x80', funcs.jsondecode('"\\uD800\\uDC00"')) + eq('\xED\xA0\x80a\xED\xB0\x80', funcs.jsondecode('"\\uD800a\\uDC00"')) + eq('\xED\xA0\x80\t\xED\xB0\x80', funcs.jsondecode('"\\uD800\\t\\uDC00"')) + + eq('\xED\xA0\x80', funcs.jsondecode('"\\uD800"')) + eq('\xED\xA0\x80a', funcs.jsondecode('"\\uD800a"')) + eq('\xED\xA0\x80\t', funcs.jsondecode('"\\uD800\\t"')) + + eq('\xED\xB0\x80', funcs.jsondecode('"\\uDC00"')) + eq('\xED\xB0\x80a', funcs.jsondecode('"\\uDC00a"')) + eq('\xED\xB0\x80\t', funcs.jsondecode('"\\uDC00\\t"')) + + eq('\xED\xB0\x80', funcs.jsondecode('"\\uDC00"')) + eq('a\xED\xB0\x80', funcs.jsondecode('"a\\uDC00"')) + eq('\t\xED\xB0\x80', funcs.jsondecode('"\\t\\uDC00"')) + end) end) describe('jsonencode() function', function() -- cgit From 2c378fdfaf4927b7071b2e673c19c8acb8dcdfd4 Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 5 Feb 2016 00:29:47 +0300 Subject: eval/decode: Parse strings with NUL to special dictionaries --- test/functional/eval/json_functions_spec.lua | 71 +++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 651f6c27b3..7916bc829c 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -1,13 +1,63 @@ local helpers = require('test.functional.helpers') local clear = helpers.clear local funcs = helpers.funcs +local meths = helpers.meths local eq = helpers.eq local eval = helpers.eval local execute = helpers.execute local exc_exec = helpers.exc_exec describe('jsondecode() function', function() - before_each(clear) + before_each(function() + clear() + execute([[ + function Eq(exp, act) + let act = a:act + let exp = a:exp + if type(exp) != type(act) + return 0 + endif + if type(exp) == type({}) + if sort(keys(exp)) !=# sort(keys(act)) + return 0 + endif + if sort(keys(exp)) ==# ['_TYPE', '_VAL'] + let exp_typ = v:msgpack_types[exp._TYPE] + let act_typ = act._TYPE + if exp_typ isnot act_typ + return 0 + endif + return Eq(exp._VAL, act._VAL) + else + return empty(filter(copy(exp), '!Eq(v:val, act[v:key])')) + endif + else + if type(exp) == type([]) + if len(exp) != len(act) + return 0 + endif + return empty(filter(copy(exp), '!Eq(v:val, act[v:key])')) + endif + return exp ==# act + endif + return 1 + endfunction + ]]) + execute([[ + function EvalEq(exp, act_expr) + let act = eval(a:act_expr) + if Eq(a:exp, act) + return 1 + else + return string(act) + endif + endfunction + ]]) + end) + + local speq = function(expected, actual_expr) + eq(1, funcs.EvalEq(expected, actual_expr)) + end it('accepts readfile()-style list', function() eq({Test=1}, funcs.jsondecode({ @@ -221,6 +271,14 @@ describe('jsondecode() function', function() exc_exec('call jsondecode("\\t\\"abc\\\\u00")')) eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u000', exc_exec('call jsondecode("\\t\\"abc\\\\u000")')) + eq('Vim(call):E474: Expected four hex digits after \\u: \\u" ', + exc_exec('call jsondecode("\\t\\"abc\\\\u\\" ")')) + eq('Vim(call):E474: Expected four hex digits after \\u: \\u0" ', + exc_exec('call jsondecode("\\t\\"abc\\\\u0\\" ")')) + eq('Vim(call):E474: Expected four hex digits after \\u: \\u00" ', + exc_exec('call jsondecode("\\t\\"abc\\\\u00\\" ")')) + eq('Vim(call):E474: Expected four hex digits after \\u: \\u000" ', + exc_exec('call jsondecode("\\t\\"abc\\\\u000\\" ")')) eq('Vim(call):E474: Expected string end: \t"abc\\u0000', exc_exec('call jsondecode("\\t\\"abc\\\\u0000")')) end) @@ -315,6 +373,17 @@ describe('jsondecode() function', function() eq('a\xED\xB0\x80', funcs.jsondecode('"a\\uDC00"')) eq('\t\xED\xB0\x80', funcs.jsondecode('"\\t\\uDC00"')) end) + + local sp_decode_eq = function(expected, json) + meths.set_var('__json', json) + speq(expected, 'jsondecode(g:__json)') + execute('unlet! g:__json') + end + + it('parses strings with NUL properly', function() + sp_decode_eq({_TYPE='string', _VAL={'\n'}}, '"\\u0000"') + sp_decode_eq({_TYPE='string', _VAL={'\n', '\n'}}, '"\\u0000\\n\\u0000"') + end) end) describe('jsonencode() function', function() -- cgit From e303ea8a19bcd385eb7829beb7f2ef691c064b35 Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 5 Feb 2016 02:29:10 +0300 Subject: eval/decode: Add support for special maps Special dictionaries representing map are created when encountering duplicate key or when key is empty or contains NUL. Also checks that values are separated by a comma/colon properly. --- test/functional/eval/json_functions_spec.lua | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 7916bc829c..6f81a36479 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -203,6 +203,19 @@ describe('jsondecode() function', function() exc_exec('call jsondecode("[}")')) end) + it('fails to parse concat inside container', function() + eq('Vim(call):E474: Expected comma before list item: []]', + exc_exec('call jsondecode("[[][]]")')) + eq('Vim(call):E474: Expected comma before list item: {}]', + exc_exec('call jsondecode("[{}{}]")')) + eq('Vim(call):E474: Expected comma before list item: ]', + exc_exec('call jsondecode("[1 2]")')) + eq('Vim(call):E474: Expected comma before dictionary key: ": 4}', + exc_exec('call jsondecode("{\\"1\\": 2 \\"3\\": 4}")')) + eq('Vim(call):E474: Expected colon before dictionary value: , "3" 4}', + exc_exec('call jsondecode("{\\"1\\" 2, \\"3\\" 4}")')) + end) + it('fails to parse containers with leading comma or colon', function() eq('Vim(call):E474: Leading comma: ,}', exc_exec('call jsondecode("{,}")')) @@ -384,6 +397,45 @@ describe('jsondecode() function', function() sp_decode_eq({_TYPE='string', _VAL={'\n'}}, '"\\u0000"') sp_decode_eq({_TYPE='string', _VAL={'\n', '\n'}}, '"\\u0000\\n\\u0000"') end) + + it('parses dictionaries with duplicate keys to special maps', function() + sp_decode_eq({_TYPE='map', _VAL={{'a', 1}, {'a', 2}}}, + '{"a": 1, "a": 2}') + sp_decode_eq({_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'a', 2}}}, + '{"b": 3, "a": 1, "a": 2}') + sp_decode_eq({_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'a', 2}}}, + '{"b": 3, "a": 1, "c": 4, "a": 2}') + sp_decode_eq({_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'a', 2}, {'c', 4}}}, + '{"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}') + sp_decode_eq({{_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'a', 2}, {'c', 4}}}}, + '[{"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}]') + sp_decode_eq({{d={_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'a', 2}, {'c', 4}}}}}, + '[{"d": {"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}}]') + sp_decode_eq({1, {d={_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'a', 2}, {'c', 4}}}}}, + '[1, {"d": {"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}}]') + sp_decode_eq({1, {a={}, d={_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'a', 2}, {'c', 4}}}}}, + '[1, {"a": [], "d": {"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}}]') + end) + + it('parses dictionaries with empty keys to special maps', function() + sp_decode_eq({_TYPE='map', _VAL={{'', 4}}}, + '{"": 4}') + sp_decode_eq({_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'d', 2}, {'', 4}}}, + '{"b": 3, "a": 1, "c": 4, "d": 2, "": 4}') + sp_decode_eq({_TYPE='map', _VAL={{'', 3}, {'a', 1}, {'c', 4}, {'d', 2}, {'', 4}}}, + '{"": 3, "a": 1, "c": 4, "d": 2, "": 4}') + sp_decode_eq({{_TYPE='map', _VAL={{'', 3}, {'a', 1}, {'c', 4}, {'d', 2}, {'', 4}}}}, + '[{"": 3, "a": 1, "c": 4, "d": 2, "": 4}]') + end) + + it('parses dictionaries with keys with NUL bytes to special maps', function() + sp_decode_eq({_TYPE='map', _VAL={{{_TYPE='string', _VAL={'a\n', 'b'}}, 4}}}, + '{"a\\u0000\\nb": 4}') + sp_decode_eq({_TYPE='map', _VAL={{{_TYPE='string', _VAL={'a\n', 'b', ''}}, 4}}}, + '{"a\\u0000\\nb\\n": 4}') + sp_decode_eq({_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'d', 2}, {{_TYPE='string', _VAL={'\n'}}, 4}}}, + '{"b": 3, "a": 1, "c": 4, "d": 2, "\\u0000": 4}') + end) end) describe('jsonencode() function', function() -- cgit From 569e404622900222d88d856adbc6421734146bea Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 6 Feb 2016 23:07:53 +0300 Subject: eval/encode: Fix non-utf-8 &encoding handling, add tests --- test/functional/eval/json_functions_spec.lua | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 6f81a36479..13597eb7a0 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -8,8 +8,8 @@ local execute = helpers.execute local exc_exec = helpers.exc_exec describe('jsondecode() function', function() - before_each(function() - clear() + local restart = function(cmd) + clear(cmd) execute([[ function Eq(exp, act) let act = a:act @@ -53,7 +53,8 @@ describe('jsondecode() function', function() endif endfunction ]]) - end) + end + before_each(restart) local speq = function(expected, actual_expr) eq(1, funcs.EvalEq(expected, actual_expr)) @@ -396,6 +397,7 @@ describe('jsondecode() function', function() it('parses strings with NUL properly', function() sp_decode_eq({_TYPE='string', _VAL={'\n'}}, '"\\u0000"') sp_decode_eq({_TYPE='string', _VAL={'\n', '\n'}}, '"\\u0000\\n\\u0000"') + sp_decode_eq({_TYPE='string', _VAL={'\n«\n'}}, '"\\u0000\\u00AB\\u0000"') end) it('parses dictionaries with duplicate keys to special maps', function() @@ -436,6 +438,12 @@ describe('jsondecode() function', function() sp_decode_eq({_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'d', 2}, {{_TYPE='string', _VAL={'\n'}}, 4}}}, '{"b": 3, "a": 1, "c": 4, "d": 2, "\\u0000": 4}') end) + + it('converts strings to latin1 when &encoding is latin1', function() + restart('set encoding=latin1') + eq('\xAB', funcs.jsondecode('"\\u00AB"')) + sp_decode_eq({_TYPE='string', _VAL={'\n\xAB\n'}}, '"\\u0000\\u00AB\\u0000"') + end) end) describe('jsonencode() function', function() @@ -447,6 +455,7 @@ describe('jsonencode() function', function() eq('"\\t"', funcs.jsonencode('\t')) eq('"\\n"', funcs.jsonencode('\n')) eq('"\\u001B"', funcs.jsonencode('\27')) + eq('"þÿþ"', funcs.jsonencode('þÿþ')) end) it('dumps numbers', function() @@ -642,4 +651,16 @@ describe('jsonencode() function', function() eq('Vim(call):E118: Too many arguments for function: jsonencode', exc_exec('call jsonencode(["", ""], 1)')) end) + + it('converts strings from latin1 when &encoding is latin1', function() + clear('set encoding=latin1') + eq('"\\u00AB"', funcs.jsonencode('\xAB')) + eq('"\\u0000\\u00AB\\u0000"', eval('jsonencode({"_TYPE": v:msgpack_types.string, "_VAL": ["\\n\xAB\\n"]})')) + end) + + it('ignores improper values in &isprint', function() + meths.set_option('isprint', '1') + eq(1, eval('"\x01" =~# "\\\\p"')) + eq('"\\u0001"', funcs.jsonencode('\x01')) + end) end) -- cgit From f0bd4a149408e75ebf887530964e0948518938dc Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 11 Feb 2016 01:29:09 +0300 Subject: eval/encode: Fix invalid UTF-8 strings handling: 1. Do not allow reading past buffer end when creating error messages. 2. Fix surrogate pairs range, avoid magic constants. --- test/functional/eval/json_functions_spec.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 13597eb7a0..398fab6c4b 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -663,4 +663,11 @@ describe('jsonencode() function', function() eq(1, eval('"\x01" =~# "\\\\p"')) eq('"\\u0001"', funcs.jsonencode('\x01')) end) + + it('fails when using surrogate character in a UTF-8 string', function() + eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xA0\x80', + exc_exec('call jsonencode("\xED\xA0\x80")')) + eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xAF\xBF', + exc_exec('call jsonencode("\xED\xAF\xBF")')) + end) end) -- cgit From 2f67786796d5fb4237f4b0258ec3db0982cc7f53 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 13 Feb 2016 21:39:28 +0300 Subject: eval: Rename json* functions to json_* --- test/functional/eval/json_functions_spec.lua | 368 +++++++++++++-------------- 1 file changed, 184 insertions(+), 184 deletions(-) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 398fab6c4b..0b0403ce8e 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -7,7 +7,7 @@ local eval = helpers.eval local execute = helpers.execute local exc_exec = helpers.exc_exec -describe('jsondecode() function', function() +describe('json_decode() function', function() local restart = function(cmd) clear(cmd) execute([[ @@ -61,7 +61,7 @@ describe('jsondecode() function', function() end it('accepts readfile()-style list', function() - eq({Test=1}, funcs.jsondecode({ + eq({Test=1}, funcs.json_decode({ '{', '\t"Test": 1', '}', @@ -69,7 +69,7 @@ describe('jsondecode() function', function() end) it('accepts strings with newlines', function() - eq({Test=1}, funcs.jsondecode([[ + eq({Test=1}, funcs.json_decode([[ { "Test": 1 } @@ -77,242 +77,242 @@ describe('jsondecode() function', function() end) it('parses null, true, false', function() - eq(nil, funcs.jsondecode('null')) - eq(true, funcs.jsondecode('true')) - eq(false, funcs.jsondecode('false')) + eq(nil, funcs.json_decode('null')) + eq(true, funcs.json_decode('true')) + eq(false, funcs.json_decode('false')) end) it('fails to parse incomplete null, true, false', function() eq('Vim(call):E474: Expected null: n', - exc_exec('call jsondecode("n")')) + exc_exec('call json_decode("n")')) eq('Vim(call):E474: Expected null: nu', - exc_exec('call jsondecode("nu")')) + exc_exec('call json_decode("nu")')) eq('Vim(call):E474: Expected null: nul', - exc_exec('call jsondecode("nul")')) + exc_exec('call json_decode("nul")')) eq('Vim(call):E474: Expected null: nul\n\t', - exc_exec('call jsondecode("nul\\n\\t")')) + exc_exec('call json_decode("nul\\n\\t")')) eq('Vim(call):E474: Expected true: t', - exc_exec('call jsondecode("t")')) + exc_exec('call json_decode("t")')) eq('Vim(call):E474: Expected true: tr', - exc_exec('call jsondecode("tr")')) + exc_exec('call json_decode("tr")')) eq('Vim(call):E474: Expected true: tru', - exc_exec('call jsondecode("tru")')) + exc_exec('call json_decode("tru")')) eq('Vim(call):E474: Expected true: tru\t\n', - exc_exec('call jsondecode("tru\\t\\n")')) + exc_exec('call json_decode("tru\\t\\n")')) eq('Vim(call):E474: Expected false: f', - exc_exec('call jsondecode("f")')) + exc_exec('call json_decode("f")')) eq('Vim(call):E474: Expected false: fa', - exc_exec('call jsondecode("fa")')) + exc_exec('call json_decode("fa")')) eq('Vim(call):E474: Expected false: fal', - exc_exec('call jsondecode("fal")')) + exc_exec('call json_decode("fal")')) eq('Vim(call):E474: Expected false: fal <', - exc_exec('call jsondecode(" fal <")')) + exc_exec('call json_decode(" fal <")')) eq('Vim(call):E474: Expected false: fals', - exc_exec('call jsondecode("fals")')) + exc_exec('call json_decode("fals")')) end) it('parses integer numbers', function() - eq(100000, funcs.jsondecode('100000')) - eq(-100000, funcs.jsondecode('-100000')) - eq(100000, funcs.jsondecode(' 100000 ')) - eq(-100000, funcs.jsondecode(' -100000 ')) + eq(100000, funcs.json_decode('100000')) + eq(-100000, funcs.json_decode('-100000')) + eq(100000, funcs.json_decode(' 100000 ')) + eq(-100000, funcs.json_decode(' -100000 ')) end) it('fails to parse +numbers', function() eq('Vim(call):E474: Unidentified byte: +1000', - exc_exec('call jsondecode("+1000")')) + exc_exec('call json_decode("+1000")')) end) it('fails to parse negative numbers with space after -', function() eq('Vim(call):E474: Missing number after minus sign: - 1000', - exc_exec('call jsondecode("- 1000")')) + exc_exec('call json_decode("- 1000")')) end) it('fails to parse -', function() eq('Vim(call):E474: Missing number after minus sign: -', - exc_exec('call jsondecode("-")')) + exc_exec('call json_decode("-")')) end) it('parses floating-point numbers', function() - eq('100000.0', eval('string(jsondecode("100000.0"))')) - eq(100000.5, funcs.jsondecode('100000.5')) - eq(-100000.5, funcs.jsondecode('-100000.5')) - eq(-100000.5e50, funcs.jsondecode('-100000.5e50')) - eq(100000.5e50, funcs.jsondecode('100000.5e50')) - eq(100000.5e50, funcs.jsondecode('100000.5e+50')) - eq(-100000.5e-50, funcs.jsondecode('-100000.5e-50')) - eq(100000.5e-50, funcs.jsondecode('100000.5e-50')) + eq('100000.0', eval('string(json_decode("100000.0"))')) + eq(100000.5, funcs.json_decode('100000.5')) + eq(-100000.5, funcs.json_decode('-100000.5')) + eq(-100000.5e50, funcs.json_decode('-100000.5e50')) + eq(100000.5e50, funcs.json_decode('100000.5e50')) + eq(100000.5e50, funcs.json_decode('100000.5e+50')) + eq(-100000.5e-50, funcs.json_decode('-100000.5e-50')) + eq(100000.5e-50, funcs.json_decode('100000.5e-50')) end) it('fails to parse incomplete floating-point numbers', function() eq('Vim(call):E474: Missing number after decimal dot: 0.', - exc_exec('call jsondecode("0.")')) + exc_exec('call json_decode("0.")')) eq('Vim(call):E474: Missing exponent: 0.0e', - exc_exec('call jsondecode("0.0e")')) + exc_exec('call json_decode("0.0e")')) eq('Vim(call):E474: Missing exponent: 0.0e+', - exc_exec('call jsondecode("0.0e+")')) + exc_exec('call json_decode("0.0e+")')) eq('Vim(call):E474: Missing exponent: 0.0e-', - exc_exec('call jsondecode("0.0e-")')) + exc_exec('call json_decode("0.0e-")')) end) it('fails to parse floating-point numbers with spaces inside', function() eq('Vim(call):E474: Missing number after decimal dot: 0. ', - exc_exec('call jsondecode("0. ")')) + exc_exec('call json_decode("0. ")')) eq('Vim(call):E474: Missing number after decimal dot: 0. 0', - exc_exec('call jsondecode("0. 0")')) + exc_exec('call json_decode("0. 0")')) eq('Vim(call):E474: Missing exponent: 0.0e 1', - exc_exec('call jsondecode("0.0e 1")')) + exc_exec('call json_decode("0.0e 1")')) eq('Vim(call):E474: Missing exponent: 0.0e+ 1', - exc_exec('call jsondecode("0.0e+ 1")')) + exc_exec('call json_decode("0.0e+ 1")')) eq('Vim(call):E474: Missing exponent: 0.0e- 1', - exc_exec('call jsondecode("0.0e- 1")')) + exc_exec('call json_decode("0.0e- 1")')) end) it('fails to parse "," and ":"', function() eq('Vim(call):E474: Comma not inside container: , ', - exc_exec('call jsondecode(" , ")')) + exc_exec('call json_decode(" , ")')) eq('Vim(call):E474: Colon not inside container: : ', - exc_exec('call jsondecode(" : ")')) + exc_exec('call json_decode(" : ")')) end) it('parses empty containers', function() - eq({}, funcs.jsondecode('[]')) - eq('[]', eval('string(jsondecode("[]"))')) + eq({}, funcs.json_decode('[]')) + eq('[]', eval('string(json_decode("[]"))')) end) it('fails to parse "[" and "{"', function() eq('Vim(call):E474: Unexpected end of input: {', - exc_exec('call jsondecode("{")')) + exc_exec('call json_decode("{")')) eq('Vim(call):E474: Unexpected end of input: [', - exc_exec('call jsondecode("[")')) + exc_exec('call json_decode("[")')) end) it('fails to parse "}" and "]"', function() eq('Vim(call):E474: No container to close: ]', - exc_exec('call jsondecode("]")')) + exc_exec('call json_decode("]")')) eq('Vim(call):E474: No container to close: }', - exc_exec('call jsondecode("}")')) + exc_exec('call json_decode("}")')) end) it('fails to parse containers which are closed by different brackets', function() eq('Vim(call):E474: Closing dictionary with bracket: ]', - exc_exec('call jsondecode("{]")')) + exc_exec('call json_decode("{]")')) eq('Vim(call):E474: Closing list with figure brace: }', - exc_exec('call jsondecode("[}")')) + exc_exec('call json_decode("[}")')) end) it('fails to parse concat inside container', function() eq('Vim(call):E474: Expected comma before list item: []]', - exc_exec('call jsondecode("[[][]]")')) + exc_exec('call json_decode("[[][]]")')) eq('Vim(call):E474: Expected comma before list item: {}]', - exc_exec('call jsondecode("[{}{}]")')) + exc_exec('call json_decode("[{}{}]")')) eq('Vim(call):E474: Expected comma before list item: ]', - exc_exec('call jsondecode("[1 2]")')) + exc_exec('call json_decode("[1 2]")')) eq('Vim(call):E474: Expected comma before dictionary key: ": 4}', - exc_exec('call jsondecode("{\\"1\\": 2 \\"3\\": 4}")')) + exc_exec('call json_decode("{\\"1\\": 2 \\"3\\": 4}")')) eq('Vim(call):E474: Expected colon before dictionary value: , "3" 4}', - exc_exec('call jsondecode("{\\"1\\" 2, \\"3\\" 4}")')) + exc_exec('call json_decode("{\\"1\\" 2, \\"3\\" 4}")')) end) it('fails to parse containers with leading comma or colon', function() eq('Vim(call):E474: Leading comma: ,}', - exc_exec('call jsondecode("{,}")')) + exc_exec('call json_decode("{,}")')) eq('Vim(call):E474: Leading comma: ,]', - exc_exec('call jsondecode("[,]")')) + exc_exec('call json_decode("[,]")')) eq('Vim(call):E474: Using colon not in dictionary: :]', - exc_exec('call jsondecode("[:]")')) + exc_exec('call json_decode("[:]")')) eq('Vim(call):E474: Unexpected colon: :}', - exc_exec('call jsondecode("{:}")')) + exc_exec('call json_decode("{:}")')) end) it('fails to parse containers with trailing comma', function() eq('Vim(call):E474: Trailing comma: ]', - exc_exec('call jsondecode("[1,]")')) + exc_exec('call json_decode("[1,]")')) eq('Vim(call):E474: Trailing comma: }', - exc_exec('call jsondecode("{\\"1\\": 2,}")')) + exc_exec('call json_decode("{\\"1\\": 2,}")')) end) it('fails to parse dictionaries with missing value', function() eq('Vim(call):E474: Expected value after colon: }', - exc_exec('call jsondecode("{\\"1\\":}")')) + exc_exec('call json_decode("{\\"1\\":}")')) eq('Vim(call):E474: Expected value: }', - exc_exec('call jsondecode("{\\"1\\"}")')) + exc_exec('call json_decode("{\\"1\\"}")')) end) it('fails to parse containers with two commas or colons', function() eq('Vim(call):E474: Duplicate comma: , "2": 2}', - exc_exec('call jsondecode("{\\"1\\": 1,, \\"2\\": 2}")')) + exc_exec('call json_decode("{\\"1\\": 1,, \\"2\\": 2}")')) eq('Vim(call):E474: Duplicate comma: , "2", 2]', - exc_exec('call jsondecode("[\\"1\\", 1,, \\"2\\", 2]")')) + exc_exec('call json_decode("[\\"1\\", 1,, \\"2\\", 2]")')) eq('Vim(call):E474: Duplicate colon: : 2}', - exc_exec('call jsondecode("{\\"1\\": 1, \\"2\\":: 2}")')) + exc_exec('call json_decode("{\\"1\\": 1, \\"2\\":: 2}")')) eq('Vim(call):E474: Comma after colon: , 2}', - exc_exec('call jsondecode("{\\"1\\": 1, \\"2\\":, 2}")')) + exc_exec('call json_decode("{\\"1\\": 1, \\"2\\":, 2}")')) eq('Vim(call):E474: Unexpected colon: : "2": 2}', - exc_exec('call jsondecode("{\\"1\\": 1,: \\"2\\": 2}")')) + exc_exec('call json_decode("{\\"1\\": 1,: \\"2\\": 2}")')) eq('Vim(call):E474: Unexpected colon: :, "2": 2}', - exc_exec('call jsondecode("{\\"1\\": 1:, \\"2\\": 2}")')) + exc_exec('call json_decode("{\\"1\\": 1:, \\"2\\": 2}")')) end) it('fails to parse concat of two values', function() eq('Vim(call):E474: Trailing characters: []', - exc_exec('call jsondecode("{}[]")')) + exc_exec('call json_decode("{}[]")')) end) it('parses containers', function() - eq({1}, funcs.jsondecode('[1]')) - eq({nil, 1}, funcs.jsondecode('[null, 1]')) - eq({['1']=2}, funcs.jsondecode('{"1": 2}')) + eq({1}, funcs.json_decode('[1]')) + eq({nil, 1}, funcs.json_decode('[null, 1]')) + eq({['1']=2}, funcs.json_decode('{"1": 2}')) eq({['1']=2, ['3']={{['4']={['5']={{}, 1}}}}}, - funcs.jsondecode('{"1": 2, "3": [{"4": {"5": [[], 1]}}]}')) + funcs.json_decode('{"1": 2, "3": [{"4": {"5": [[], 1]}}]}')) end) it('fails to parse incomplete strings', function() eq('Vim(call):E474: Expected string end: \t"', - exc_exec('call jsondecode("\\t\\"")')) + exc_exec('call json_decode("\\t\\"")')) eq('Vim(call):E474: Expected string end: \t"abc', - exc_exec('call jsondecode("\\t\\"abc")')) + exc_exec('call json_decode("\\t\\"abc")')) eq('Vim(call):E474: Unfinished escape sequence: \t"abc\\', - exc_exec('call jsondecode("\\t\\"abc\\\\")')) + exc_exec('call json_decode("\\t\\"abc\\\\")')) eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u', - exc_exec('call jsondecode("\\t\\"abc\\\\u")')) + exc_exec('call json_decode("\\t\\"abc\\\\u")')) eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u0', - exc_exec('call jsondecode("\\t\\"abc\\\\u0")')) + exc_exec('call json_decode("\\t\\"abc\\\\u0")')) eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u00', - exc_exec('call jsondecode("\\t\\"abc\\\\u00")')) + exc_exec('call json_decode("\\t\\"abc\\\\u00")')) eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u000', - exc_exec('call jsondecode("\\t\\"abc\\\\u000")')) + exc_exec('call json_decode("\\t\\"abc\\\\u000")')) eq('Vim(call):E474: Expected four hex digits after \\u: \\u" ', - exc_exec('call jsondecode("\\t\\"abc\\\\u\\" ")')) + exc_exec('call json_decode("\\t\\"abc\\\\u\\" ")')) eq('Vim(call):E474: Expected four hex digits after \\u: \\u0" ', - exc_exec('call jsondecode("\\t\\"abc\\\\u0\\" ")')) + exc_exec('call json_decode("\\t\\"abc\\\\u0\\" ")')) eq('Vim(call):E474: Expected four hex digits after \\u: \\u00" ', - exc_exec('call jsondecode("\\t\\"abc\\\\u00\\" ")')) + exc_exec('call json_decode("\\t\\"abc\\\\u00\\" ")')) eq('Vim(call):E474: Expected four hex digits after \\u: \\u000" ', - exc_exec('call jsondecode("\\t\\"abc\\\\u000\\" ")')) + exc_exec('call json_decode("\\t\\"abc\\\\u000\\" ")')) eq('Vim(call):E474: Expected string end: \t"abc\\u0000', - exc_exec('call jsondecode("\\t\\"abc\\\\u0000")')) + exc_exec('call json_decode("\\t\\"abc\\\\u0000")')) end) it('fails to parse unknown escape sequnces', function() eq('Vim(call):E474: Unknown escape sequence: \\a"', - exc_exec('call jsondecode("\\t\\"\\\\a\\"")')) + exc_exec('call json_decode("\\t\\"\\\\a\\"")')) end) it('parses strings properly', function() - eq('\n', funcs.jsondecode('"\\n"')) - eq('', funcs.jsondecode('""')) - eq('\\/"\t\b\n\r\f', funcs.jsondecode([["\\\/\"\t\b\n\r\f"]])) - eq('/a', funcs.jsondecode([["\/a"]])) + eq('\n', funcs.json_decode('"\\n"')) + eq('', funcs.json_decode('""')) + eq('\\/"\t\b\n\r\f', funcs.json_decode([["\\\/\"\t\b\n\r\f"]])) + eq('/a', funcs.json_decode([["\/a"]])) -- Unicode characters: 2-byte, 3-byte, 4-byte eq({ '«', 'ફ', '\xF0\x90\x80\x80', - }, funcs.jsondecode({ + }, funcs.json_decode({ '[', '"«",', '"ફ",', @@ -323,74 +323,74 @@ describe('jsondecode() function', function() it('fails on strings with invalid bytes', function() eq('Vim(call):E474: Only UTF-8 strings allowed: \255"', - exc_exec('call jsondecode("\\t\\"\\xFF\\"")')) + exc_exec('call json_decode("\\t\\"\\xFF\\"")')) eq('Vim(call):E474: ASCII control characters cannot be present inside string: ', - exc_exec('call jsondecode(["\\"\\n\\""])')) + exc_exec('call json_decode(["\\"\\n\\""])')) -- 0xC2 starts 2-byte unicode character eq('Vim(call):E474: Only UTF-8 strings allowed: \194"', - exc_exec('call jsondecode("\\t\\"\\xC2\\"")')) + exc_exec('call json_decode("\\t\\"\\xC2\\"")')) -- 0xE0 0xAA starts 3-byte unicode character eq('Vim(call):E474: Only UTF-8 strings allowed: \224"', - exc_exec('call jsondecode("\\t\\"\\xE0\\"")')) + exc_exec('call json_decode("\\t\\"\\xE0\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \224\170"', - exc_exec('call jsondecode("\\t\\"\\xE0\\xAA\\"")')) + exc_exec('call json_decode("\\t\\"\\xE0\\xAA\\"")')) -- 0xF0 0x90 0x80 starts 4-byte unicode character eq('Vim(call):E474: Only UTF-8 strings allowed: \240"', - exc_exec('call jsondecode("\\t\\"\\xF0\\"")')) + exc_exec('call json_decode("\\t\\"\\xF0\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144"', - exc_exec('call jsondecode("\\t\\"\\xF0\\x90\\"")')) + exc_exec('call json_decode("\\t\\"\\xF0\\x90\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144\128"', - exc_exec('call jsondecode("\\t\\"\\xF0\\x90\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xF0\\x90\\x80\\"")')) -- 0xF9 0x80 0x80 0x80 starts 5-byte unicode character eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9"', - exc_exec('call jsondecode("\\t\\"\\xF9\\"")')) + exc_exec('call json_decode("\\t\\"\\xF9\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80"', - exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xF9\\x80\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80"', - exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80\x80"', - exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\x80\\"")')) -- 0xFC 0x90 0x80 0x80 0x80 starts 6-byte unicode character eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC"', - exc_exec('call jsondecode("\\t\\"\\xFC\\"")')) + exc_exec('call json_decode("\\t\\"\\xFC\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90"', - exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\"")')) + exc_exec('call json_decode("\\t\\"\\xFC\\x90\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80"', - exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80"', - exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\"")')) eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80\x80"', - exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\"")')) -- Specification does not allow unquoted characters above 0x10FFFF eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xF9\x80\x80\x80\x80"', - exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\x80\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\x80\\x80\\"")')) eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xFC\x90\x80\x80\x80\x80"', - exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\x80\\"")')) + exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\x80\\"")')) -- '"\xF9\x80\x80\x80\x80"', -- '"\xFC\x90\x80\x80\x80\x80"', end) it('parses surrogate pairs properly', function() - eq('\xF0\x90\x80\x80', funcs.jsondecode('"\\uD800\\uDC00"')) - eq('\xED\xA0\x80a\xED\xB0\x80', funcs.jsondecode('"\\uD800a\\uDC00"')) - eq('\xED\xA0\x80\t\xED\xB0\x80', funcs.jsondecode('"\\uD800\\t\\uDC00"')) + eq('\xF0\x90\x80\x80', funcs.json_decode('"\\uD800\\uDC00"')) + eq('\xED\xA0\x80a\xED\xB0\x80', funcs.json_decode('"\\uD800a\\uDC00"')) + eq('\xED\xA0\x80\t\xED\xB0\x80', funcs.json_decode('"\\uD800\\t\\uDC00"')) - eq('\xED\xA0\x80', funcs.jsondecode('"\\uD800"')) - eq('\xED\xA0\x80a', funcs.jsondecode('"\\uD800a"')) - eq('\xED\xA0\x80\t', funcs.jsondecode('"\\uD800\\t"')) + eq('\xED\xA0\x80', funcs.json_decode('"\\uD800"')) + eq('\xED\xA0\x80a', funcs.json_decode('"\\uD800a"')) + eq('\xED\xA0\x80\t', funcs.json_decode('"\\uD800\\t"')) - eq('\xED\xB0\x80', funcs.jsondecode('"\\uDC00"')) - eq('\xED\xB0\x80a', funcs.jsondecode('"\\uDC00a"')) - eq('\xED\xB0\x80\t', funcs.jsondecode('"\\uDC00\\t"')) + eq('\xED\xB0\x80', funcs.json_decode('"\\uDC00"')) + eq('\xED\xB0\x80a', funcs.json_decode('"\\uDC00a"')) + eq('\xED\xB0\x80\t', funcs.json_decode('"\\uDC00\\t"')) - eq('\xED\xB0\x80', funcs.jsondecode('"\\uDC00"')) - eq('a\xED\xB0\x80', funcs.jsondecode('"a\\uDC00"')) - eq('\t\xED\xB0\x80', funcs.jsondecode('"\\t\\uDC00"')) + eq('\xED\xB0\x80', funcs.json_decode('"\\uDC00"')) + eq('a\xED\xB0\x80', funcs.json_decode('"a\\uDC00"')) + eq('\t\xED\xB0\x80', funcs.json_decode('"\\t\\uDC00"')) end) local sp_decode_eq = function(expected, json) meths.set_var('__json', json) - speq(expected, 'jsondecode(g:__json)') + speq(expected, 'json_decode(g:__json)') execute('unlet! g:__json') end @@ -441,47 +441,47 @@ describe('jsondecode() function', function() it('converts strings to latin1 when &encoding is latin1', function() restart('set encoding=latin1') - eq('\xAB', funcs.jsondecode('"\\u00AB"')) + eq('\xAB', funcs.json_decode('"\\u00AB"')) sp_decode_eq({_TYPE='string', _VAL={'\n\xAB\n'}}, '"\\u0000\\u00AB\\u0000"') end) end) -describe('jsonencode() function', function() +describe('json_encode() function', function() before_each(clear) it('dumps strings', function() - eq('"Test"', funcs.jsonencode('Test')) - eq('""', funcs.jsonencode('')) - eq('"\\t"', funcs.jsonencode('\t')) - eq('"\\n"', funcs.jsonencode('\n')) - eq('"\\u001B"', funcs.jsonencode('\27')) - eq('"þÿþ"', funcs.jsonencode('þÿþ')) + eq('"Test"', funcs.json_encode('Test')) + eq('""', funcs.json_encode('')) + eq('"\\t"', funcs.json_encode('\t')) + eq('"\\n"', funcs.json_encode('\n')) + eq('"\\u001B"', funcs.json_encode('\27')) + eq('"þÿþ"', funcs.json_encode('þÿþ')) end) it('dumps numbers', function() - eq('0', funcs.jsonencode(0)) - eq('10', funcs.jsonencode(10)) - eq('-10', funcs.jsonencode(-10)) + eq('0', funcs.json_encode(0)) + eq('10', funcs.json_encode(10)) + eq('-10', funcs.json_encode(-10)) end) it('dumps floats', function() - eq('0.0', eval('jsonencode(0.0)')) - eq('10.5', funcs.jsonencode(10.5)) - eq('-10.5', funcs.jsonencode(-10.5)) - eq('-1.0e-5', funcs.jsonencode(-1e-5)) - eq('1.0e50', eval('jsonencode(1.0e50)')) + eq('0.0', eval('json_encode(0.0)')) + eq('10.5', funcs.json_encode(10.5)) + eq('-10.5', funcs.json_encode(-10.5)) + eq('-1.0e-5', funcs.json_encode(-1e-5)) + eq('1.0e50', eval('json_encode(1.0e50)')) end) it('dumps lists', function() - eq('[]', funcs.jsonencode({})) - eq('[[]]', funcs.jsonencode({{}})) - eq('[[], []]', funcs.jsonencode({{}, {}})) + eq('[]', funcs.json_encode({})) + eq('[[]]', funcs.json_encode({{}})) + eq('[[], []]', funcs.json_encode({{}, {}})) end) it('dumps dictionaries', function() - eq('{}', eval('jsonencode({})')) - eq('{"d": []}', funcs.jsonencode({d={}})) - eq('{"d": [], "e": []}', funcs.jsonencode({d={}, e={}})) + eq('{}', eval('json_encode({})')) + eq('{"d": []}', funcs.json_encode({d={}})) + eq('{"d": [], "e": []}', funcs.json_encode({d={}, e={}})) end) it('cannot dump generic mapping with generic mapping keys and values', @@ -490,148 +490,148 @@ describe('jsonencode() function', function() execute('let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}') execute('let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}') execute('call add(todump._VAL, [todumpv1, todumpv2])') - eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)')) end) it('cannot dump generic mapping with ext key', function() execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') - eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)')) end) it('cannot dump generic mapping with array key', function() execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') - eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)')) end) it('cannot dump generic mapping with UINT64_MAX key', function() execute('let todump = {"_TYPE": v:msgpack_types.integer}') execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') - eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)')) end) it('cannot dump generic mapping with floating-point key', function() execute('let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') - eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) + eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)')) end) it('can dump generic mapping with STR special key and NUL', function() execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n"]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') - eq('{"\\u0000": 1}', eval('jsonencode(todump)')) + eq('{"\\u0000": 1}', eval('json_encode(todump)')) end) it('can dump generic mapping with BIN special key and NUL', function() execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n"]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') - eq('{"\\u0000": 1}', eval('jsonencode(todump)')) + eq('{"\\u0000": 1}', eval('json_encode(todump)')) end) it('can dump STR special mapping with NUL and NL', function() execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n", ""]}') - eq('"\\u0000\\n"', eval('jsonencode(todump)')) + eq('"\\u0000\\n"', eval('json_encode(todump)')) end) it('can dump BIN special mapping with NUL and NL', function() execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n", ""]}') - eq('"\\u0000\\n"', eval('jsonencode(todump)')) + eq('"\\u0000\\n"', eval('json_encode(todump)')) end) it('cannot dump special ext mapping', function() execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}') - eq('Vim(call):E474: Unable to convert EXT string to JSON', exc_exec('call jsonencode(todump)')) + eq('Vim(call):E474: Unable to convert EXT string to JSON', exc_exec('call json_encode(todump)')) end) it('can dump special array mapping', function() execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}') - eq('[5, [""]]', eval('jsonencode(todump)')) + eq('[5, [""]]', eval('json_encode(todump)')) end) it('can dump special UINT64_MAX mapping', function() execute('let todump = {"_TYPE": v:msgpack_types.integer}') execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]') - eq('18446744073709551615', eval('jsonencode(todump)')) + eq('18446744073709551615', eval('json_encode(todump)')) end) it('can dump special INT64_MIN mapping', function() execute('let todump = {"_TYPE": v:msgpack_types.integer}') execute('let todump._VAL = [-1, 2, 0, 0]') - eq('-9223372036854775808', eval('jsonencode(todump)')) + eq('-9223372036854775808', eval('json_encode(todump)')) end) it('can dump special BOOLEAN true mapping', function() execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 1}') - eq('true', eval('jsonencode(todump)')) + eq('true', eval('json_encode(todump)')) end) it('can dump special BOOLEAN false mapping', function() execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 0}') - eq('false', eval('jsonencode(todump)')) + eq('false', eval('json_encode(todump)')) end) it('can dump special NIL mapping', function() execute('let todump = {"_TYPE": v:msgpack_types.nil, "_VAL": 0}') - eq('null', eval('jsonencode(todump)')) + eq('null', eval('json_encode(todump)')) end) it('fails to dump a function reference', function() eq('Vim(call):E474: Error while dumping encode_tv2json() argument, itself: attempt to dump function reference', - exc_exec('call jsonencode(function("tr"))')) + exc_exec('call json_encode(function("tr"))')) end) it('fails to dump a function reference in a list', function() eq('Vim(call):E474: Error while dumping encode_tv2json() argument, index 0: attempt to dump function reference', - exc_exec('call jsonencode([function("tr")])')) + exc_exec('call json_encode([function("tr")])')) end) it('fails to dump a recursive list', function() execute('let todump = [[[]]]') execute('call add(todump[0][0], todump)') eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', - exc_exec('call jsonencode(todump)')) + exc_exec('call json_encode(todump)')) end) it('fails to dump a recursive dict', function() execute('let todump = {"d": {"d": {}}}') execute('call extend(todump.d.d, {"d": todump})') eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', - exc_exec('call jsonencode([todump])')) + exc_exec('call json_encode([todump])')) end) it('can dump dict with two same dicts inside', function() execute('let inter = {}') execute('let todump = {"a": inter, "b": inter}') - eq('{"a": {}, "b": {}}', eval('jsonencode(todump)')) + eq('{"a": {}, "b": {}}', eval('json_encode(todump)')) end) it('can dump list with two same lists inside', function() execute('let inter = []') execute('let todump = [inter, inter]') - eq('[[], []]', eval('jsonencode(todump)')) + eq('[[], []]', eval('json_encode(todump)')) end) it('fails to dump a recursive list in a special dict', function() execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}') execute('call add(todump._VAL, todump)') eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', - exc_exec('call jsonencode(todump)')) + exc_exec('call json_encode(todump)')) end) it('fails to dump a recursive (val) map in a special dict', function() execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}') execute('call add(todump._VAL, ["", todump])') eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', - exc_exec('call jsonencode([todump])')) + exc_exec('call json_encode([todump])')) end) it('fails to dump a recursive (val) map in a special dict, _VAL reference', function() execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [["", []]]}') execute('call add(todump._VAL[0][1], todump._VAL)') eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', - exc_exec('call jsonencode(todump)')) + exc_exec('call json_encode(todump)')) end) it('fails to dump a recursive (val) special list in a special dict', @@ -639,35 +639,35 @@ describe('jsonencode() function', function() execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}') execute('call add(todump._VAL, ["", todump._VAL])') eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', - exc_exec('call jsonencode(todump)')) + exc_exec('call json_encode(todump)')) end) it('fails when called with no arguments', function() - eq('Vim(call):E119: Not enough arguments for function: jsonencode', - exc_exec('call jsonencode()')) + eq('Vim(call):E119: Not enough arguments for function: json_encode', + exc_exec('call json_encode()')) end) it('fails when called with two arguments', function() - eq('Vim(call):E118: Too many arguments for function: jsonencode', - exc_exec('call jsonencode(["", ""], 1)')) + eq('Vim(call):E118: Too many arguments for function: json_encode', + exc_exec('call json_encode(["", ""], 1)')) end) it('converts strings from latin1 when &encoding is latin1', function() clear('set encoding=latin1') - eq('"\\u00AB"', funcs.jsonencode('\xAB')) - eq('"\\u0000\\u00AB\\u0000"', eval('jsonencode({"_TYPE": v:msgpack_types.string, "_VAL": ["\\n\xAB\\n"]})')) + eq('"\\u00AB"', funcs.json_encode('\xAB')) + eq('"\\u0000\\u00AB\\u0000"', eval('json_encode({"_TYPE": v:msgpack_types.string, "_VAL": ["\\n\xAB\\n"]})')) end) it('ignores improper values in &isprint', function() meths.set_option('isprint', '1') eq(1, eval('"\x01" =~# "\\\\p"')) - eq('"\\u0001"', funcs.jsonencode('\x01')) + eq('"\\u0001"', funcs.json_encode('\x01')) end) it('fails when using surrogate character in a UTF-8 string', function() eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xA0\x80', - exc_exec('call jsonencode("\xED\xA0\x80")')) + exc_exec('call json_encode("\xED\xA0\x80")')) eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xAF\xBF', - exc_exec('call jsonencode("\xED\xAF\xBF")')) + exc_exec('call json_encode("\xED\xAF\xBF")')) end) end) -- cgit From 406562ac6d3863dfdaedbf40f9d4a23ca37c9ec5 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 21 Feb 2016 21:33:58 +0300 Subject: encode: Fail to dump NaN and infinity Thanks to vim/vim#654 --- test/functional/eval/json_functions_spec.lua | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 0b0403ce8e..33d177a2b6 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -472,6 +472,15 @@ describe('json_encode() function', function() eq('1.0e50', eval('json_encode(1.0e50)')) end) + it('fails to dump NaN and infinite values', function() + eq('Vim(call):E474: Unable to represent NaN value in JSON', + exc_exec('call json_encode(str2float("nan"))')) + eq('Vim(call):E474: Unable to represent infinity in JSON', + exc_exec('call json_encode(str2float("inf"))')) + eq('Vim(call):E474: Unable to represent infinity in JSON', + exc_exec('call json_encode(-str2float("inf"))')) + end) + it('dumps lists', function() eq('[]', funcs.json_encode({})) eq('[[]]', funcs.json_encode({{}})) -- cgit From 942e0b338c9bff1dfdcb59e8308160449f1f38b4 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 25 Feb 2016 17:27:23 +0300 Subject: encode: Handle incomplete surrogates like `\uSURR\uOTHR` properly --- test/functional/eval/json_functions_spec.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 33d177a2b6..d6286611d2 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -386,6 +386,8 @@ describe('json_decode() function', function() eq('\xED\xB0\x80', funcs.json_decode('"\\uDC00"')) eq('a\xED\xB0\x80', funcs.json_decode('"a\\uDC00"')) eq('\t\xED\xB0\x80', funcs.json_decode('"\\t\\uDC00"')) + + eq('\xED\xA0\x80¬', funcs.json_decode('"\\uD800\\u00AC"')) end) local sp_decode_eq = function(expected, json) -- cgit From 4a29995fe74ed95c641ef40c68d8a4223e90cccf Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Mar 2016 06:41:00 +0300 Subject: eval/decode: Rename brackets in error messages U+007D is officially RIGHT CURLY BRACKET. U+005D is officially RIGHT SQUARE BRACKET. --- test/functional/eval/json_functions_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index d6286611d2..131b177622 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -198,9 +198,9 @@ describe('json_decode() function', function() it('fails to parse containers which are closed by different brackets', function() - eq('Vim(call):E474: Closing dictionary with bracket: ]', + eq('Vim(call):E474: Closing dictionary with square bracket: ]', exc_exec('call json_decode("{]")')) - eq('Vim(call):E474: Closing list with figure brace: }', + eq('Vim(call):E474: Closing list with curly bracket: }', exc_exec('call json_decode("[}")')) end) -- cgit From b725f6b4287d800f22bce32f32022ad07aa2610e Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Mar 2016 07:18:29 +0300 Subject: functests: Make sure that json functions are tested with C messages --- test/functional/eval/json_functions_spec.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 131b177622..59dbb804e4 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -10,6 +10,7 @@ local exc_exec = helpers.exc_exec describe('json_decode() function', function() local restart = function(cmd) clear(cmd) + execute('language C') execute([[ function Eq(exp, act) let act = a:act @@ -449,7 +450,10 @@ describe('json_decode() function', function() end) describe('json_encode() function', function() - before_each(clear) + before_each(function() + clear() + execute('language C') + end) it('dumps strings', function() eq('"Test"', funcs.json_encode('Test')) -- cgit From 394830631f130ad646f23358bf7863e7a37c6d78 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Mar 2016 07:27:14 +0300 Subject: eval/decode: Make sure that U+00C3 is parsed correctly --- test/functional/eval/json_functions_spec.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 59dbb804e4..7ec3882a58 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -447,6 +447,10 @@ describe('json_decode() function', function() eq('\xAB', funcs.json_decode('"\\u00AB"')) sp_decode_eq({_TYPE='string', _VAL={'\n\xAB\n'}}, '"\\u0000\\u00AB\\u0000"') end) + + it('parses U+00C3 correctly', function() + eq('\xC3\x83', funcs.json_decode('"\xC3\x83"')) + end) end) describe('json_encode() function', function() -- cgit From 224d7df6309319cfa1f98aad3aa93c5b63ee4145 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Mar 2016 07:37:21 +0300 Subject: eval/decode: Make sure that blank input does not crash Neovim --- test/functional/eval/json_functions_spec.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 7ec3882a58..aa36b62757 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -451,6 +451,19 @@ describe('json_decode() function', function() it('parses U+00C3 correctly', function() eq('\xC3\x83', funcs.json_decode('"\xC3\x83"')) end) + + it('fails to parse empty string', function() + eq('Vim(call):E474: Attempt to decode a blank string', + exc_exec('call json_decode("")')) + eq('Vim(call):E474: Attempt to decode a blank string', + exc_exec('call json_decode(" ")')) + eq('Vim(call):E474: Attempt to decode a blank string', + exc_exec('call json_decode("\\t")')) + eq('Vim(call):E474: Attempt to decode a blank string', + exc_exec('call json_decode("\\n")')) + eq('Vim(call):E474: Attempt to decode a blank string', + exc_exec('call json_decode(" \\t\\n \\n\\t\\t \\n\\t\\n \\n \\t\\n\\t ")')) + end) end) describe('json_encode() function', function() -- cgit From eb806c96205ff776d9cd5df82da72c14e030f6d6 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Mar 2016 08:54:39 +0300 Subject: eval/decode: Make sure that error messages do not cause overflow --- test/functional/eval/json_functions_spec.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index aa36b62757..58030fca72 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -448,6 +448,12 @@ describe('json_decode() function', function() sp_decode_eq({_TYPE='string', _VAL={'\n\xAB\n'}}, '"\\u0000\\u00AB\\u0000"') end) + it('fails to convert string to latin1 if it is impossible', function() + restart('set encoding=latin1') + eq('Vim(call):E474: Failed to convert string "ꯍ" from UTF-8', + exc_exec('call json_decode(\'"\\uABCD"\')')) + end) + it('parses U+00C3 correctly', function() eq('\xC3\x83', funcs.json_decode('"\xC3\x83"')) end) -- cgit From 032ac502ff1378757d9ba56e5760d362570e48e4 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Mar 2016 08:59:03 +0300 Subject: eval/decode: Do not loose high surrogates followed by high surrogates --- test/functional/eval/json_functions_spec.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 58030fca72..6379e7fed8 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -389,6 +389,8 @@ describe('json_decode() function', function() eq('\t\xED\xB0\x80', funcs.json_decode('"\\t\\uDC00"')) eq('\xED\xA0\x80¬', funcs.json_decode('"\\uD800\\u00AC"')) + + eq('\xED\xA0\x80\xED\xA0\x80', funcs.json_decode('"\\uD800\\uD800"')) end) local sp_decode_eq = function(expected, json) -- cgit From 9c543f2e246469adec1daddf156f4bcabe30931a Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Mar 2016 09:09:39 +0300 Subject: eval/decode: Reject more numbers, accept 1e5 --- test/functional/eval/json_functions_spec.lua | 43 ++++++++++++++++------------ 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 6379e7fed8..0140ad79aa 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -121,19 +121,32 @@ describe('json_decode() function', function() eq(-100000, funcs.json_decode(' -100000 ')) end) - it('fails to parse +numbers', function() + it('fails to parse +numbers and .number', function() eq('Vim(call):E474: Unidentified byte: +1000', exc_exec('call json_decode("+1000")')) + eq('Vim(call):E474: Unidentified byte: .1000', + exc_exec('call json_decode(".1000")')) end) - it('fails to parse negative numbers with space after -', function() - eq('Vim(call):E474: Missing number after minus sign: - 1000', - exc_exec('call json_decode("- 1000")')) - end) - - it('fails to parse -', function() + it('fails to parse incomplete numbers', function() + eq('Vim(call):E474: Missing number after minus sign: -.1', + exc_exec('call json_decode("-.1")')) eq('Vim(call):E474: Missing number after minus sign: -', exc_exec('call json_decode("-")')) + eq('Vim(call):E474: Missing number after decimal dot: -1.', + exc_exec('call json_decode("-1.")')) + eq('Vim(call):E474: Missing number after decimal dot: 0.', + exc_exec('call json_decode("0.")')) + eq('Vim(call):E474: Missing exponent: 0.0e', + exc_exec('call json_decode("0.0e")')) + eq('Vim(call):E474: Missing exponent: 0.0e+', + exc_exec('call json_decode("0.0e+")')) + eq('Vim(call):E474: Missing exponent: 0.0e-', + exc_exec('call json_decode("0.0e-")')) + eq('Vim(call):E474: Missing exponent: 0.0e-', + exc_exec('call json_decode("0.0e-")')) + eq('Vim(call):E474: Missing number after decimal dot: 1.e5', + exc_exec('call json_decode("1.e5")')) end) it('parses floating-point numbers', function() @@ -145,20 +158,12 @@ describe('json_decode() function', function() eq(100000.5e50, funcs.json_decode('100000.5e+50')) eq(-100000.5e-50, funcs.json_decode('-100000.5e-50')) eq(100000.5e-50, funcs.json_decode('100000.5e-50')) + eq(100000e-50, funcs.json_decode('100000e-50')) end) - it('fails to parse incomplete floating-point numbers', function() - eq('Vim(call):E474: Missing number after decimal dot: 0.', - exc_exec('call json_decode("0.")')) - eq('Vim(call):E474: Missing exponent: 0.0e', - exc_exec('call json_decode("0.0e")')) - eq('Vim(call):E474: Missing exponent: 0.0e+', - exc_exec('call json_decode("0.0e+")')) - eq('Vim(call):E474: Missing exponent: 0.0e-', - exc_exec('call json_decode("0.0e-")')) - end) - - it('fails to parse floating-point numbers with spaces inside', function() + it('fails to parse numbers with spaces inside', function() + eq('Vim(call):E474: Missing number after minus sign: - 1000', + exc_exec('call json_decode("- 1000")')) eq('Vim(call):E474: Missing number after decimal dot: 0. ', exc_exec('call json_decode("0. ")')) eq('Vim(call):E474: Missing number after decimal dot: 0. 0', -- cgit From 515fea1ef09e3debee9e226f34d3e62e47e8a08d Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 9 Mar 2016 02:08:53 +0300 Subject: eval/decode: Reject even more numbers Rejects leading zeroes and numbers like 1.e+5 (decimal dot with missing number with signed exponent). --- test/functional/eval/json_functions_spec.lua | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 0140ad79aa..8438620109 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -119,6 +119,8 @@ describe('json_decode() function', function() eq(-100000, funcs.json_decode('-100000')) eq(100000, funcs.json_decode(' 100000 ')) eq(-100000, funcs.json_decode(' -100000 ')) + eq(0, funcs.json_decode('0')) + eq(0, funcs.json_decode('-0')) end) it('fails to parse +numbers and .number', function() @@ -128,6 +130,17 @@ describe('json_decode() function', function() exc_exec('call json_decode(".1000")')) end) + it('fails to parse numbers with leading zeroes', function() + eq('Vim(call):E474: Leading zeroes are not allowed: 00.1', + exc_exec('call json_decode("00.1")')) + eq('Vim(call):E474: Leading zeroes are not allowed: 01', + exc_exec('call json_decode("01")')) + eq('Vim(call):E474: Leading zeroes are not allowed: -01', + exc_exec('call json_decode("-01")')) + eq('Vim(call):E474: Leading zeroes are not allowed: -001.0', + exc_exec('call json_decode("-001.0")')) + end) + it('fails to parse incomplete numbers', function() eq('Vim(call):E474: Missing number after minus sign: -.1', exc_exec('call json_decode("-.1")')) @@ -147,6 +160,10 @@ describe('json_decode() function', function() exc_exec('call json_decode("0.0e-")')) eq('Vim(call):E474: Missing number after decimal dot: 1.e5', exc_exec('call json_decode("1.e5")')) + eq('Vim(call):E474: Missing number after decimal dot: 1.e+5', + exc_exec('call json_decode("1.e+5")')) + eq('Vim(call):E474: Missing number after decimal dot: 1.e+', + exc_exec('call json_decode("1.e+")')) end) it('parses floating-point numbers', function() @@ -159,6 +176,27 @@ describe('json_decode() function', function() eq(-100000.5e-50, funcs.json_decode('-100000.5e-50')) eq(100000.5e-50, funcs.json_decode('100000.5e-50')) eq(100000e-50, funcs.json_decode('100000e-50')) + eq(0.5, funcs.json_decode('0.5')) + eq(0.005, funcs.json_decode('0.005')) + eq(0.005, funcs.json_decode('0.00500')) + eq(0.5, funcs.json_decode('0.00500e+002')) + eq(0.00005, funcs.json_decode('0.00500e-002')) + + eq(-0.0, funcs.json_decode('-0.0')) + eq(-0.0, funcs.json_decode('-0.0e0')) + eq(-0.0, funcs.json_decode('-0.0e+0')) + eq(-0.0, funcs.json_decode('-0.0e-0')) + eq(-0.0, funcs.json_decode('-0e-0')) + eq(-0.0, funcs.json_decode('-0e-2')) + eq(-0.0, funcs.json_decode('-0e+2')) + + eq(0.0, funcs.json_decode('0.0')) + eq(0.0, funcs.json_decode('0.0e0')) + eq(0.0, funcs.json_decode('0.0e+0')) + eq(0.0, funcs.json_decode('0.0e-0')) + eq(0.0, funcs.json_decode('0e-0')) + eq(0.0, funcs.json_decode('0e-2')) + eq(0.0, funcs.json_decode('0e+2')) end) it('fails to parse numbers with spaces inside', function() -- cgit From c129f6cfafc77d3f6e22b2ac11b5c8f2cec033d3 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 12 Mar 2016 13:47:34 +0300 Subject: eval/decode: Accept `\r` as space character --- test/functional/eval/json_functions_spec.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 8438620109..8483152dbf 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -515,6 +515,12 @@ describe('json_decode() function', function() eq('Vim(call):E474: Attempt to decode a blank string', exc_exec('call json_decode(" \\t\\n \\n\\t\\t \\n\\t\\n \\n \\t\\n\\t ")')) end) + + it('accepts all spaces in every position where space may be put', function() + local s = ' \t\n\r \t\r\n \n\t\r \n\r\t \r\t\n \r\n\t\t \n\r\t \r\n\t\n \r\t\n\r \t\r \n\t\r\n \n \t\r\n \r\t\n\t \r\n\t\r \n\r \t\n\r\t \r \t\n\r \n\t\r\t \n\r\t\n \r\n \t\r\n\t' + local str = ('%s{%s"key"%s:%s[%s"val"%s,%s"val2"%s]%s,%s"key2"%s:%s1%s}%s'):gsub('%%s', s) + eq({key={'val', 'val2'}, key2=1}, funcs.json_decode(str)) + end) end) describe('json_encode() function', function() -- cgit From 4f8b6864350c3aac5427103e27c856d1782b1be1 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 12 Mar 2016 20:54:19 +0300 Subject: documentation,functests: State that UTF-8-only support is intentional --- test/functional/eval/json_functions_spec.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 8483152dbf..bed9d668fa 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -521,6 +521,14 @@ describe('json_decode() function', function() local str = ('%s{%s"key"%s:%s[%s"val"%s,%s"val2"%s]%s,%s"key2"%s:%s1%s}%s'):gsub('%%s', s) eq({key={'val', 'val2'}, key2=1}, funcs.json_decode(str)) end) + + it('always treats input as UTF-8', function() + -- When &encoding is latin1 string "«" is U+00C2 U+00AB U+00C2: «Â. So if + -- '"«"' was parsed as latin1 json_decode would return three characters, and + -- only one U+00AB when this string is parsed as latin1. + restart('set encoding=latin1') + eq(('%c'):format(0xAB), funcs.json_decode('"«"')) + end) end) describe('json_encode() function', function() -- cgit From af7ff808c73110f04aadaaab72eac6307dae0cc2 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 20 Mar 2016 19:55:59 +0300 Subject: eval: Fix overflow in error message in f_json_decode --- test/functional/eval/json_functions_spec.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index bed9d668fa..091b6f5457 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -6,6 +6,7 @@ local eq = helpers.eq local eval = helpers.eval local execute = helpers.execute local exc_exec = helpers.exc_exec +local redir_exec = helpers.redir_exec describe('json_decode() function', function() local restart = function(cmd) @@ -529,6 +530,13 @@ describe('json_decode() function', function() restart('set encoding=latin1') eq(('%c'):format(0xAB), funcs.json_decode('"«"')) end) + + it('does not overflow when writing error message about decoding ["", ""]', + function() + eq('\nE474: Attempt to decode a blank string' + .. '\nE474: Failed to parse \n', + redir_exec('call json_decode(["", ""])')) + end) end) describe('json_encode() function', function() -- cgit From 9af400f97916851ecd86975118faea2a8c68598f Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 20 Mar 2016 20:03:12 +0300 Subject: eval: Treat [] and [""] as any other empty string --- test/functional/eval/json_functions_spec.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 091b6f5457..0b1862fa8b 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -507,6 +507,10 @@ describe('json_decode() function', function() it('fails to parse empty string', function() eq('Vim(call):E474: Attempt to decode a blank string', exc_exec('call json_decode("")')) + eq('Vim(call):E474: Attempt to decode a blank string', + exc_exec('call json_decode([])')) + eq('Vim(call):E474: Attempt to decode a blank string', + exc_exec('call json_decode([""])')) eq('Vim(call):E474: Attempt to decode a blank string', exc_exec('call json_decode(" ")')) eq('Vim(call):E474: Attempt to decode a blank string', -- cgit From fd92e648ac206340752c420ad639f2a6dab2a579 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 20 Mar 2016 23:24:53 +0300 Subject: eval/encode: Dump FF character correctly --- test/functional/eval/json_functions_spec.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 0b1862fa8b..bdd3306993 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -779,4 +779,9 @@ describe('json_encode() function', function() eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xAF\xBF', exc_exec('call json_encode("\xED\xAF\xBF")')) end) + + it('dumps control characters as expected', function() + eq([["\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000B\f\r\u000E\u000F\u0010\u0011\u0012\u0013"]], + eval('json_encode({"_TYPE": v:msgpack_types.string, "_VAL": ["\n\1\2\3\4\5\6\7\8\9", "\11\12\13\14\15\16\17\18\19"]})')) + end) end) -- cgit From 3e435df42cd1f8237bd11a4b08407a9ec3f81dba Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 21 Mar 2016 00:19:41 +0300 Subject: functests: Replace \xXX escapes with \DDD in lua code --- test/functional/eval/json_functions_spec.lua | 80 ++++++++++++++-------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index bdd3306993..b788c2cf07 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -356,12 +356,12 @@ describe('json_decode() function', function() eq({ '«', 'ફ', - '\xF0\x90\x80\x80', + '\240\144\128\128', }, funcs.json_decode({ '[', '"«",', '"ફ",', - '"\xF0\x90\x80\x80"', + '"\240\144\128\128"', ']', })) end) @@ -387,54 +387,54 @@ describe('json_decode() function', function() eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144\128"', exc_exec('call json_decode("\\t\\"\\xF0\\x90\\x80\\"")')) -- 0xF9 0x80 0x80 0x80 starts 5-byte unicode character - eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \249"', exc_exec('call json_decode("\\t\\"\\xF9\\"")')) - eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \249\128"', exc_exec('call json_decode("\\t\\"\\xF9\\x80\\"")')) - eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \249\128\128"', exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\"")')) - eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80\x80"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \249\128\128\128"', exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\x80\\"")')) -- 0xFC 0x90 0x80 0x80 0x80 starts 6-byte unicode character - eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \252"', exc_exec('call json_decode("\\t\\"\\xFC\\"")')) - eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \252\144"', exc_exec('call json_decode("\\t\\"\\xFC\\x90\\"")')) - eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \252\144\128"', exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\"")')) - eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \252\144\128\128"', exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\"")')) - eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80\x80"', + eq('Vim(call):E474: Only UTF-8 strings allowed: \252\144\128\128\128"', exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\"")')) -- Specification does not allow unquoted characters above 0x10FFFF - eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xF9\x80\x80\x80\x80"', + eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \249\128\128\128\128"', exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\x80\\x80\\"")')) - eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xFC\x90\x80\x80\x80\x80"', + eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \252\144\128\128\128\128"', exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\x80\\"")')) - -- '"\xF9\x80\x80\x80\x80"', - -- '"\xFC\x90\x80\x80\x80\x80"', + -- '"\249\128\128\128\128"', + -- '"\252\144\128\128\128\128"', end) it('parses surrogate pairs properly', function() - eq('\xF0\x90\x80\x80', funcs.json_decode('"\\uD800\\uDC00"')) - eq('\xED\xA0\x80a\xED\xB0\x80', funcs.json_decode('"\\uD800a\\uDC00"')) - eq('\xED\xA0\x80\t\xED\xB0\x80', funcs.json_decode('"\\uD800\\t\\uDC00"')) + eq('\240\144\128\128', funcs.json_decode('"\\uD800\\uDC00"')) + eq('\237\160\128a\237\176\128', funcs.json_decode('"\\uD800a\\uDC00"')) + eq('\237\160\128\t\237\176\128', funcs.json_decode('"\\uD800\\t\\uDC00"')) - eq('\xED\xA0\x80', funcs.json_decode('"\\uD800"')) - eq('\xED\xA0\x80a', funcs.json_decode('"\\uD800a"')) - eq('\xED\xA0\x80\t', funcs.json_decode('"\\uD800\\t"')) + eq('\237\160\128', funcs.json_decode('"\\uD800"')) + eq('\237\160\128a', funcs.json_decode('"\\uD800a"')) + eq('\237\160\128\t', funcs.json_decode('"\\uD800\\t"')) - eq('\xED\xB0\x80', funcs.json_decode('"\\uDC00"')) - eq('\xED\xB0\x80a', funcs.json_decode('"\\uDC00a"')) - eq('\xED\xB0\x80\t', funcs.json_decode('"\\uDC00\\t"')) + eq('\237\176\128', funcs.json_decode('"\\uDC00"')) + eq('\237\176\128a', funcs.json_decode('"\\uDC00a"')) + eq('\237\176\128\t', funcs.json_decode('"\\uDC00\\t"')) - eq('\xED\xB0\x80', funcs.json_decode('"\\uDC00"')) - eq('a\xED\xB0\x80', funcs.json_decode('"a\\uDC00"')) - eq('\t\xED\xB0\x80', funcs.json_decode('"\\t\\uDC00"')) + eq('\237\176\128', funcs.json_decode('"\\uDC00"')) + eq('a\237\176\128', funcs.json_decode('"a\\uDC00"')) + eq('\t\237\176\128', funcs.json_decode('"\\t\\uDC00"')) - eq('\xED\xA0\x80¬', funcs.json_decode('"\\uD800\\u00AC"')) + eq('\237\160\128¬', funcs.json_decode('"\\uD800\\u00AC"')) - eq('\xED\xA0\x80\xED\xA0\x80', funcs.json_decode('"\\uD800\\uD800"')) + eq('\237\160\128\237\160\128', funcs.json_decode('"\\uD800\\uD800"')) end) local sp_decode_eq = function(expected, json) @@ -490,8 +490,8 @@ describe('json_decode() function', function() it('converts strings to latin1 when &encoding is latin1', function() restart('set encoding=latin1') - eq('\xAB', funcs.json_decode('"\\u00AB"')) - sp_decode_eq({_TYPE='string', _VAL={'\n\xAB\n'}}, '"\\u0000\\u00AB\\u0000"') + eq('\171', funcs.json_decode('"\\u00AB"')) + sp_decode_eq({_TYPE='string', _VAL={'\n\171\n'}}, '"\\u0000\\u00AB\\u0000"') end) it('fails to convert string to latin1 if it is impossible', function() @@ -501,7 +501,7 @@ describe('json_decode() function', function() end) it('parses U+00C3 correctly', function() - eq('\xC3\x83', funcs.json_decode('"\xC3\x83"')) + eq('\195\131', funcs.json_decode('"\195\131"')) end) it('fails to parse empty string', function() @@ -763,21 +763,21 @@ describe('json_encode() function', function() it('converts strings from latin1 when &encoding is latin1', function() clear('set encoding=latin1') - eq('"\\u00AB"', funcs.json_encode('\xAB')) - eq('"\\u0000\\u00AB\\u0000"', eval('json_encode({"_TYPE": v:msgpack_types.string, "_VAL": ["\\n\xAB\\n"]})')) + eq('"\\u00AB"', funcs.json_encode('\171')) + eq('"\\u0000\\u00AB\\u0000"', eval('json_encode({"_TYPE": v:msgpack_types.string, "_VAL": ["\\n\171\\n"]})')) end) it('ignores improper values in &isprint', function() meths.set_option('isprint', '1') - eq(1, eval('"\x01" =~# "\\\\p"')) - eq('"\\u0001"', funcs.json_encode('\x01')) + eq(1, eval('"\1" =~# "\\\\p"')) + eq('"\\u0001"', funcs.json_encode('\1')) end) it('fails when using surrogate character in a UTF-8 string', function() - eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xA0\x80', - exc_exec('call json_encode("\xED\xA0\x80")')) - eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xAF\xBF', - exc_exec('call json_encode("\xED\xAF\xBF")')) + eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \237\160\128', + exc_exec('call json_encode("\237\160\128")')) + eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \237\175\191', + exc_exec('call json_encode("\237\175\191")')) end) it('dumps control characters as expected', function() -- cgit From bda0165514a582978c2da672b528562df78a2d1a Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 4 Apr 2016 04:53:07 +0300 Subject: eval/encode: Make sure that encoder can encode NULL variables Adds two undocumented v: variables: _null_list and _null_dict because I do not know a reproducible way to get such lists (though I think I heard about this) and dictionaries (do not remember hearing about them). NULL strings are obtained using $XXX_UNEXISTENT_VAR_XXX. Fixes crash in json_encode($XXX_UNEXISTENT_VAR_XXX). Other added tests worked fine before this commit. --- test/functional/eval/json_functions_spec.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index b788c2cf07..0a7c4cf669 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -784,4 +784,16 @@ describe('json_encode() function', function() eq([["\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000B\f\r\u000E\u000F\u0010\u0011\u0012\u0013"]], eval('json_encode({"_TYPE": v:msgpack_types.string, "_VAL": ["\n\1\2\3\4\5\6\7\8\9", "\11\12\13\14\15\16\17\18\19"]})')) end) + + it('can dump NULL string', function() + eq('""', eval('json_encode($XXX_UNEXISTENT_VAR_XXX)')) + end) + + it('can dump NULL list', function() + eq('[]', eval('json_encode(v:_null_list)')) + end) + + it('can dump NULL dictionary', function() + eq('{}', eval('json_encode(v:_null_dict)')) + end) end) -- cgit From a64114eba017c0db3d1849186c9c54fb09308761 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 18 Apr 2016 01:37:21 +0300 Subject: functests: Make json_functions_spec use new NIL where appropriate --- test/functional/eval/json_functions_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/functional/eval/json_functions_spec.lua') diff --git a/test/functional/eval/json_functions_spec.lua b/test/functional/eval/json_functions_spec.lua index 0a7c4cf669..1cece78ce1 100644 --- a/test/functional/eval/json_functions_spec.lua +++ b/test/functional/eval/json_functions_spec.lua @@ -79,7 +79,7 @@ describe('json_decode() function', function() end) it('parses null, true, false', function() - eq(nil, funcs.json_decode('null')) + eq(NIL, funcs.json_decode('null')) eq(true, funcs.json_decode('true')) eq(false, funcs.json_decode('false')) end) @@ -309,7 +309,7 @@ describe('json_decode() function', function() it('parses containers', function() eq({1}, funcs.json_decode('[1]')) - eq({nil, 1}, funcs.json_decode('[null, 1]')) + eq({NIL, 1}, funcs.json_decode('[null, 1]')) eq({['1']=2}, funcs.json_decode('{"1": 2}')) eq({['1']=2, ['3']={{['4']={['5']={{}, 1}}}}}, funcs.json_decode('{"1": 2, "3": [{"4": {"5": [[], 1]}}]}')) -- cgit