misc_helpers_spec.lua 7.1 KB

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