From 0f20e47e0ee99f424b6c66bbbef2fcef41c1a373 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Mon, 17 Jun 2024 22:06:53 +0200 Subject: [PATCH] Add unit tests --- apps/openmw/mwlua/mwscriptbindings.cpp | 6 +-- scripts/data/morrowind_tests/global.lua | 1 + .../data/morrowind_tests/global_mwscript.lua | 38 +++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 scripts/data/morrowind_tests/global_mwscript.lua diff --git a/apps/openmw/mwlua/mwscriptbindings.cpp b/apps/openmw/mwlua/mwscriptbindings.cpp index b85e72577d..2362ee488f 100644 --- a/apps/openmw/mwlua/mwscriptbindings.cpp +++ b/apps/openmw/mwlua/mwscriptbindings.cpp @@ -203,13 +203,13 @@ namespace MWLua if (index >= size) return sol::nullopt; auto i = index++; - if (i <= locals.mShorts.size()) + if (i < locals.mShorts.size()) return std::make_tuple(compilerLocals.get('s')[i], locals.mShorts[i]); i -= locals.mShorts.size(); - if (i <= locals.mLongs.size()) + if (i < locals.mLongs.size()) return std::make_tuple(compilerLocals.get('l')[i], locals.mLongs[i]); i -= locals.mLongs.size(); - if (i <= locals.mFloats.size()) + if (i < locals.mFloats.size()) return std::make_tuple(compilerLocals.get('f')[i], locals.mFloats[i]); return sol::nullopt; }); diff --git a/scripts/data/morrowind_tests/global.lua b/scripts/data/morrowind_tests/global.lua index cdc10c0059..fb7113d1b1 100644 --- a/scripts/data/morrowind_tests/global.lua +++ b/scripts/data/morrowind_tests/global.lua @@ -24,6 +24,7 @@ end local testModules = { 'global_issues', 'global_dialogues', + 'global_mwscript', } return { diff --git a/scripts/data/morrowind_tests/global_mwscript.lua b/scripts/data/morrowind_tests/global_mwscript.lua new file mode 100644 index 0000000000..f92833055a --- /dev/null +++ b/scripts/data/morrowind_tests/global_mwscript.lua @@ -0,0 +1,38 @@ +local testing = require('testing_util') +local core = require('openmw.core') +local world = require('openmw.world') + +function iterateOverVariables(variables) + local first = nil + local last = nil + local count = 0 + for k, _ in pairs(variables) do + first = first or k + last = k + count = count + 1 + end + return first, last, count +end + +return { + {'Should support iteration over script variables', function() + local mainVars = world.mwscript.getGlobalScript('main').variables + local first, last, count = iterateOverVariables(mainVars) + testing.expectEqual(first, nil) + testing.expectEqual(last, nil) + testing.expectEqual(count, 0) + testing.expectEqual(count, #mainVars) + + local jiub = world.getObjectByFormId(core.getFormId('Morrowind.esm', 172867)) + local jiubVars = world.mwscript.getLocalScript(jiub).variables + first, last, count = iterateOverVariables(jiubVars) + print(first, last, count) + testing.expectEqual(first, 'state') + testing.expectEqual(last, 'timer') + testing.expectEqual(count, 3) + testing.expectEqual(count, #jiubVars) + testing.expectEqual(jiubVars[1], jiubVars.state) + testing.expectEqual(jiubVars[2], jiubVars.wandering) + testing.expectEqual(jiubVars[3], jiubVars.timer) + end}, +}