diff options
| author | Mark Pulford <mark@kyne.com.au> | 2011-12-30 17:48:12 +1030 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2011-12-30 17:48:12 +1030 |
| commit | 85bf3b798f6d52c374c35f7fbe47df132891d3b2 (patch) | |
| tree | 39b0da8e6d6536cdd90038547a985559102962de /tests | |
| parent | 2416b145073211b840781da6abf4b6d97f4657a6 (diff) | |
| download | lua-cjson-85bf3b798f6d52c374c35f7fbe47df132891d3b2.tar.gz lua-cjson-85bf3b798f6d52c374c35f7fbe47df132891d3b2.tar.bz2 lua-cjson-85bf3b798f6d52c374c35f7fbe47df132891d3b2.zip | |
Add support for Lua 5.2 and cjson.new
Update all Lua scripts to use new module init style everywhere:
local json = require "cjson"
Lua CJSON does not register a global table under Lua 5.2. The global
table can be disabled under Lua 5.1 with DISABLE_CJSON_GLOBAL.
Other changes:
- Store CJSON configuration as an upvalue for each function.
- Add "cjson.new" function to create another module table with a
separate configuration.
- Add _NAME and _VERSION variables.
Diffstat (limited to 'tests')
| -rwxr-xr-x | tests/bench.lua | 2 | ||||
| -rw-r--r-- | tests/common.lua | 6 | ||||
| -rwxr-xr-x | tests/decode.lua | 4 | ||||
| -rwxr-xr-x | tests/encode.lua | 4 | ||||
| -rwxr-xr-x | tests/test.lua | 16 |
5 files changed, 16 insertions, 16 deletions
diff --git a/tests/bench.lua b/tests/bench.lua index fdd0bb0..c81213d 100755 --- a/tests/bench.lua +++ b/tests/bench.lua | |||
| @@ -72,7 +72,7 @@ function bench_file(filename) | |||
| 72 | return benchmark(tests, 0.1, 5) | 72 | return benchmark(tests, 0.1, 5) |
| 73 | end | 73 | end |
| 74 | 74 | ||
| 75 | cjson.encode_keep_buffer(true) | 75 | json.encode_keep_buffer(true) |
| 76 | 76 | ||
| 77 | for i = 1, #arg do | 77 | for i = 1, #arg do |
| 78 | local results = bench_file(arg[i]) | 78 | local results = bench_file(arg[i]) |
diff --git a/tests/common.lua b/tests/common.lua index f3dc6f7..7472a10 100644 --- a/tests/common.lua +++ b/tests/common.lua | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | require "cjson" | 1 | local json = require "cjson" |
| 2 | 2 | ||
| 3 | -- Misc routines to assist with CJSON testing | 3 | -- Misc routines to assist with CJSON testing |
| 4 | -- | 4 | -- |
| @@ -77,8 +77,8 @@ function serialise_value(value, indent, depth) | |||
| 77 | if indent == nil then indent = "" end | 77 | if indent == nil then indent = "" end |
| 78 | if depth == nil then depth = 0 end | 78 | if depth == nil then depth = 0 end |
| 79 | 79 | ||
| 80 | if value == cjson.null then | 80 | if value == json.null then |
| 81 | return "cjson.null" | 81 | return "json.null" |
| 82 | elseif type(value) == "string" then | 82 | elseif type(value) == "string" then |
| 83 | return string.format("%q", value) | 83 | return string.format("%q", value) |
| 84 | elseif type(value) == "nil" or type(value) == "number" or | 84 | elseif type(value) == "nil" or type(value) == "number" or |
diff --git a/tests/decode.lua b/tests/decode.lua index cac29e6..89354cd 100755 --- a/tests/decode.lua +++ b/tests/decode.lua | |||
| @@ -7,8 +7,8 @@ | |||
| 7 | -- ./decode.lua test.json | 7 | -- ./decode.lua test.json |
| 8 | 8 | ||
| 9 | require "common" | 9 | require "common" |
| 10 | require "cjson" | 10 | local json = require "cjson" |
| 11 | 11 | ||
| 12 | local json_text = file_load(arg[1]) | 12 | local json_text = file_load(arg[1]) |
| 13 | local t = cjson.decode(json_text) | 13 | local t = json.decode(json_text) |
| 14 | print(serialise_value(t)) | 14 | print(serialise_value(t)) |
diff --git a/tests/encode.lua b/tests/encode.lua index f13787c..a8d749a 100755 --- a/tests/encode.lua +++ b/tests/encode.lua | |||
| @@ -7,7 +7,7 @@ | |||
| 7 | -- ./encode.lua lua_data.lua | 7 | -- ./encode.lua lua_data.lua |
| 8 | 8 | ||
| 9 | require "common" | 9 | require "common" |
| 10 | require "cjson" | 10 | local json = require "cjson" |
| 11 | 11 | ||
| 12 | function get_lua_table(file) | 12 | function get_lua_table(file) |
| 13 | local func = loadstring("data = " .. file_load(file)) | 13 | local func = loadstring("data = " .. file_load(file)) |
| @@ -23,6 +23,6 @@ function get_lua_table(file) | |||
| 23 | end | 23 | end |
| 24 | 24 | ||
| 25 | local t = get_lua_table(arg[1]) | 25 | local t = get_lua_table(arg[1]) |
| 26 | print(cjson.encode(t)) | 26 | print(json.encode(t)) |
| 27 | 27 | ||
| 28 | -- vi:ai et sw=4 ts=4: | 28 | -- vi:ai et sw=4 ts=4: |
diff --git a/tests/test.lua b/tests/test.lua index bdae6ea..99ac73a 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -112,8 +112,8 @@ local decode_numeric_tests = { | |||
| 112 | 112 | ||
| 113 | local encode_table_tests = { | 113 | local encode_table_tests = { |
| 114 | function() | 114 | function() |
| 115 | cjson.encode_sparse_array(true, 2, 3) | 115 | json.encode_sparse_array(true, 2, 3) |
| 116 | cjson.encode_max_depth(5) | 116 | json.encode_max_depth(5) |
| 117 | return "Setting sparse array (true, 2, 3) / max depth (5)" | 117 | return "Setting sparse array (true, 2, 3) / max depth (5)" |
| 118 | end, | 118 | end, |
| 119 | { json.encode, { { [3] = "sparse test" } }, | 119 | { json.encode, { { [3] = "sparse test" } }, |
| @@ -211,19 +211,19 @@ local escape_tests = { | |||
| 211 | local locale_tests = { | 211 | local locale_tests = { |
| 212 | function () | 212 | function () |
| 213 | os.setlocale("cs_CZ") | 213 | os.setlocale("cs_CZ") |
| 214 | cjson.update_locale() | 214 | json.update_locale() |
| 215 | return "Setting locale to cs_CZ (comma separator)" | 215 | return "Setting locale to cs_CZ (comma separator)" |
| 216 | end, | 216 | end, |
| 217 | { json.encode, { 1.5 }, true, { '1.5' } }, | 217 | { json.encode, { 1.5 }, true, { '1.5' } }, |
| 218 | { json.decode, { "[ 10, \"test\" ]" }, true, { { 10, "test" } } }, | 218 | { json.decode, { "[ 10, \"test\" ]" }, true, { { 10, "test" } } }, |
| 219 | function () | 219 | function () |
| 220 | os.setlocale("C") | 220 | os.setlocale("C") |
| 221 | cjson.update_locale() | 221 | json.update_locale() |
| 222 | return "Reverting locale to POSIX" | 222 | return "Reverting locale to POSIX" |
| 223 | end | 223 | end |
| 224 | } | 224 | } |
| 225 | 225 | ||
| 226 | print(string.format("Testing Lua CJSON version %s\n", cjson.version)) | 226 | print(string.format("Testing Lua CJSON version %s\n", json.version)) |
| 227 | 227 | ||
| 228 | run_test_group("decode simple value", decode_simple_tests) | 228 | run_test_group("decode simple value", decode_simple_tests) |
| 229 | run_test_group("encode simple value", encode_simple_tests) | 229 | run_test_group("encode simple value", encode_simple_tests) |
| @@ -232,7 +232,7 @@ run_test_group("decode numeric", decode_numeric_tests) | |||
| 232 | -- INCLUDE: | 232 | -- INCLUDE: |
| 233 | -- - Sparse array exception.. | 233 | -- - Sparse array exception.. |
| 234 | -- - .. | 234 | -- - .. |
| 235 | -- cjson.encode_sparse_array(true, 2, 3) | 235 | -- json.encode_sparse_array(true, 2, 3) |
| 236 | 236 | ||
| 237 | run_test_group("encode table", encode_table_tests) | 237 | run_test_group("encode table", encode_table_tests) |
| 238 | run_test_group("decode error", decode_error_tests) | 238 | run_test_group("decode error", decode_error_tests) |
| @@ -240,8 +240,8 @@ run_test_group("encode error", encode_error_tests) | |||
| 240 | run_test_group("escape", escape_tests) | 240 | run_test_group("escape", escape_tests) |
| 241 | run_test_group("locale", locale_tests) | 241 | run_test_group("locale", locale_tests) |
| 242 | 242 | ||
| 243 | cjson.refuse_invalid_numbers(false) | 243 | json.refuse_invalid_numbers(false) |
| 244 | cjson.encode_max_depth(20) | 244 | json.encode_max_depth(20) |
| 245 | for i = 1, #arg do | 245 | for i = 1, #arg do |
| 246 | run_test("decode cycle " .. arg[i], test_decode_cycle, { arg[i] }, | 246 | run_test("decode cycle " .. arg[i], test_decode_cycle, { arg[i] }, |
| 247 | true, { true }) | 247 | true, { true }) |
