Merge pull request #10 from Myzel394/add-tests

Add tests
This commit is contained in:
Myzel394 2024-04-27 20:59:25 +02:00 committed by GitHub
commit ec7326783d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 223 additions and 0 deletions

21
.github/workflows/run-tests.yaml vendored Normal file
View File

@ -0,0 +1,21 @@
name: Run tests
on:
pull_request:
jobs:
debug-builds:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: "recursive"
- name: Install Lua
uses: leafo/gh-actions-lua@v10
with:
luaVersion: "5.4.6"
- name: Run tests
run: lua -v ./tests/$(ls ./tests)

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "luaunit"]
path = luaunit
url = https://github.com/bluebird75/luaunit

1
luaunit Submodule

@ -0,0 +1 @@
Subproject commit 0e0d3dd06fe1955a01f0e6763bc8dc6847ee3e8d

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() )