diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-08-16 18:50:58 +0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-16 18:50:58 +0600 |
commit | 65f250ee4ea3bced33af0f7b4f56f404906e631b (patch) | |
tree | 1a51edbb7c677cd834a9a4516fb481994afa69d6 /test/lua/unit | |
parent | 7a94c375be320b2897277cbbf9fb6a73d9c44f3c (diff) | |
parent | 089746e704a325bf429dcb1e7caa554253c47063 (diff) | |
download | rspamd-65f250ee4ea3bced33af0f7b4f56f404906e631b.tar.gz rspamd-65f250ee4ea3bced33af0f7b4f56f404906e631b.zip |
Merge pull request #5104 from rspamd/vstakhov-transparent-ucl
[Project] Allow manipulations with opaque UCL objects
Diffstat (limited to 'test/lua/unit')
-rw-r--r-- | test/lua/unit/ucl.lua | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/test/lua/unit/ucl.lua b/test/lua/unit/ucl.lua new file mode 100644 index 000000000..1b975d390 --- /dev/null +++ b/test/lua/unit/ucl.lua @@ -0,0 +1,75 @@ +-- Test some UCL stuff + +context("UCL manipulation", function() + local ucl = require "ucl" + + local parser = ucl.parser() + local res, err = parser:parse_string('{"key":"val"}') + assert(res) + + local reply = parser:get_object_wrapped() + local expected = { + key = 'ohlol', + ololo = 'ohlol' + } + + test("UCL transparent test: object", function() + assert_equal(tostring(reply), '{"key":"val"}') + assert_equal(reply:type(), 'object') + assert_equal(reply:at('key'):unwrap(), 'val') + reply.ololo = 'ohlol' + reply.ololo = 'ohlol' + reply.key = 'ohlol' + assert_equal(reply:at('key'):unwrap(), 'ohlol') + + for k, v in reply:pairs() do + assert_equal(expected[k], v:unwrap()) + end + end) + + test("UCL transparent test: array", function() + parser = ucl.parser() + res, err = parser:parse_string('["e1","e2"]') + assert(res) + local ireply = parser:get_object_wrapped() + + assert_equal(tostring(ireply), '["e1","e2"]') + assert_equal(ireply:type(), 'array') + ireply[1] = 1 + ireply[1] = 1 + ireply[1] = 1 + ireply[1] = 1 + ireply[1] = 1 + ireply[ireply:len() + 1] = 100500 + local iexpected = { 1, "e2", 100500 } + for k, v in ireply:ipairs() do + assert_equal(v:unwrap(), iexpected[k]) + end + end) + + test("UCL transparent test: concat", function() + reply.tbl = ireply + expected.tbl = iexpected + for k, v in reply:pairs() do + if type(expected[k]) == 'table' then + for kk, vv in v:ipairs() do + assert_equal(expected[k][kk], vv:unwrap()) + end + else + assert_equal(expected[k], v:unwrap()) + end + end + end) + + test("UCL transparent test: implicit conversion array->object", function() + -- Assign empty table, so it'll be an array + reply.t = {} + assert_equal(reply.t:type(), 'array') + -- We can convert empty table to object + reply.t.test = 'test' + assert_equal(reply.t:type(), 'object') + assert_equal(reply.t.test:unwrap(), 'test') + end) + + collectgarbage() -- To ensure we don't crash with asan +end)
\ No newline at end of file |