]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/tests/misc_helpers_spec.lua
Builtin: Optimize misc helpers (#12377)
[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("area parsing", function()
71         describe("valid inputs", function()
72                 it("accepts absolute numbers", function()
73                         local p1, p2 = core.string_to_area("(10.0, 5, -2) (  30.2 4 -12.53)")
74                         assert(p1.x == 10 and p1.y == 5 and p1.z == -2)
75                         assert(p2.x == 30.2 and p2.y == 4 and p2.z == -12.53)
76                 end)
77
78                 it("accepts relative numbers", function()
79                         local p1, p2 = core.string_to_area("(1,2,3) (~5,~-5,~)", {x=10,y=10,z=10})
80                         assert(type(p1) == "table" and type(p2) == "table")
81                         assert(p1.x == 1 and p1.y == 2 and p1.z == 3)
82                         assert(p2.x == 15 and p2.y == 5 and p2.z == 10)
83
84                         p1, p2 = core.string_to_area("(1 2 3) (~5 ~-5 ~)", {x=10,y=10,z=10})
85                         assert(type(p1) == "table" and type(p2) == "table")
86                         assert(p1.x == 1 and p1.y == 2 and p1.z == 3)
87                         assert(p2.x == 15 and p2.y == 5 and p2.z == 10)
88                 end)
89         end)
90         describe("invalid inputs", function()
91                 it("rejects too few numbers", function()
92                         local p1, p2 = core.string_to_area("(1,1) (1,1,1,1)", {x=1,y=1,z=1})
93                         assert(p1 == nil and p2 == nil)
94                 end)
95
96                 it("rejects too many numbers", function()
97                         local p1, p2 = core.string_to_area("(1,1,1,1) (1,1,1,1)", {x=1,y=1,z=1})
98                         assert(p1 == nil and p2 == nil)
99                 end)
100
101                 it("rejects nan & inf", function()
102                         local p1, p2 = core.string_to_area("(1,1,1) (1,1,nan)", {x=1,y=1,z=1})
103                         assert(p1 == nil and p2 == nil)
104
105                         p1, p2 = core.string_to_area("(1,1,1) (1,1,~nan)", {x=1,y=1,z=1})
106                         assert(p1 == nil and p2 == nil)
107
108                         p1, p2 = core.string_to_area("(1,1,1) (1,~nan,1)", {x=1,y=1,z=1})
109                         assert(p1 == nil and p2 == nil)
110
111                         p1, p2 = core.string_to_area("(1,1,1) (1,1,inf)", {x=1,y=1,z=1})
112                         assert(p1 == nil and p2 == nil)
113
114                         p1, p2 = core.string_to_area("(1,1,1) (1,1,~inf)", {x=1,y=1,z=1})
115                         assert(p1 == nil and p2 == nil)
116
117                         p1, p2 = core.string_to_area("(1,1,1) (1,~inf,1)", {x=1,y=1,z=1})
118                         assert(p1 == nil and p2 == nil)
119
120                         p1, p2 = core.string_to_area("(nan,nan,nan) (nan,nan,nan)", {x=1,y=1,z=1})
121                         assert(p1 == nil and p2 == nil)
122
123                         p1, p2 = core.string_to_area("(nan,nan,nan) (nan,nan,nan)")
124                         assert(p1 == nil and p2 == nil)
125
126                         p1, p2 = core.string_to_area("(inf,inf,inf) (-inf,-inf,-inf)", {x=1,y=1,z=1})
127                         assert(p1 == nil and p2 == nil)
128
129                         p1, p2 = core.string_to_area("(inf,inf,inf) (-inf,-inf,-inf)")
130                         assert(p1 == nil and p2 == nil)
131                 end)
132
133                 it("rejects words", function()
134                         local p1, p2 = core.string_to_area("bananas", {x=1,y=1,z=1})
135                         assert(p1 == nil and p2 == nil)
136
137                         p1, p2 = core.string_to_area("bananas", "foobar")
138                         assert(p1 == nil and p2 == nil)
139
140                         p1, p2 = core.string_to_area("bananas")
141                         assert(p1 == nil and p2 == nil)
142
143                         p1, p2 = core.string_to_area("(bananas,bananas,bananas)")
144                         assert(p1 == nil and p2 == nil)
145
146                         p1, p2 = core.string_to_area("(bananas,bananas,bananas) (bananas,bananas,bananas)")
147                         assert(p1 == nil and p2 == nil)
148                 end)
149
150                 it("requires parenthesis & valid numbers", function()
151                         local p1, p2 = core.string_to_area("(10.0, 5, -2  30.2,   4, -12.53")
152                         assert(p1 == nil and p2 == nil)
153
154                         p1, p2 = core.string_to_area("(10.0, 5,) -2  fgdf2,   4, -12.53")
155                         assert(p1 == nil and p2 == nil)
156                 end)
157         end)
158 end)
159
160 describe("table", function()
161         it("indexof()", function()
162                 assert.equal(1, table.indexof({"foo", "bar"}, "foo"))
163                 assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
164         end)
165 end)
166
167 describe("formspec_escape", function()
168         it("escapes", function()
169                 assert.equal(nil, core.formspec_escape(nil))
170                 assert.equal("", core.formspec_escape(""))
171                 assert.equal("\\[Hello\\\\\\[", core.formspec_escape("[Hello\\["))
172         end)
173 end)