Vector3: add equals, equality operator

This commit is contained in:
Starbeamrainbowlabs 2021-06-26 16:28:39 +01:00
parent cfa086ce46
commit 112bb96e99
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 67 additions and 12 deletions

View file

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

View file

@ -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)

View file

@ -37,7 +37,9 @@ describe("Vector3.limit_to", function()
Vector3.new(80198051, 80198051, 80198051),
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)
it("should return a new Vector3 instance if the length is smaller", function()
local a = Vector3.new(3, 4, 5)
@ -47,6 +49,8 @@ describe("Vector3.limit_to", function()
Vector3.new(3, 4, 5),
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)

View file

@ -174,6 +174,17 @@ function Vector3.dot_product(a, b)
return Vector3.dot(a, b)
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.
-- The direction in which the vector is pointing is not changed.
-- @param a Vector3 The vector to operate on.
@ -238,6 +249,10 @@ function Vector3.__div(a, b)
return Vector3.divide(a, b)
end
function Vector3.__eq(a, b)
return Vector3.equals(a, b)
end
--- Returns the current Vector3 as a string.
function Vector3.__tostring(a)
return "("..a.x..", "..a.y..", "..a.z..")"