diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/agentzh.t | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/tests/agentzh.t b/tests/agentzh.t index 0b546ff..e76f910 100644 --- a/tests/agentzh.t +++ b/tests/agentzh.t | |||
| @@ -41,7 +41,79 @@ print(cjson.encode({dogs = {}})) | |||
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | === TEST 4: & in JSON | 44 | === TEST 4: empty_array userdata |
| 45 | --- lua | ||
| 46 | local cjson = require "cjson" | ||
| 47 | print(cjson.encode({arr = cjson.empty_array})) | ||
| 48 | --- out | ||
| 49 | {"arr":[]} | ||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | === TEST 5: empty_array_mt | ||
| 54 | --- lua | ||
| 55 | local cjson = require "cjson" | ||
| 56 | local empty_arr = setmetatable({}, cjson.empty_array_mt) | ||
| 57 | print(cjson.encode({arr = empty_arr})) | ||
| 58 | --- out | ||
| 59 | {"arr":[]} | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | === TEST 6: empty_array_mt and empty tables as objects (explicit) | ||
| 64 | --- lua | ||
| 65 | local cjson = require "cjson" | ||
| 66 | local empty_arr = setmetatable({}, cjson.empty_array_mt) | ||
| 67 | print(cjson.encode({obj = {}, arr = empty_arr})) | ||
| 68 | --- out | ||
| 69 | {"arr":[],"obj":{}} | ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | === TEST 7: empty_array_mt and empty tables as objects (explicit) | ||
| 74 | --- lua | ||
| 75 | local cjson = require "cjson" | ||
| 76 | cjson.encode_empty_table_as_object(true) | ||
| 77 | local empty_arr = setmetatable({}, cjson.empty_array_mt) | ||
| 78 | local data = { | ||
| 79 | arr = empty_arr, | ||
| 80 | foo = { | ||
| 81 | obj = {}, | ||
| 82 | foobar = { | ||
| 83 | arr = cjson.empty_array, | ||
| 84 | obj = {} | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | print(cjson.encode(data)) | ||
| 89 | --- out | ||
| 90 | {"foo":{"foobar":{"obj":{},"arr":[]},"obj":{}},"arr":[]} | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | === TEST 8: empty_array_mt on non-empty tables | ||
| 95 | --- lua | ||
| 96 | local cjson = require "cjson" | ||
| 97 | cjson.encode_empty_table_as_object(true) | ||
| 98 | local array = {"hello", "world", "lua"} | ||
| 99 | setmetatable(array, cjson.empty_array_mt) | ||
| 100 | local data = { | ||
| 101 | arr = array, | ||
| 102 | foo = { | ||
| 103 | obj = {}, | ||
| 104 | foobar = { | ||
| 105 | arr = cjson.empty_array, | ||
| 106 | obj = {} | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | print(cjson.encode(data)) | ||
| 111 | --- out | ||
| 112 | {"foo":{"foobar":{"obj":{},"arr":[]},"obj":{}},"arr":["hello","world","lua"]} | ||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | === TEST 9: & in JSON | ||
| 45 | --- lua | 117 | --- lua |
| 46 | local cjson = require "cjson" | 118 | local cjson = require "cjson" |
| 47 | local a="[\"a=1&b=2\"]" | 119 | local a="[\"a=1&b=2\"]" |
| @@ -52,7 +124,7 @@ print(cjson.encode(b)) | |||
| 52 | 124 | ||
| 53 | 125 | ||
| 54 | 126 | ||
| 55 | === TEST 5: default and max precision | 127 | === TEST 10: default and max precision |
| 56 | --- lua | 128 | --- lua |
| 57 | local math = require "math" | 129 | local math = require "math" |
| 58 | local cjson = require "cjson" | 130 | local cjson = require "cjson" |
