2 _G.vector = {metatable = {}}
3 dofile("builtin/common/vector.lua")
4 dofile("builtin/common/misc_helpers.lua")
6 describe("string", function()
7 it("trim()", function()
8 assert.equal("foo bar", string.trim("\n \t\tfoo bar\t "))
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", ":|"))
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 | :", ":|"))
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))
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))
45 describe("privs", function()
46 it("from string", function()
47 assert.same({ a = true, b = true }, core.string_to_privs("a,b"))
50 it("to string", function()
51 assert.equal("one", core.privs_to_string({ one=true }))
53 local ret = core.privs_to_string({ a=true, b=true })
54 assert(ret == "a,b" or ret == "b,a")
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)"))
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}))
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
123 p1, p2 = core.string_to_area("(nan,nan,nan) (nan,nan,nan)")
124 assert(p1 == nil and p2 == nil)
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)
129 p1, p2 = core.string_to_area("(inf,inf,inf) (-inf,-inf,-inf)")
130 assert(p1 == nil and p2 == nil)
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)
137 p1, p2 = core.string_to_area("bananas", "foobar")
138 assert(p1 == nil and p2 == nil)
140 p1, p2 = core.string_to_area("bananas")
141 assert(p1 == nil and p2 == nil)
143 p1, p2 = core.string_to_area("(bananas,bananas,bananas)")
144 assert(p1 == nil and p2 == nil)
146 p1, p2 = core.string_to_area("(bananas,bananas,bananas) (bananas,bananas,bananas)")
147 assert(p1 == nil and p2 == nil)
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)
154 p1, p2 = core.string_to_area("(10.0, 5,) -2 fgdf2, 4, -12.53")
155 assert(p1 == nil and p2 == nil)
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"))
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\\["))