test: Add tests for utils module

This commit is contained in:
Myzel394 2024-04-27 20:46:09 +02:00
parent e427c5f224
commit 342823df20
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 198 additions and 5 deletions

198
tests/test_utils.lua Normal file
View File

@ -0,0 +1,198 @@
local lu = require"luaunit.luaunit"
local utils = require"lua.jsonfly.utils"
function testBasicKey()
local key = "foo.bar"
---@type KeyDescription[]
local EXPECTED = {
{
type = "object_wrapper",
},
{
type = "key",
key = "foo",
},
{
type = "object_wrapper",
},
{
type = "key",
key = "bar",
}
}
local descriptor = utils:extract_key_description(key)
lu.assertEquals(descriptor, EXPECTED)
end
function testArrayKey()
local key = "foo.0.bar"
---@type KeyDescription[]
local EXPECTED = {
{
type = "object_wrapper",
},
{
type = "key",
key = "foo",
},
{
type = "array_wrapper",
},
{
type = "array_index",
key = 0,
},
{
type = "object_wrapper",
},
{
type = "key",
key = "bar",
}
}
local descriptor = utils:extract_key_description(key)
lu.assertEquals(descriptor, EXPECTED)
end
function testNestedArrayKey()
local key = "foo.0.bar.1.baz"
---@type KeyDescription[]
local EXPECTED = {
{
type = "object_wrapper",
},
{
type = "key",
key = "foo",
},
{
type = "array_wrapper",
},
{
type = "array_index",
key = 0,
},
{
type = "object_wrapper",
},
{
type = "key",
key = "bar",
},
{
type = "array_wrapper",
},
{
type = "array_index",
key = 1,
},
{
type = "object_wrapper",
},
{
type = "key",
key = "baz",
}
}
local descriptor = utils:extract_key_description(key)
lu.assertEquals(descriptor, EXPECTED)
end
function testEscapedArrayDoesNotCreateArray()
local key = "foo.\\0.bar"
---@type KeyDescription[]
local EXPECTED = {
{
type = "object_wrapper",
},
{
type = "key",
key = "foo",
},
{
type = "object_wrapper",
},
{
type = "key",
key = "0",
},
{
type = "object_wrapper",
},
{
type = "key",
key = "bar",
}
}
local descriptor = utils:extract_key_description(key)
lu.assertEquals(descriptor, EXPECTED)
end
function testBracketArrayKey()
local key = "foo.[0].bar"
---@type KeyDescription[]
local EXPECTED = {
{
type = "object_wrapper",
},
{
type = "key",
key = "foo",
},
{
type = "array_wrapper",
},
{
type = "array_index",
key = 0,
},
{
type = "object_wrapper",
},
{
type = "key",
key = "bar",
}
}
local descriptor = utils:extract_key_description(key)
lu.assertEquals(descriptor, EXPECTED)
end
function testRootArrayKey()
local key = "0.foo"
---@type KeyDescription[]
local EXPECTED = {
{
type = "array_wrapper",
},
{
type = "array_index",
key = 0,
},
{
type = "object_wrapper",
},
{
type = "key",
key = "foo",
}
}
local descriptor = utils:extract_key_description(key)
lu.assertEquals(descriptor, EXPECTED)
end
os.exit( lu.LuaUnit.run() )

View File

@ -1,5 +0,0 @@
local lu = require"luaunit.luaunit"
os.exit( lu.LuaUnit.run() )