]> git.lizzy.rs Git - minetest.git/blob - builtin/common/tests/misc_helpers_spec.lua
Error when string.split is given empty separator (#13132)
[minetest.git] / builtin / common / tests / misc_helpers_spec.lua
1 _G.core = {}
2 dofile("builtin/common/vector.lua")
3 dofile("builtin/common/misc_helpers.lua")
4
5 describe("string", function()
6         it("trim()", function()
7                 assert.equal("foo bar", string.trim("\n \t\tfoo bar\t "))
8         end)
9
10         describe("split()", function()
11                 it("removes empty", function()
12                         assert.same({ "hello" }, string.split("hello"))
13                         assert.same({ "hello", "world" }, string.split("hello,world"))
14                         assert.same({ "hello", "world" }, string.split("hello,world,,,"))
15                         assert.same({ "hello", "world" }, string.split(",,,hello,world"))
16                         assert.same({ "hello", "world", "2" }, string.split("hello,,,world,2"))
17                         assert.same({ "hello ", " world" }, string.split("hello :| world", ":|"))
18                 end)
19
20                 it("keeps empty", function()
21                         assert.same({ "hello" }, string.split("hello", ",", true))
22                         assert.same({ "hello", "world" }, string.split("hello,world", ",", true))
23                         assert.same({ "hello", "world", "" }, string.split("hello,world,", ",", true))
24                         assert.same({ "hello", "", "", "world", "2" }, string.split("hello,,,world,2", ",", true))
25                         assert.same({ "", "", "hello", "world", "2" }, string.split(",,hello,world,2", ",", true))
26                         assert.same({ "hello ", " world | :" }, string.split("hello :| world | :", ":|"))
27                 end)
28
29                 it("max_splits", function()
30                         assert.same({ "one" }, string.split("one", ",", true, 2))
31                         assert.same({ "one,two,three,four" }, string.split("one,two,three,four", ",", true, 0))
32                         assert.same({ "one", "two", "three,four" }, string.split("one,two,three,four", ",", true, 2))
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", ",", false, 2))
35                 end)
36
37                 it("pattern", function()
38                         assert.same({ "one", "two" }, string.split("one,two", ",", false, -1, true))
39                         assert.same({ "one", "two", "three" }, string.split("one2two3three", "%d", false, -1, true))
40                 end)
41
42                 it("rejects empty separator", function()
43                         assert.has.errors(function()
44                                 string.split("", "")
45                         end)
46                 end)
47         end)
48 end)
49
50 describe("privs", function()
51         it("from string", function()
52                 assert.same({ a = true, b = true }, core.string_to_privs("a,b"))
53         end)
54
55         it("to string", function()
56                 assert.equal("one", core.privs_to_string({ one=true }))
57
58                 local ret = core.privs_to_string({ a=true, b=true })
59                 assert(ret == "a,b" or ret == "b,a")
60         end)
61 end)
62
63 describe("pos", function()
64         it("from string", function()
65                 assert.equal(vector.new(10, 5.1, -2), core.string_to_pos("10.0, 5.1, -2"))
66                 assert.equal(vector.new(10, 5.1, -2), core.string_to_pos("( 10.0, 5.1, -2)"))
67                 assert.is_nil(core.string_to_pos("asd, 5, -2)"))
68         end)
69
70         it("to string", function()
71                 assert.equal("(10.1,5.2,-2.3)", core.pos_to_string({ x = 10.1, y = 5.2, z = -2.3}))
72         end)
73 end)
74
75 describe("area parsing", function()
76         describe("valid inputs", function()
77                 it("accepts absolute numbers", function()
78                         local p1, p2 = core.string_to_area("(10.0, 5, -2) (  30.2 4 -12.53)")
79                         assert(p1.x == 10 and p1.y == 5 and p1.z == -2)
80                         assert(p2.x == 30.2 and p2.y == 4 and p2.z == -12.53)
81                 end)
82
83                 it("accepts relative numbers", function()
84                         local 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
89                         p1, p2 = core.string_to_area("(1 2 3) (~5 ~-5 ~)", {x=10,y=10,z=10})
90                         assert(type(p1) == "table" and type(p2) == "table")
91                         assert(p1.x == 1 and p1.y == 2 and p1.z == 3)
92                         assert(p2.x == 15 and p2.y == 5 and p2.z == 10)
93                 end)
94         end)
95         describe("invalid inputs", function()
96                 it("rejects too few numbers", function()
97                         local p1, p2 = core.string_to_area("(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 too many numbers", function()
102                         local p1, p2 = core.string_to_area("(1,1,1,1) (1,1,1,1)", {x=1,y=1,z=1})
103                         assert(p1 == nil and p2 == nil)
104                 end)
105
106                 it("rejects nan & inf", function()
107                         local p1, p2 = core.string_to_area("(1,1,1) (1,1,nan)", {x=1,y=1,z=1})
108                         assert(p1 == nil and p2 == nil)
109
110                         p1, p2 = core.string_to_area("(1,1,1) (1,1,~nan)", {x=1,y=1,z=1})
111                         assert(p1 == nil and p2 == nil)
112
113                         p1, p2 = core.string_to_area("(1,1,1) (1,~nan,1)", {x=1,y=1,z=1})
114                         assert(p1 == nil and p2 == nil)
115
116                         p1, p2 = core.string_to_area("(1,1,1) (1,1,inf)", {x=1,y=1,z=1})
117                         assert(p1 == nil and p2 == nil)
118
119                         p1, p2 = core.string_to_area("(1,1,1) (1,1,~inf)", {x=1,y=1,z=1})
120                         assert(p1 == nil and p2 == nil)
121
122                         p1, p2 = core.string_to_area("(1,1,1) (1,~inf,1)", {x=1,y=1,z=1})
123                         assert(p1 == nil and p2 == nil)
124
125                         p1, p2 = core.string_to_area("(nan,nan,nan) (nan,nan,nan)", {x=1,y=1,z=1})
126                         assert(p1 == nil and p2 == nil)
127
128                         p1, p2 = core.string_to_area("(nan,nan,nan) (nan,nan,nan)")
129                         assert(p1 == nil and p2 == nil)
130
131                         p1, p2 = core.string_to_area("(inf,inf,inf) (-inf,-inf,-inf)", {x=1,y=1,z=1})
132                         assert(p1 == nil and p2 == nil)
133
134                         p1, p2 = core.string_to_area("(inf,inf,inf) (-inf,-inf,-inf)")
135                         assert(p1 == nil and p2 == nil)
136                 end)
137
138                 it("rejects words", function()
139                         local p1, p2 = core.string_to_area("bananas", {x=1,y=1,z=1})
140                         assert(p1 == nil and p2 == nil)
141
142                         p1, p2 = core.string_to_area("bananas", "foobar")
143                         assert(p1 == nil and p2 == nil)
144
145                         p1, p2 = core.string_to_area("bananas")
146                         assert(p1 == nil and p2 == nil)
147
148                         p1, p2 = core.string_to_area("(bananas,bananas,bananas)")
149                         assert(p1 == nil and p2 == nil)
150
151                         p1, p2 = core.string_to_area("(bananas,bananas,bananas) (bananas,bananas,bananas)")
152                         assert(p1 == nil and p2 == nil)
153                 end)
154
155                 it("requires parenthesis & valid numbers", function()
156                         local p1, p2 = core.string_to_area("(10.0, 5, -2  30.2,   4, -12.53")
157                         assert(p1 == nil and p2 == nil)
158
159                         p1, p2 = core.string_to_area("(10.0, 5,) -2  fgdf2,   4, -12.53")
160                         assert(p1 == nil and p2 == nil)
161                 end)
162         end)
163 end)
164
165 describe("table", function()
166         it("indexof()", function()
167                 assert.equal(1, table.indexof({"foo", "bar"}, "foo"))
168                 assert.equal(-1, table.indexof({"foo", "bar"}, "baz"))
169         end)
170 end)
171
172 describe("formspec_escape", function()
173         it("escapes", function()
174                 assert.equal(nil, core.formspec_escape(nil))
175                 assert.equal("", core.formspec_escape(""))
176                 assert.equal("\\[Hello\\\\\\[", core.formspec_escape("[Hello\\["))
177         end)
178 end)