forked from Mirrorlandia_minetest/minetest
Add Lua unit tests to builtin using busted (#9184)
This commit is contained in:
parent
6d472b1840
commit
1173ff0c13
@ -18,7 +18,7 @@ read_globals = {
|
|||||||
"profiler",
|
"profiler",
|
||||||
"Settings",
|
"Settings",
|
||||||
|
|
||||||
string = {fields = {"split"}},
|
string = {fields = {"split", "trim"}},
|
||||||
table = {fields = {"copy", "getn", "indexof", "insert_all"}},
|
table = {fields = {"copy", "getn", "indexof", "insert_all"}},
|
||||||
math = {fields = {"hypot"}},
|
math = {fields = {"hypot"}},
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ matrix:
|
|||||||
- ubuntu-toolchain-r-test
|
- ubuntu-toolchain-r-test
|
||||||
- llvm-toolchain-trusty-7
|
- llvm-toolchain-trusty-7
|
||||||
|
|
||||||
- name: "Builtin Luacheck"
|
- name: "Builtin Luacheck and Unit Tests"
|
||||||
language: generic
|
language: generic
|
||||||
compiler: null
|
compiler: null
|
||||||
os: linux
|
os: linux
|
||||||
@ -31,8 +31,10 @@ matrix:
|
|||||||
- luarocks
|
- luarocks
|
||||||
before_install:
|
before_install:
|
||||||
- luarocks install --local luacheck
|
- luarocks install --local luacheck
|
||||||
|
- luarocks install --local busted
|
||||||
script:
|
script:
|
||||||
- $HOME/.luarocks/bin/luacheck builtin
|
- $HOME/.luarocks/bin/luacheck builtin
|
||||||
|
- $HOME/.luarocks/bin/busted builtin
|
||||||
|
|
||||||
- env: CLANG_TIDY=clang-tidy-7
|
- env: CLANG_TIDY=clang-tidy-7
|
||||||
compiler: clang
|
compiler: clang
|
||||||
|
@ -200,9 +200,6 @@ function table.indexof(list, val)
|
|||||||
return -1
|
return -1
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(table.indexof({"foo", "bar"}, "foo") == 1)
|
|
||||||
assert(table.indexof({"foo", "bar"}, "baz") == -1)
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
if INIT ~= "client" then
|
if INIT ~= "client" then
|
||||||
function file_exists(filename)
|
function file_exists(filename)
|
||||||
@ -220,8 +217,6 @@ function string:trim()
|
|||||||
return (self:gsub("^%s*(.-)%s*$", "%1"))
|
return (self:gsub("^%s*(.-)%s*$", "%1"))
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
function math.hypot(x, y)
|
function math.hypot(x, y)
|
||||||
local t
|
local t
|
||||||
@ -520,9 +515,6 @@ function core.string_to_pos(value)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(core.string_to_pos("10.0, 5, -2").x == 10)
|
|
||||||
assert(core.string_to_pos("( 10.0, 5, -2)").z == -2)
|
|
||||||
assert(core.string_to_pos("asd, 5, -2)") == nil)
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
function core.string_to_area(value)
|
function core.string_to_area(value)
|
||||||
@ -764,6 +756,3 @@ function core.privs_to_string(privs, delim)
|
|||||||
end
|
end
|
||||||
return table.concat(list, delim)
|
return table.concat(list, delim)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(core.string_to_privs("a,b").b == true)
|
|
||||||
assert(core.privs_to_string({a=true,b=true}) == "a,b")
|
|
||||||
|
@ -204,17 +204,3 @@ function core.deserialize(str, safe)
|
|||||||
return nil, data
|
return nil, data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Unit tests
|
|
||||||
local test_in = {cat={sound="nyan", speed=400}, dog={sound="woof"}}
|
|
||||||
local test_out = core.deserialize(core.serialize(test_in))
|
|
||||||
|
|
||||||
assert(test_in.cat.sound == test_out.cat.sound)
|
|
||||||
assert(test_in.cat.speed == test_out.cat.speed)
|
|
||||||
assert(test_in.dog.sound == test_out.dog.sound)
|
|
||||||
|
|
||||||
test_in = {escape_chars="\n\r\t\v\\\"\'", non_european="θשׁ٩∂"}
|
|
||||||
test_out = core.deserialize(core.serialize(test_in))
|
|
||||||
assert(test_in.escape_chars == test_out.escape_chars)
|
|
||||||
assert(test_in.non_european == test_out.non_european)
|
|
||||||
|
73
builtin/common/tests/misc_helpers_spec.lua
Normal file
73
builtin/common/tests/misc_helpers_spec.lua
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
_G.core = {}
|
||||||
|
dofile("builtin/common/misc_helpers.lua")
|
||||||
|
|
||||||
|
describe("string", function()
|
||||||
|
it("trim()", function()
|
||||||
|
assert.equal("foo bar", string.trim("\n \t\tfoo bar\t "))
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("split()", function()
|
||||||
|
it("removes empty", function()
|
||||||
|
assert.same({ "hello" }, string.split("hello"))
|
||||||
|
assert.same({ "hello", "world" }, string.split("hello,world"))
|
||||||
|
assert.same({ "hello", "world" }, string.split("hello,world,,,"))
|
||||||
|
assert.same({ "hello", "world" }, string.split(",,,hello,world"))
|
||||||
|
assert.same({ "hello", "world", "2" }, string.split("hello,,,world,2"))
|
||||||
|
assert.same({ "hello ", " world" }, string.split("hello :| world", ":|"))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("keeps empty", function()
|
||||||
|
assert.same({ "hello" }, string.split("hello", ",", true))
|
||||||
|
assert.same({ "hello", "world" }, string.split("hello,world", ",", true))
|
||||||
|
assert.same({ "hello", "world", "" }, string.split("hello,world,", ",", true))
|
||||||
|
assert.same({ "hello", "", "", "world", "2" }, string.split("hello,,,world,2", ",", true))
|
||||||
|
assert.same({ "", "", "hello", "world", "2" }, string.split(",,hello,world,2", ",", true))
|
||||||
|
assert.same({ "hello ", " world | :" }, string.split("hello :| world | :", ":|"))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("max_splits", function()
|
||||||
|
assert.same({ "one" }, string.split("one", ",", true, 2))
|
||||||
|
assert.same({ "one,two,three,four" }, string.split("one,two,three,four", ",", true, 0))
|
||||||
|
assert.same({ "one", "two", "three,four" }, string.split("one,two,three,four", ",", true, 2))
|
||||||
|
assert.same({ "one", "", "two,three,four" }, string.split("one,,two,three,four", ",", true, 2))
|
||||||
|
assert.same({ "one", "two", "three,four" }, string.split("one,,,,,,two,three,four", ",", false, 2))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("pattern", function()
|
||||||
|
assert.same({ "one", "two" }, string.split("one,two", ",", false, -1, true))
|
||||||
|
assert.same({ "one", "two", "three" }, string.split("one2two3three", "%d", false, -1, true))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("privs", function()
|
||||||
|
it("from string", function()
|
||||||
|
assert.same({ a = true, b = true }, core.string_to_privs("a,b"))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("to string", function()
|
||||||
|
assert.equal("one", core.privs_to_string({ one=true }))
|
||||||
|
|
||||||
|
local ret = core.privs_to_string({ a=true, b=true })
|
||||||
|
assert(ret == "a,b" or ret == "b,a")
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("pos", function()
|
||||||
|
it("from string", function()
|
||||||
|
assert.same({ x = 10, y = 5.1, z = -2}, core.string_to_pos("10.0, 5.1, -2"))
|
||||||
|
assert.same({ x = 10, y = 5.1, z = -2}, core.string_to_pos("( 10.0, 5.1, -2)"))
|
||||||
|
assert.is_nil(core.string_to_pos("asd, 5, -2)"))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("to string", function()
|
||||||
|
assert.equal("(10.1,5.2,-2.3)", core.pos_to_string({ x = 10.1, y = 5.2, z = -2.3}))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe("table", function()
|
||||||
|
it("indexof()", function()
|
||||||
|
assert.equal(1, table.indexof({"foo", "bar"}, "foo"))
|
||||||
|
assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
|
||||||
|
end)
|
||||||
|
end)
|
28
builtin/common/tests/serialize_spec.lua
Normal file
28
builtin/common/tests/serialize_spec.lua
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
_G.core = {}
|
||||||
|
|
||||||
|
_G.setfenv = function() end
|
||||||
|
|
||||||
|
dofile("builtin/common/serialize.lua")
|
||||||
|
|
||||||
|
describe("serialize", function()
|
||||||
|
it("works", function()
|
||||||
|
local test_in = {cat={sound="nyan", speed=400}, dog={sound="woof"}}
|
||||||
|
local test_out = core.deserialize(core.serialize(test_in))
|
||||||
|
|
||||||
|
assert.same(test_in, test_out)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles characters", function()
|
||||||
|
local test_in = {escape_chars="\n\r\t\v\\\"\'", non_european="θשׁ٩∂"}
|
||||||
|
local test_out = core.deserialize(core.serialize(test_in))
|
||||||
|
assert.same(test_in, test_out)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles recursive structures", function()
|
||||||
|
local test_in = { hello = "world" }
|
||||||
|
test_in.foo = test_in
|
||||||
|
|
||||||
|
local test_out = core.deserialize(core.serialize(test_in))
|
||||||
|
assert.same(test_in, test_out)
|
||||||
|
end)
|
||||||
|
end)
|
46
builtin/common/tests/vector_spec.lua
Normal file
46
builtin/common/tests/vector_spec.lua
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
_G.vector = {}
|
||||||
|
dofile("builtin/common/vector.lua")
|
||||||
|
|
||||||
|
describe("vector", function()
|
||||||
|
describe("new()", function()
|
||||||
|
it("constructs", function()
|
||||||
|
assert.same({ x = 0, y = 0, z = 0 }, vector.new())
|
||||||
|
assert.same({ x = 1, y = 2, z = 3 }, vector.new(1, 2, 3))
|
||||||
|
assert.same({ x = 3, y = 2, z = 1 }, vector.new({ x = 3, y = 2, z = 1 }))
|
||||||
|
|
||||||
|
local input = vector.new({ x = 3, y = 2, z = 1 })
|
||||||
|
local output = vector.new(input)
|
||||||
|
assert.same(input, output)
|
||||||
|
assert.are_not.equal(input, output)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("throws on invalid input", function()
|
||||||
|
assert.has.errors(function()
|
||||||
|
vector.new({ x = 3 })
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert.has.errors(function()
|
||||||
|
vector.new({ d = 3 })
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("equal()", function()
|
||||||
|
local function assertE(a, b)
|
||||||
|
assert.is_true(vector.equals(a, b))
|
||||||
|
end
|
||||||
|
local function assertNE(a, b)
|
||||||
|
assert.is_false(vector.equals(a, b))
|
||||||
|
end
|
||||||
|
|
||||||
|
assertE({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
|
||||||
|
assertE({x = -1, y = 0, z = 1}, {x = -1, y = 0, z = 1})
|
||||||
|
local a = { x = 2, y = 4, z = -10 }
|
||||||
|
assertE(a, a)
|
||||||
|
assertNE({x = -1, y = 0, z = 1}, a)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("add()", function()
|
||||||
|
assert.same({ x = 2, y = 4, z = 6 }, vector.add(vector.new(1, 2, 3), { x = 1, y = 2, z = 3 }))
|
||||||
|
end)
|
||||||
|
end)
|
Loading…
Reference in New Issue
Block a user