parse_axes: bugfix, start writing tests

This commit is contained in:
Starbeamrainbowlabs 2021-11-07 18:04:18 +00:00
parent 3f5fc45e1a
commit 96c3ede365
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 110 additions and 7 deletions

View file

@ -0,0 +1,88 @@
local Vector3 = require("worldeditadditions.utils.vector3")
local axes = require("worldeditadditions.utils.parse.axes")
local parse_abs_axis_name = axes.parse_abs_axis_name
describe("parse_abs_axis_name", function()
it("should work with positive x", function()
local success, result = parse_abs_axis_name("x")
assert.is_true(success)
assert.are.same(
Vector3.new(1, 0, 0),
result
)
end)
it("should work with negative x", function()
local success, result = parse_abs_axis_name("-x")
assert.is_true(success)
assert.are.same(
Vector3.new(-1, 0, 0),
result
)
end)
it("should work with positive y", function()
local success, result = parse_abs_axis_name("y")
assert.is_true(success)
assert.are.same(
Vector3.new(0, 1, 0),
result
)
end)
it("should work with negative y", function()
local success, result = parse_abs_axis_name("-y")
assert.is_true(success)
assert.are.same(
Vector3.new(0, -1, 0),
result
)
end)
it("should work with positive z", function()
local success, result = parse_abs_axis_name("z")
assert.is_true(success)
assert.are.same(
Vector3.new(0, 0, 1),
result
)
end)
it("should work with negative x", function()
local success, result = parse_abs_axis_name("-z")
assert.is_true(success)
assert.are.same(
Vector3.new(0, 0, -1),
result
)
end)
it("returns an error with invalid input", function()
local success, result = parse_abs_axis_name("cheese")
assert.is_false(success)
assert.are.same(
"string",
type(result)
)
end)
it("returns an error with no input", function()
local success, result = parse_abs_axis_name()
assert.is_false(success)
assert.are.same(
"string",
type(result)
)
end)
it("returns an error with input of the wrong type", function()
local success, result = parse_abs_axis_name(5)
assert.is_false(success)
assert.are.same(
"string",
type(result)
)
end)
it("returns an error with input of the wrong type again", function()
local success, result = parse_abs_axis_name({ "yay", "tests are very useful" })
assert.is_false(success)
assert.are.same(
"string",
type(result)
)
end)
end)

View file

@ -1,6 +1,10 @@
local wea = worldeditadditions local Vector3
local Vector3 = dofile(wea.modpath.."/utils/vector3.lua") if worldeditadditions then
local wea = worldeditadditions
Vector3 = dofile(wea.modpath.."/utils/vector3.lua")
else
Vector3 = require("worldeditadditions.utils.vector3")
end
--[[ --[[
parse_axes("6",name) == return Vector3.new(6,6,6), Vector3.new(-6,-6,-6) parse_axes("6",name) == return Vector3.new(6,6,6), Vector3.new(-6,-6,-6)
@ -35,6 +39,10 @@ local function parse_abs_axis_name(axis_name)
if axis_name:match("-z") then result.z = -1 if axis_name:match("-z") then result.z = -1
elseif axis_name:match("z") then result.z = 1 end elseif axis_name:match("z") then result.z = 1 end
if Vector3.new() == result then
return false, "Error: Unknown axis_name '"..axis_name.."'."
end
return true, result return true, result
end end
@ -79,9 +87,8 @@ end
--- Parses a token list of axes and counts into a Vector3. --- Parses a token list of axes and counts into a Vector3.
-- For example, "x 4" would become { x = 4, y = 0, z = 0 }, and "? 4 -z 10" -- For example, "x 4" would become { x = 4, y = 0, z = 0 }, and "? 4 -z 10"
-- might become { x = 4, y = 0, z = -10 }. -- might become { x = 4, y = 0, z = -10 }.
-- Note that the input here needs to be *pre split*. wea.split_shell is -- Note that the input here needs to be *post split*. wea.split_shell is
-- recommended for this purpose. -- recommended for this purpose.
-- Uses wea.parse.axis for parsing axis names.
-- @param token_list string[] A list of tokens to parse -- @param token_list string[] A list of tokens to parse
-- @param facing_dir PlayerDir The direction the player is facing. Returned from wea.player_dir(name). -- @param facing_dir PlayerDir The direction the player is facing. Returned from wea.player_dir(name).
-- @returns Vector3,Vector3 A Vector3 pair generated from parsing out the input token list representing the delta change that can be applied to a defined pos1, pos2 region. -- @returns Vector3,Vector3 A Vector3 pair generated from parsing out the input token list representing the delta change that can be applied to a defined pos1, pos2 region.
@ -143,4 +150,9 @@ local function parse_axes(token_list, facing_dir)
return true, pos1, pos2 return true, pos1, pos2
end end
return parse_axes return {
parse_axes = parse_axes,
parse_axis_name = parse_axis_name,
parse_abs_axis_name = parse_abs_axis_name,
parse_relative_axis_name = parse_relative_axis_name
}

View file

@ -4,8 +4,11 @@
-- ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██
-- ██ ██ ██ ██ ██ ███████ ███████ -- ██ ██ ██ ██ ██ ███████ ███████
local axes = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua")
worldeditadditions.parse = { worldeditadditions.parse = {
axes = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") axes = axes.parse_axes,
axis_name = axes.parse_axis_name
} }
dofile(worldeditadditions.modpath.."/utils/parse/chance.lua") dofile(worldeditadditions.modpath.."/utils/parse/chance.lua")