From 95cbf2012ef273b13a494a2c66cef61c334d442f Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Thu, 2 Feb 2012 00:33:58 +1030 Subject: Add cjson.safe module to suppress exceptions Add cjson.safe module to suppress exceptions during JSON conversions. --- tests/test.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'tests') diff --git a/tests/test.lua b/tests/test.lua index ac6419a..96b47ff 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -7,6 +7,7 @@ -- Note: The output of this script is easier to read with "less -S" local json = require "cjson" +local json_safe = require "cjson.safe" local util = require "cjson.util" local function gen_raw_octets() @@ -386,6 +387,19 @@ local cjson_tests = { { "Check encode_sparse_array()", function (...) return json.encode_sparse_array(...) end, { }, true, { false, 2, 10 } }, + + { "Encode (safe) simple value", + json_safe.encode, { true }, + true, { "true" } }, + { "Encode (safe) argument validation [throw error]", + json_safe.encode, { "arg1", "arg2" }, + false, { "bad argument #1 to '?' (expected 1 argument)" } }, + { "Decode (safe) error generation", + json_safe.decode, { "Oops" }, + true, { nil, "Expected value but found invalid token at character 1" } }, + { "Decode (safe) error generation after new()", + function(...) return json_safe.new().decode(...) end, { "Oops" }, + true, { nil, "Expected value but found invalid token at character 1" } }, } print(("==> Testing Lua CJSON version %s\n"):format(json._VERSION)) -- cgit v1.2.3-55-g6feb