misc_helpers_spec.lua 6.2 KB

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