From 96c3ede36538bfd01c793722a04824adc0442b12 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 7 Nov 2021 18:04:18 +0000 Subject: [PATCH] parse_axes: bugfix, start writing tests --- .../parse/axes/parse_abs_axis_name.test.lua | 88 +++++++++++++++++++ worldeditadditions/utils/parse/axes.lua | 24 +++-- worldeditadditions/utils/parse/init.lua | 5 +- 3 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 .tests/parse/axes/parse_abs_axis_name.test.lua diff --git a/.tests/parse/axes/parse_abs_axis_name.test.lua b/.tests/parse/axes/parse_abs_axis_name.test.lua new file mode 100644 index 0000000..1d33ce6 --- /dev/null +++ b/.tests/parse/axes/parse_abs_axis_name.test.lua @@ -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) diff --git a/worldeditadditions/utils/parse/axes.lua b/worldeditadditions/utils/parse/axes.lua index 3af6496..afb0921 100644 --- a/worldeditadditions/utils/parse/axes.lua +++ b/worldeditadditions/utils/parse/axes.lua @@ -1,6 +1,10 @@ -local wea = worldeditadditions -local Vector3 = dofile(wea.modpath.."/utils/vector3.lua") - +local Vector3 +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) @@ -35,6 +39,10 @@ local function parse_abs_axis_name(axis_name) if axis_name:match("-z") then result.z = -1 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 end @@ -79,9 +87,8 @@ end --- 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" -- 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. --- Uses wea.parse.axis for parsing axis names. -- @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). -- @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 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 +} diff --git a/worldeditadditions/utils/parse/init.lua b/worldeditadditions/utils/parse/init.lua index dc01104..2308fe8 100644 --- a/worldeditadditions/utils/parse/init.lua +++ b/worldeditadditions/utils/parse/init.lua @@ -4,8 +4,11 @@ -- ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ███████ ███████ +local axes = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") + 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")