2 dofile("builtin/common/vector.lua")
3 dofile("builtin/common/misc_helpers.lua")
5 describe("string", function()
6 it("trim()", function()
7 assert.equal("foo bar", string.trim("\n \t\tfoo bar\t "))
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", ":|"))
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 | :", ":|"))
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))
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))
42 it("rejects empty separator", function()
43 assert.has.errors(function()
50 describe("privs", function()
51 it("from string", function()
52 assert.same({ a = true, b = true }, core.string_to_privs("a,b"))
55 it("to string", function()
56 assert.equal("one", core.privs_to_string({ one=true }))
58 local ret = core.privs_to_string({ a=true, b=true })
59 assert(ret == "a,b" or ret == "b,a")
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)"))
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}))
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
128 p1, p2 = core.string_to_area("(nan,nan,nan) (nan,nan,nan)")
129 assert(p1 == nil and p2 == nil)
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)
134 p1, p2 = core.string_to_area("(inf,inf,inf) (-inf,-inf,-inf)")
135 assert(p1 == nil and p2 == nil)
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)
142 p1, p2 = core.string_to_area("bananas", "foobar")
143 assert(p1 == nil and p2 == nil)
145 p1, p2 = core.string_to_area("bananas")
146 assert(p1 == nil and p2 == nil)
148 p1, p2 = core.string_to_area("(bananas,bananas,bananas)")
149 assert(p1 == nil and p2 == nil)
151 p1, p2 = core.string_to_area("(bananas,bananas,bananas) (bananas,bananas,bananas)")
152 assert(p1 == nil and p2 == nil)
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)
159 p1, p2 = core.string_to_area("(10.0, 5,) -2 fgdf2, 4, -12.53")
160 assert(p1 == nil and p2 == nil)
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"))
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\\["))