| 1 |
-- Test some UCL stuff |
| 2 |
|
| 3 |
context("UCL manipulation", function() |
| 4 |
local ucl = require "ucl" |
| 5 |
|
| 6 |
local parser = ucl.parser() |
| 7 |
local res, err = parser:parse_string('{"key":"val"}') |
| 8 |
assert(res) |
| 9 |
|
| 10 |
local reply = parser:get_object_wrapped() |
| 11 |
local expected = { |
| 12 |
key = 'ohlol', |
| 13 |
ololo = 'ohlol' |
| 14 |
} |
| 15 |
|
| 16 |
test("UCL transparent test: object", function() |
| 17 |
assert_equal(tostring(reply), '{"key":"val"}') |
| 18 |
assert_equal(reply:type(), 'object') |
| 19 |
assert_equal(reply:at('key'):unwrap(), 'val') |
| 20 |
reply.ololo = 'ohlol' |
| 21 |
reply.ololo = 'ohlol' |
| 22 |
reply.key = 'ohlol' |
| 23 |
assert_equal(reply:at('key'):unwrap(), 'ohlol') |
| 24 |
|
| 25 |
for k, v in reply:pairs() do |
| 26 |
assert_equal(expected[k], v:unwrap()) |
| 27 |
end |
| 28 |
end) |
| 29 |
|
| 30 |
test("UCL transparent test: array", function() |
| 31 |
parser = ucl.parser() |
| 32 |
res, err = parser:parse_string('["e1","e2"]') |
| 33 |
assert(res) |
| 34 |
local ireply = parser:get_object_wrapped() |
| 35 |
|
| 36 |
assert_equal(tostring(ireply), '["e1","e2"]') |
| 37 |
assert_equal(ireply:type(), 'array') |
| 38 |
ireply[1] = 1 |
| 39 |
ireply[1] = 1 |
| 40 |
ireply[1] = 1 |
| 41 |
ireply[1] = 1 |
| 42 |
ireply[1] = 1 |
| 43 |
ireply[ireply:len() + 1] = 100500 |
| 44 |
local iexpected = { 1, "e2", 100500 } |
| 45 |
for k, v in ireply:ipairs() do |
| 46 |
assert_equal(v:unwrap(), iexpected[k]) |
| 47 |
end |
| 48 |
end) |
| 49 |
|
| 50 |
test("UCL transparent test: concat", function() |
| 51 |
reply.tbl = ireply |
| 52 |
expected.tbl = iexpected |
| 53 |
for k, v in reply:pairs() do |
| 54 |
if type(expected[k]) == 'table' then |
| 55 |
for kk, vv in v:ipairs() do |
| 56 |
assert_equal(expected[k][kk], vv:unwrap()) |
| 57 |
end |
| 58 |
else |
| 59 |
assert_equal(expected[k], v:unwrap()) |
| 60 |
end |
| 61 |
end |
| 62 |
end) |
| 63 |
|
| 64 |
test("UCL transparent test: implicit conversion array->object", function() |
| 65 |
-- Assign empty table, so it'll be an array |
| 66 |
reply.t = {} |
| 67 |
assert_equal(reply.t:type(), 'array') |
| 68 |
-- We can convert empty table to object |
| 69 |
reply.t.test = 'test' |
| 70 |
assert_equal(reply.t:type(), 'object') |
| 71 |
assert_equal(reply.t.test:unwrap(), 'test') |
| 72 |
end) |
| 73 |
|
| 74 |
collectgarbage() -- To ensure we don't crash with asan |
| 75 |
end) |