From d9230feae0bd47b51227470ce5ee69e3a4ab6e91 Mon Sep 17 00:00:00 2001 From: lL1l1 <82986251+lL1l1@users.noreply.github.com> Date: Sun, 22 Dec 2024 01:07:27 -0800 Subject: [PATCH] Try fixing tests --- tests/blueprint/unit.spec.lua | 2 ++ tests/utility/color.spec.lua | 2 ++ tests/utility/fafMockLibrary.lua | 42 ++++++++++++++++++++++++++++++ tests/utility/string.spec.lua | 44 ++------------------------------ 4 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 tests/utility/fafMockLibrary.lua diff --git a/tests/blueprint/unit.spec.lua b/tests/blueprint/unit.spec.lua index 1b613d67a8..c0ef8f0ff7 100644 --- a/tests/blueprint/unit.spec.lua +++ b/tests/blueprint/unit.spec.lua @@ -20,6 +20,8 @@ --** SOFTWARE. --****************************************************************************************************** +require("/tests/utility/fafMockLibrary.lua") + ---@type Luft local luft = require "./tests/packages/luft" diff --git a/tests/utility/color.spec.lua b/tests/utility/color.spec.lua index d95093533e..1a59f6bee7 100644 --- a/tests/utility/color.spec.lua +++ b/tests/utility/color.spec.lua @@ -2,6 +2,8 @@ --** Shared under the MIT license --************************************************************************************************** +require "./tests/utility/fafMockLibrary.lua" + local luft = require "./tests/packages/luft" -- color library diff --git a/tests/utility/fafMockLibrary.lua b/tests/utility/fafMockLibrary.lua new file mode 100644 index 0000000000..04c64ea8ea --- /dev/null +++ b/tests/utility/fafMockLibrary.lua @@ -0,0 +1,42 @@ +-- Vector2 is needed in utils but not provided outside the game, so it has to be created here +local Vector2Meta = { + __index = function(t, k) + if k == 'x' then + return t[1] + elseif k == 'y' then + return t[2] + elseif k == 'z' then + return t[3] + else + error("bad argument #2 to `?' ('x', 'y', or 'z' expected)", 1) + end + end, + + __newindex = function(t, k, v) + if k == 'x' then + t[1] = v + elseif k == 'y' then + t[2] = v + elseif k == 'z' then + t[3] = v + else + error("bad argument #2 to `?' ('x', 'y', or 'z' expected)", 1) + end + end, +} + +Vector2 = function(...) + if arg.n ~= 2 then + error("expected 2 args, but got " .. arg.n) + end + if not type(arg[1]) == "number" then + error("number expected but got " .. type(arg[1])) + end + if not type(arg[2]) == "number" then + error("number expected but got " .. type(arg[2])) + end + + local newVector2 = {arg[1], arg[2]} + setmetatable(newVector2, Vector2Meta) + return newVector2 +end diff --git a/tests/utility/string.spec.lua b/tests/utility/string.spec.lua index 07c361d047..ec1c058558 100644 --- a/tests/utility/string.spec.lua +++ b/tests/utility/string.spec.lua @@ -1,48 +1,8 @@ +require("/tests/utility/fafMockLibrary.lua") + -- Test framework local luft = require "./tests/packages/luft" --- Vector2 is needed in utils but not provided outside the game, so it has to be created here -local Vector2Meta = { - __index = function(t, k) - if k == 'x' then - return t[1] - elseif k == 'y' then - return t[2] - elseif k == 'z' then - return t[3] - else - error("bad argument #2 to `?' ('x', 'y', or 'z' expected)", 1) - end - end, - - __newindex = function(t, k, v) - if k == 'x' then - t[1] = v - elseif k == 'y' then - t[2] = v - elseif k == 'z' then - t[3] = v - else - error("bad argument #2 to `?' ('x', 'y', or 'z' expected)", 1) - end - end, -} -Vector2 = function(...) - if arg.n ~= 2 then - error("expected 2 args, but got " .. arg.n) - end - if not type(arg[1]) == "number" then - error("number expected but got " .. type(arg[1])) - end - if not type(arg[2]) == "number" then - error("number expected but got " .. type(arg[2])) - end - - local newVector2 = {arg[1], arg[2]} - setmetatable(newVector2, Vector2Meta) - return newVector2 -end - -- These functions are imported to the global scope in globalInit and RuleInit require "./lua/system/utils.lua"