aboutsummaryrefslogtreecommitdiff
path: root/builtin/common/tests/misc_helpers_spec.lua
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2020-01-18 02:09:18 +0000
committerGitHub <noreply@github.com>2020-01-18 02:09:18 +0000
commit1173ff0c13a07dec2ede3166378dc5f50fff37b3 (patch)
treedf83476a7389fce570fb2bb6852e0de024d20e98 /builtin/common/tests/misc_helpers_spec.lua
parent6d472b1840971a087841ea7feee5a921fb82b84a (diff)
downloadminetest-1173ff0c13a07dec2ede3166378dc5f50fff37b3.tar.gz
minetest-1173ff0c13a07dec2ede3166378dc5f50fff37b3.tar.bz2
minetest-1173ff0c13a07dec2ede3166378dc5f50fff37b3.zip
Add Lua unit tests to builtin using busted (#9184)
Diffstat (limited to 'builtin/common/tests/misc_helpers_spec.lua')
-rw-r--r--builtin/common/tests/misc_helpers_spec.lua73
1 files changed, 73 insertions, 0 deletions
diff --git a/builtin/common/tests/misc_helpers_spec.lua b/builtin/common/tests/misc_helpers_spec.lua
new file mode 100644
index 000000000..bb9d13e7f
--- /dev/null
+++ b/builtin/common/tests/misc_helpers_spec.lua
@@ -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)