aboutsummaryrefslogtreecommitdiff
path: root/builtin/common/tests/vector_spec.lua
diff options
context:
space:
mode:
authorDS <vorunbekannt75@web.de>2021-04-05 15:55:56 +0200
committerGitHub <noreply@github.com>2021-04-05 15:55:56 +0200
commit23325277659132e95b346307b591c944625bda16 (patch)
tree29523e7d4334630a302ab573810643e87e61f38a /builtin/common/tests/vector_spec.lua
parent19c283546c5418382ed3ab648ca165ef1cc7994b (diff)
downloadminetest-23325277659132e95b346307b591c944625bda16.tar.gz
minetest-23325277659132e95b346307b591c944625bda16.tar.bz2
minetest-23325277659132e95b346307b591c944625bda16.zip
Add vector.to_string and vector.from_string (#10323)
Writing vectors as strings is very common and should belong to `vector.*`. `minetest.pos_to_string` is also too long to write, implies that one should only use it for positions and leaves no spaces after the commas.
Diffstat (limited to 'builtin/common/tests/vector_spec.lua')
-rw-r--r--builtin/common/tests/vector_spec.lua19
1 files changed, 19 insertions, 0 deletions
diff --git a/builtin/common/tests/vector_spec.lua b/builtin/common/tests/vector_spec.lua
index 0f287363a..104c656e9 100644
--- a/builtin/common/tests/vector_spec.lua
+++ b/builtin/common/tests/vector_spec.lua
@@ -48,6 +48,25 @@ describe("vector", function()
assert.same({ x = 41, y = 52, z = 63 }, vector.offset(vector.new(1, 2, 3), 40, 50, 60))
end)
+ it("to_string()", function()
+ local v = vector.new(1, 2, 3.14)
+ assert.same("(1, 2, 3.14)", vector.to_string(v))
+ end)
+
+ it("from_string()", function()
+ local v = vector.new(1, 2, 3.14)
+ assert.same({v, 13}, {vector.from_string("(1, 2, 3.14)")})
+ assert.same({v, 12}, {vector.from_string("(1,2 ,3.14)")})
+ assert.same({v, 12}, {vector.from_string("(1,2,3.14,)")})
+ assert.same({v, 11}, {vector.from_string("(1 2 3.14)")})
+ assert.same({v, 15}, {vector.from_string("( 1, 2, 3.14 )")})
+ assert.same({v, 15}, {vector.from_string(" ( 1, 2, 3.14) ")})
+ assert.same({vector.new(), 8}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ")})
+ assert.same({v, 22}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ", 8)})
+ assert.same({v, 22}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ", 9)})
+ assert.same(nil, vector.from_string("nothing"))
+ end)
+
-- This function is needed because of floating point imprecision.
local function almost_equal(a, b)
if type(a) == "number" then