base: fixes travis
[luajson.git] / tests / lunit-strings.lua
blob0b6ebd5f612093d2bb1ba0fe0d176a3bd070adaf
1 local json = require("json")
2 local lunit = require("lunit")
3 local testutil = require("testutil")
5 local encode = json.encode
6 -- DECODE NOT 'local' due to requirement for testutil to access it
7 decode = json.decode.getDecoder(false)
9 local error = error
11 module("lunit-strings", lunit.testcase, package.seeall)
13 local function assert_table_equal(expect, t)
14 if type(expect) ~= 'table' then
15 return assert_equal(expect, t)
16 end
17 for k,v in pairs(expect) do
18 if type(k) ~= 'string' and type(k) ~= 'number' and type(k) ~= 'boolean' then
19 error("INVALID expected table key")
20 end
21 local found = t[k]
22 if found == nil then
23 fail(tostring(k) .. " not found but expected")
24 end
25 assert_table_equal(v, t[k])
26 end
27 for k,v in pairs(t) do
28 if nil == expect[k] then
29 fail(tostring(k) .. " found but not expected")
30 end
31 end
32 end
34 function setup()
35 -- Ensure that the decoder is reset
36 _G["decode"] = json.decode.getDecoder(false)
37 end
39 function test_strict_quotes()
40 local opts = {
41 strings = {
42 strict_quotes = true
45 assert_error(function()
46 local decoder = json.decode.getDecoder(opts)
47 decoder("'hello'")
48 end)
49 opts.strings.strict_quotes = false
50 assert_equal("hello", json.decode.getDecoder(opts)("'hello'"))
51 -- Quote test
52 assert_equal("he'\"llo'", json.decode.getDecoder(opts)("'he\\'\"llo\\''"))
54 end
56 local utf16_matches = {
57 -- 1-byte
58 { '"\\u0000"', string.char(0x00) },
59 { '"\\u007F"', string.char(0x7F) },
60 -- 2-byte
61 { '"\\u0080"', string.char(0xC2, 0x80) },
62 { '"\\u00A2"', string.char(0xC2, 0xA2) },
63 { '"\\u07FF"', string.char(0xDF, 0xBF) },
64 -- 3-byte
65 { '"\\u0800"', string.char(0xE0, 0xA0, 0x80) },
66 { '"\\u20AC"', string.char(0xE2, 0x82, 0xAC) },
67 { '"\\uFEFF"', string.char(0xEF, 0xBB, 0xBF) },
68 { '"\\uFFFF"', string.char(0xEF, 0xBF, 0xBF) }
71 function test_utf16_decode()
72 for i, v in ipairs(utf16_matches) do
73 -- Test that the default \u decoder outputs UTF8
74 local num = tostring(i) .. ' '
75 assert_equal(num .. v[2], num .. json.decode(v[1]))
76 end
77 end
79 local BOM = string.char(0xEF, 0xBB, 0xBF)
80 -- BOM skipping tests - here due to relation to UTF8/16
81 local BOM_skip_tests = {
82 { BOM .. '"x"', "x" },
83 { BOM .. '["\\uFFFF",true]', { string.char(0xEF, 0xBF, 0xBF), true } },
84 -- Other uses of unicode spaces
87 function test_bom_skip()
88 for i,v in ipairs(BOM_skip_tests) do
89 assert_table_equal(v[2], json.decode(v[1]))
90 end
91 end
93 -- Unicode whitespace codepoints gleaned from unicode.org
94 local WHITESPACES = {
95 "\\u0009", -- \t
96 "\\u000A", -- \n
97 "\\u000B", -- \v
98 "\\u000C", -- \f
99 "\\u000D", -- \r
100 "\\u0020", -- space
101 "\\u0085",
102 "\\u00A0",
103 "\\u1680",
104 "\\u180E",
105 "\\u2000",
106 "\\u2001",
107 "\\u2002",
108 "\\u2003",
109 "\\u2004",
110 "\\u2005",
111 "\\u2006",
112 "\\u2007",
113 "\\u2008",
114 "\\u2009",
115 "\\u200A",
116 "\\u200B", -- addition, zero-width space
117 "\\u2028",
118 "\\u2029",
119 "\\u202F",
120 "\\u205F",
121 "\\u3000",
122 "\\uFEFF" -- Zero-width non-breaking space (BOM)
125 local inject_ws_values = {
126 "%WS%true",
127 " %WS%'the%WS blob' %WS%",
128 "%WS%{ key: %WS%\"valueMan\",%WS% key2:%WS%4.4}",
129 "%WS%false%WS%"
131 function test_whitespace_ignore()
132 for _, ws in ipairs(WHITESPACES) do
133 ws = json.decode('"' .. ws .. '"')
134 for _, v in ipairs(inject_ws_values) do
135 v = v:gsub("%%WS%%", ws)
136 assert_true(nil ~= json.decode(v))
141 function test_u_encoding()
142 local encoder = json.encode.getEncoder()
143 local decoder = json.decode.getDecoder()
144 for i = 0, 255 do
145 local char = string.char(i)
146 assert_equal(char, decoder(encoder(char)))
150 function test_x_encoding()
151 local encoder = json.encode.getEncoder({ strings = { xEncode = true } })
152 local decoder = json.decode.getDecoder()
153 for i = 0, 255 do
154 local char = string.char(i)
155 assert_equal(char, decoder(encoder(char)))
159 function test_strict_decoding()
160 local encoder = json.encode.getEncoder(json.encode.strict)
161 local decoder = json.decode.getDecoder(json.decode.strict)
162 for i = 0, 255 do
163 local char = string.char(i)
164 -- Must wrap character in array due to decoder strict-ness
165 assert_equal(char, decoder(encoder({char}))[1])