From b80543c24228dd7cb33390bb49bc97c8a9760ef1 Mon Sep 17 00:00:00 2001 From: mcc Date: Fri, 29 Nov 2019 14:44:34 -0500 Subject: [PATCH] Make vec2(vec2(1,2)) and vec3(vec3(1,2,3)) consistent between lua and luajit --- modules/vec2.lua | 2 +- modules/vec3.lua | 2 +- spec/vec2_spec.lua | 15 +++++++++++++++ spec/vec3_spec.lua | 6 ++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/vec2.lua b/modules/vec2.lua index cfc0653..01a0b97 100644 --- a/modules/vec2.lua +++ b/modules/vec2.lua @@ -54,7 +54,7 @@ function vec2.new(x, y) return new(x, y) -- {x, y} or {x=x, y=y} - elseif type(x) == "table" then + elseif type(x) == "table" or type(x) == "cdata" then -- table in vanilla lua, cdata in luajit local xx, yy = x.x or x[1], x.y or x[2] assert(type(xx) == "number", "new: Wrong argument type for x ( expected)") assert(type(yy) == "number", "new: Wrong argument type for y ( expected)") diff --git a/modules/vec3.lua b/modules/vec3.lua index 97b7f80..bf3d6bd 100644 --- a/modules/vec3.lua +++ b/modules/vec3.lua @@ -55,7 +55,7 @@ function vec3.new(x, y, z) return new(x, y, z) -- {x, y, z} or {x=x, y=y, z=z} - elseif type(x) == "table" then + elseif type(x) == "table" or type(x) == "cdata" then -- table in vanilla lua, cdata in luajit local xx, yy, zz = x.x or x[1], x.y or x[2], x.z or x[3] assert(type(xx) == "number", "new: Wrong argument type for x ( expected)") assert(type(yy) == "number", "new: Wrong argument type for y ( expected)") diff --git a/spec/vec2_spec.lua b/spec/vec2_spec.lua index bdab0bb..8b826ad 100644 --- a/spec/vec2_spec.lua +++ b/spec/vec2_spec.lua @@ -41,6 +41,12 @@ describe("vec2:", function() assert.is.equal(a, b) end) + it("clones a vector using the constructor", function() + local a = vec2(3, 5) + local b = vec2(a) + assert.is.equal(a, b) + end) + it("adds a vector to another", function() local a = vec2(3, 5) local b = vec2(7, 4) @@ -182,4 +188,13 @@ describe("vec2:", function() local b = a:to_string() assert.is.equal("(+0.000,+0.000)", b) end) + + -- Do this last, to insulate tests from accidental state contamination + it("converts a vec3 to vec2 using the constructor", function() + local vec3 = require "modules.vec3" + local a = vec2(3, 5) + local b = vec3(3, 5, 7) + local c = vec2(b) + assert.is.equal(a, c) + end) end) diff --git a/spec/vec3_spec.lua b/spec/vec3_spec.lua index fcda592..79eca55 100644 --- a/spec/vec3_spec.lua +++ b/spec/vec3_spec.lua @@ -46,6 +46,12 @@ describe("vec3:", function() assert.is.equal(a, b) end) + it("clones a vector using the constructor", function() + local a = vec3(3, 5, 7) + local b = vec3(a) + assert.is.equal(a, b) + end) + it("adds a vector to another", function() local a = vec3(3, 5, 7) local b = vec3(7, 4, 1)