aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/unit/ucl.lua
blob: 9bbf7225c5c9a456983612caafae3fd4956dcd07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- Test some UCL stuff

context("UCL manipulation", function()
  local ucl = require "ucl"

  test("UCL transparent test", function()
    local parser = ucl.parser()
    local res, err = parser:parse_string('{"key":"val"}')
    assert(res)

    local reply = parser:get_object_wrapped()

    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')
    local expected = {
      key = 'ohlol',
      ololo = 'ohlol'
    }
    for k, v in reply:pairs() do
      assert_equal(expected[k], v:unwrap())
    end

    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

    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

    collectgarbage() -- To ensure we don't crash with asan
  end)
end)