]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/tests/misc_helpers_spec.lua
b112368605886aed85a7de325eecff02f909d534
[dragonfireclient.git] / builtin / common / tests / misc_helpers_spec.lua
1 _G.core = {}
2 _G.vector = {metatable = {}}
3 dofile("builtin/common/vector.lua")
4 dofile("builtin/common/misc_helpers.lua")
5
6 describe("string", function()
7         it("trim()", function()
8                 assert.equal("foo bar", string.trim("\n \t\tfoo bar\t "))
9         end)
10
11         describe("split()", function()
12                 it("removes empty", function()
13                         assert.same({ "hello" }, string.split("hello"))
14                         assert.same({ "hello", "world" }, string.split("hello,world"))
15                         assert.same({ "hello", "world" }, string.split("hello,world,,,"))
16                         assert.same({ "hello", "world" }, string.split(",,,hello,world"))
17                         assert.same({ "hello", "world", "2" }, string.split("hello,,,world,2"))
18                         assert.same({ "hello ", " world" }, string.split("hello :| world", ":|"))
19                 end)
20
21                 it("keeps empty", function()
22                         assert.same({ "hello" }, string.split("hello", ",", true))
23                         assert.same({ "hello", "world" }, string.split("hello,world", ",", true))
24                         assert.same({ "hello", "world", "" }, string.split("hello,world,", ",", true))
25                         assert.same({ "hello", "", "", "world", "2" }, string.split("hello,,,world,2", ",", true))
26                         assert.same({ "", "", "hello", "world", "2" }, string.split(",,hello,world,2", ",", true))
27                         assert.same({ "hello ", " world | :" }, string.split("hello :| world | :", ":|"))
28                 end)
29
30                 it("max_splits", function()
31                         assert.same({ "one" }, string.split("one", ",", true, 2))
32                         assert.same({ "one,two,three,four" }, string.split("one,two,three,four", ",", true, 0))
33                         assert.same({ "one", "two", "three,four" }, string.split("one,two,three,four", ",", true, 2))
34                         assert.same({ "one", "", "two,three,four" }, string.split("one,,two,three,four", ",", true, 2))
35                         assert.same({ "one", "two", "three,four" }, string.split("one,,,,,,two,three,four", ",", false, 2))
36                 end)
37
38                 it("pattern", function()
39                         assert.same({ "one", "two" }, string.split("one,two", ",", false, -1, true))
40                         assert.same({ "one", "two", "three" }, string.split("one2two3three", "%d", false, -1, true))
41                 end)
42         end)
43 end)
44
45 describe("privs", function()
46         it("from string", function()
47                 assert.same({ a = true, b = true }, core.string_to_privs("a,b"))
48         end)
49
50         it("to string", function()
51                 assert.equal("one", core.privs_to_string({ one=true }))
52
53                 local ret = core.privs_to_string({ a=true, b=true })
54                 assert(ret == "a,b" or ret == "b,a")
55         end)
56 end)
57
58 describe("pos", function()
59         it("from string", function()
60                 assert.equal(vector.new(10, 5.1, -2), core.string_to_pos("10.0, 5.1, -2"))
61                 assert.equal(vector.new(10, 5.1, -2), core.string_to_pos("( 10.0, 5.1, -2)"))
62                 assert.is_nil(core.string_to_pos("asd, 5, -2)"))
63         end)
64
65         it("to string", function()
66                 assert.equal("(10.1,5.2,-2.3)", core.pos_to_string({ x = 10.1, y = 5.2, z = -2.3}))
67         end)
68 end)
69
70 describe("table", function()
71         it("indexof()", function()
72                 assert.equal(1, table.indexof({"foo", "bar"}, "foo"))
73                 assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
74         end)
75 end)