Vector3: add equals, equality operator

This commit is contained in:
Starbeamrainbowlabs 2021-06-26 16:28:39 +01:00
parent cfa086ce46
commit 112bb96e99
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2
4 changed files with 67 additions and 12 deletions

@ -5,20 +5,16 @@ describe("Vector3.clone", function()
local a = Vector3.new(3, 4, 5) local a = Vector3.new(3, 4, 5)
local result = a:clone() local result = a:clone()
assert.are.same( result.x = 4
Vector3.new(3, 4, 5), assert.are.same(Vector3.new(3, 4, 5), a)
result assert.are.same(Vector3.new(4, 4, 5), result)
)
assert.are_not.equal(result, a)
end) end)
it("should return a new Vector3 instance for a different vector", function() it("should return a new Vector3 instance for a different vector", function()
local a = Vector3.new(-99, 66, 88) local a = Vector3.new(-99, 66, 88)
local result = a:clone() local result = a:clone()
assert.are.same( result.y = -44
Vector3.new(-99, 66, 88), assert.are.same(Vector3.new(-99, 66, 88), a)
result assert.are.same(Vector3.new(-99, -44, 88), result)
)
assert.are_not.equal(result, a)
end) end)
end) end)

@ -0,0 +1,40 @@
local Vector3 = require("worldeditadditions.utils.vector3")
describe("Vector3.equals", function()
it("should return true when identical", function()
local a = Vector3.new(3, 4, 5)
local b = Vector3.new(3, 4, 5)
assert.are.same(
true,
a:equals(b)
)
end)
it("should return false when not identical", function()
local a = Vector3.new(3, 4, 5)
local b = Vector3.new(6, 7, 8)
assert.are.same(
false,
a:equals(b)
)
end)
it("should return false when not identical x", function()
local a = Vector3.new(3, 4, 5)
local b = Vector3.new(4, 4, 5)
assert.are.same(
false,
a:equals(b)
)
end)
it("should return false when not identical y", function()
local a = Vector3.new(3, 4, 5)
local b = Vector3.new(3, 5, 5)
assert.are.same(
false,
a:equals(b)
)
end)
end)

@ -37,7 +37,9 @@ describe("Vector3.limit_to", function()
Vector3.new(80198051, 80198051, 80198051), Vector3.new(80198051, 80198051, 80198051),
result result
) )
assert.are_not.equal(result, a) a.x = 4
assert.are.same(Vector3.new(4, 801980510, 801980510), a)
assert.are.same(Vector3.new(80198051, 80198051, 80198051), result)
end) end)
it("should return a new Vector3 instance if the length is smaller", function() it("should return a new Vector3 instance if the length is smaller", function()
local a = Vector3.new(3, 4, 5) local a = Vector3.new(3, 4, 5)
@ -47,6 +49,8 @@ describe("Vector3.limit_to", function()
Vector3.new(3, 4, 5), Vector3.new(3, 4, 5),
result result
) )
assert.are_not.equal(result, a) a.x = 40
assert.are.same(Vector3.new(40, 4, 5), a)
assert.are.same(Vector3.new(3, 4, 5), result)
end) end)
end) end)

@ -174,6 +174,17 @@ function Vector3.dot_product(a, b)
return Vector3.dot(a, b) return Vector3.dot(a, b)
end end
--- Determines if 2 vectors are equal to each other.
-- 2 vectors are equal if their values are identical.
-- @param a Vector3 The first vector to test.
-- @param a Vector3 The second vector to test.
-- @returns bool Whether the 2 vectors are equal or not.
function Vector3.equals(a, b)
return a.x == b.x
and a.y == b.y
and a.z == b.z
end
--- Returns a new vector whose length clamped to the given length. --- Returns a new vector whose length clamped to the given length.
-- The direction in which the vector is pointing is not changed. -- The direction in which the vector is pointing is not changed.
-- @param a Vector3 The vector to operate on. -- @param a Vector3 The vector to operate on.
@ -238,6 +249,10 @@ function Vector3.__div(a, b)
return Vector3.divide(a, b) return Vector3.divide(a, b)
end end
function Vector3.__eq(a, b)
return Vector3.equals(a, b)
end
--- Returns the current Vector3 as a string. --- Returns the current Vector3 as a string.
function Vector3.__tostring(a) function Vector3.__tostring(a)
return "("..a.x..", "..a.y..", "..a.z..")" return "("..a.x..", "..a.y..", "..a.z..")"