removed key_instance

This commit is contained in:
VorTechnix 2023-07-12 10:51:41 -07:00
parent 6afc9be7ae
commit adab08ce40
No known key found for this signature in database
GPG key ID: 091E91A69545D5BA
2 changed files with 158 additions and 249 deletions

View file

@ -1,158 +1,158 @@
local Vector3 = require("worldeditadditions_core.utils.vector3") local Vector3 = require("worldeditadditions_core.utils.vector3")
local facing_dirs = dofile("./.tests/parse/axes/include_facing_dirs.lua") local facing_dirs = dofile("./.tests/parse/axes/include_facing_dirs.lua")
local parse = require("worldeditadditions_core.utils.parse.axes_parser") local parse = require("worldeditadditions_core.utils.parse.axes_parser")
local parse_axes = parse.keytable local parse_axes = parse.keytable
describe("parse_axes", function() describe("parse_axes", function()
-- Basic tests -- Basic tests
it("should work on single horizontal axes", function() it("should work on single horizontal axes", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"x", "3", "x", "3",
"-z", "10", "-z", "10",
}, facing_dirs.x_pos) }, facing_dirs.x_pos)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(0, 0, -10), minv) assert.are.same(Vector3.new(0, 0, -10), minv)
assert.are.same(Vector3.new(3, 0, 0), maxv) assert.are.same(Vector3.new(3, 0, 0), maxv)
end) end)
it("should handle axis clumps and orphan (universal) values", function() it("should handle axis clumps and orphan (universal) values", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"xz", "-3", "xz", "-3",
"10", "10",
}, facing_dirs.x_pos) }, facing_dirs.x_pos)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(-3, 0, -3), minv) assert.are.same(Vector3.new(-3, 0, -3), minv)
assert.are.same(Vector3.new(10, 10, 10), maxv) assert.are.same(Vector3.new(10, 10, 10), maxv)
end) end)
it("should work on directions and their abriviations", function() it("should work on directions and their abriviations", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"l", "3", -- +z "l", "3", -- +z
"-r", "-3", -- +z "-right", "-3", -- +z
"b", "-10", -- -x "b", "-10", -- -x
}, facing_dirs.x_pos) }, facing_dirs.x_pos)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(0, 0, -3), minv) assert.are.same(Vector3.new(0, 0, -3), minv)
assert.are.same(Vector3.new(10, 0, 3), maxv) assert.are.same(Vector3.new(10, 0, 3), maxv)
end) end)
it("should work with compass directions and their abriviations", function() it("should work with compass directions and their abriviations", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"n", "3", -- +z "n", "3", -- +z
"south", "3", -- -z "south", "3", -- -z
"-west", "10", -- +x "-west", "10", -- +x
}, facing_dirs.x_pos) }, facing_dirs.x_pos)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(0, 0, -3), minv) assert.are.same(Vector3.new(0, 0, -3), minv)
assert.are.same(Vector3.new(10, 0, 3), maxv) assert.are.same(Vector3.new(10, 0, 3), maxv)
end) end)
it("should work with ?", function() it("should work with ?", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"?", "3", -- -z "?", "3", -- -z
}, facing_dirs.z_neg) }, facing_dirs.z_neg)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(0, 0, -3), minv) assert.are.same(Vector3.new(0, 0, -3), minv)
assert.are.same(Vector3.new(0, 0, 0), maxv) assert.are.same(Vector3.new(0, 0, 0), maxv)
end) end)
it("should work with complex relative / absolute combinations", function() it("should work with complex relative / absolute combinations", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"f", "3", -- +x "f", "3", -- +x
"left", "10", -- +z "left", "10", -- +z
"y", "77", "y", "77",
"x", "30", "x", "30",
"back", "99", "back", "99",
}, facing_dirs.x_pos) }, facing_dirs.x_pos)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(-99, 0, 0), minv) assert.are.same(Vector3.new(-99, 0, 0), minv)
assert.are.same(Vector3.new(33, 77, 10), maxv) assert.are.same(Vector3.new(33, 77, 10), maxv)
end) end)
it("should work with complex relative / absolute combinations with negative facing_dirs", function() it("should work with complex relative / absolute combinations with negative facing_dirs", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"f", "3", -- -z "f", "3", -- -z
"l", "10", -- +x "l", "10", -- +x
"y", "77", "y", "77",
"x", "30", "x", "30",
"b", "99", -- +z "b", "99", -- +z
}, facing_dirs.z_neg) }, facing_dirs.z_neg)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(0, 0, -3), minv) assert.are.same(Vector3.new(0, 0, -3), minv)
assert.are.same(Vector3.new(40, 77, 99), maxv) assert.are.same(Vector3.new(40, 77, 99), maxv)
end) end)
it("should return 2 0,0,0 vectors if no input", function() it("should return 2 0,0,0 vectors if no input", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
-- No input -- No input
}, facing_dirs.z_neg) }, facing_dirs.z_neg)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(0, 0, 0), minv) assert.are.same(Vector3.new(0, 0, 0), minv)
assert.are.same(Vector3.new(0, 0, 0), maxv) assert.are.same(Vector3.new(0, 0, 0), maxv)
end) end)
-- Options tests -- Options tests
it("should mirror the max values of the two vectors if mirroring keyword is present", function() it("should mirror the max values of the two vectors if mirroring keyword is present", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"x", "3", "x", "3",
"f", "-5", -- +x "f", "-5", -- +x
"z", "-10", "z", "-10",
"mir", "mir",
}, facing_dirs.x_pos) }, facing_dirs.x_pos)
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(-5, 0, -10), minv) assert.are.same(Vector3.new(-5, 0, -10), minv)
assert.are.same(Vector3.new(5, 0, 10), maxv) assert.are.same(Vector3.new(5, 0, 10), maxv)
end) end)
it("should return a single vector if 'sum' input is truthy", function() it("should return a single vector if 'sum' input is truthy", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"x", "3", "x", "3",
"z", "-10", "z", "-10",
}, facing_dirs.x_pos,"sum") }, facing_dirs.x_pos,"sum")
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(3, 0, -10), minv) assert.are.same(Vector3.new(3, 0, -10), minv)
assert.are.same(nil, maxv) assert.are.same(nil, maxv)
end) end)
it("should dissable mirroring if 'sum' input is truthy", function() it("should dissable mirroring if 'sum' input is truthy", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"x", "3", "x", "3",
"f", "-5", -- +x "f", "-5", -- +x
"z", "-10", "z", "-10",
"sym", "sym",
}, facing_dirs.x_pos,"sum") }, facing_dirs.x_pos,"sum")
assert.is.truthy(minv) assert.is.truthy(minv)
assert.are.same(Vector3.new(-2, 0, -10), minv) assert.are.same(Vector3.new(-2, 0, -10), minv)
assert.are.same(nil, maxv) assert.are.same(nil, maxv)
end) end)
-- Error tests -- Error tests
it("should return error if bad axis/dir", function() it("should return error if bad axis/dir", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"f", "3", -- +x "f", "3", -- +x
"lift", "10", -- Invalid axis "lift", "10", -- Invalid axis
"y", "77", "y", "77",
"x", "30", "x", "30",
"back", "99", -- -x "back", "99", -- -x
}, facing_dirs.x_pos) }, facing_dirs.x_pos)
assert.are.same(false, minv) assert.are.same(false, minv)
assert.are.same("string", type(maxv)) assert.are.same("string", type(maxv))
end) end)
it("should return error if bad value", function() it("should return error if bad value", function()
local minv, maxv = parse_axes({ local minv, maxv = parse_axes({
"f", "3", -- +x "f", "3", -- +x
"left", "10", -- +z "left", "10", -- +z
"y", "!Q", -- Invalid value "y", "!Q", -- Invalid value
"x", "30", "x", "30",
"back", "99", "back", "99",
}, facing_dirs.x_pos) }, facing_dirs.x_pos)
assert.are.same(false, minv) assert.are.same(false, minv)
assert.are.same("string", type(maxv)) assert.are.same("string", type(maxv))
end) end)
end) end)

View file

@ -1,91 +0,0 @@
--- A container for transmitting (axis table, sign) or (dir, sign) pairs
-- and other data within parsing functions.
-- @internal
-- @class worldeditadditions_core.parse.key_instance
local key_instance = {}
key_instance.__index = key_instance
key_instance.__name = "Key Instance"
-- Allowed values for "type" field
local types = {
err = true, rev = true,
axis = true, dir = true,
replace = {
error = "err",
axes = "axis",
},
}
-- Simple function for putting stuff in quotes
local function enquote(take)
if type(take) == "string" then
return '"'..take..'"'
else return tostring(take) end
end
--- Creates a new Key Instance.
-- This is a table with a "type" string, an entry string or table
-- and an optional signed integer (or code number in the case of errors)
-- @param: type: String: Key type (axis, dir(ection), rev (mirroring) or error).
-- @param: entry: String: The main content of the key.
-- @param: sign: Int: The signed multiplier of the key (if any).
-- @return: Key Instance: The new Key Instance.
function key_instance.new(type,entry,sign)
if types.replace[type] then
type = types.replace[type]
elseif not types[type] then
return key_instance.new("err",
"Key Instance internal error: Invalid type "..enquote(type)..".",
500)
end
local tbl = {type = type, entry = entry}
if sign and sign ~= 0 then
if type == "err" then tbl.code = sign
else tbl.sign = sign end
end
return setmetatable(tbl, key_instance)
end
--- Checks if Key Instance "entry" field is a table.
-- @param: tbl: Key Instance: The Key Instance to check.
-- @return: Bool: Returns true if Key Instance has a non 0 sign value.
function key_instance.entry_table(a)
if type(a.entry) == "table" then
return true
else return false end
end
--- Checks if Key Instance has a signed multiplier.
-- @param: tbl: Key Instance: The Key Instance to check.
-- @return: Bool: Returns true if Key Instance has a non 0 sign value.
function key_instance.has_sign(a)
if not a.sign or a.sign == 0 then
return false
else return true end
end
--- Checks if Key Instance is an error.
-- @param: tbl: Key Instance: The Key Instance to check.
-- @return: Bool: Returns true if Key Instance is an error.
function key_instance.is_error(a)
if a.type == "err" then return true
else return false end
end
function key_instance.__tostring(a)
local ret = "{type = "..enquote(a.type)..", entry = "
if type(a.entry) == "table" and #a.entry <= 3 then
ret = ret.."{"
for _i,v in ipairs(a.entry) do
ret = ret..enquote(v)..", "
end
ret = ret:sub(1,-3).."}"
else ret = ret..enquote(a.entry) end
if a:is_error() and a.code then ret = ret..", code = "..a.code.."}"
elseif a:has_sign() then ret = ret..", sign = "..a.sign.."}"
else ret = ret.."}" end
return ret
end
return key_instance