mudlet/test/TLuaInterfaceTest.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
3.1 KiB
C++
Raw Permalink Normal View History

/***************************************************************************
* Copyright (C) 2021 by Chris Mitchell - chrismit7@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <LuaInterface.h>
#include <TVar.h>
#include <VarUnit.h>
#include <QtTest/QtTest>
infrastructure: release resources in test fixture destructors (#9522) #### Brief overview of PR changes/additions - Convert the raw owning `LuaInterface*` member in the `TLuaInterfaceTest` and `TVariableEditorTest` Qt-Test fixtures to `std::unique_ptr<LuaInterface>` so the fixture's destructor frees it. - `TLuaInterfaceTest` also stops allocating the interface (and a `lua_State`) twice: members now init to `nullptr`/empty and are allocated only in `init()`; `cleanup()` resets the interface before closing the `lua_State`. #### Motivation for adding to Mudlet Keeps the test suite leak-clean and clears static-analysis warnings, per CLAUDE.md's "smart pointers for non-Qt classes". #### Other info (issues closed, discussion etc) - Clears 2 CodeQL `cpp/resource-not-released-in-destructor` warnings (`test/TVariableEditorTest.cpp`, `test/TLuaInterfaceTest.cpp`). - Also removes a real runtime leak in `TLuaInterfaceTest`: the old fixture never deleted the `interface` and double-allocated it (construction + `init()`), leaking a `LuaInterface` per test plus a construction-time `lua_State`/`LuaInterface`. Verified gone under LeakSanitizer (old binary leaked, new binary is leak-clean; `TVariableEditorTest` already deleted its interface so for it this is modernization). **Test case:** Build and run `ctest -R 'TLuaInterfaceTest|TVariableEditorTest'` - both pass (TLuaInterfaceTest 4/4, TVariableEditorTest 96 passed/13 skipped). Optionally run `./test/TLuaInterfaceTest` with `ASAN_OPTIONS=detect_leaks=1` to confirm no leaks are reported.
2026-07-29 13:45:22 +02:00
#include <memory>
extern "C" {
Fix: ensure we include the right Lua header files (#7842) #### Brief overview of PR changes/additions For Windows builds modify the `#include` lines for Lua header files to specify the 5.1 version. Also accommodate some changes in our CI build environment: * For some reason (maybe because of a more modern linker) we need to specify the original PCRE library with `-lpcre` rather than `-lpcre-1` - the exact cause of this is not clear but thanks to @jmckisson for finding it (and using it in his attempt to solve the same problems this PR is doing). * It seems the Window building is now being done in the `C:` drive rather than the previous `D:` one, so a tweak to clean the colon containing file-system root specifier to the alternative that MSYS2+Mingw-w64 uses which instead uses a (POSIX) `/` root directory followed by a single lower-case letter to specify the drive needs to be extended to handle both drives. This is because the scripts use `rsync` and that treats any `:` as the separator between host and path and gets confused when it sees a "Windows" path containing it! #### Motivation for adding to Mudlet The default version - and the one needed for some packages like Luarocks is a 5.4 one - and that includes header files in the "default" `include` directory. So the headers that get pulled in are the wrong ones, which fail to work as they are not compatible with Lua 5.1; to get the 5.1 instead I believe we need to explicitly include the version specific sub-directory in the `#include` lines. Other tweaks are also needed "to get things working nowadays." #### Other info (issues closed, discussion etc) This should be simpler to do than what is being attempted by #7841. --------- Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
2025-05-17 22:34:05 +01:00
#if defined(INCLUDE_VERSIONED_LUA_HEADERS)
#include <lua5.1/lauxlib.h>
#include <lua5.1/lua.h>
#include <lua5.1/lualib.h>
#else
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#endif
}
Fix: ensure we include the right Lua header files (#7842) #### Brief overview of PR changes/additions For Windows builds modify the `#include` lines for Lua header files to specify the 5.1 version. Also accommodate some changes in our CI build environment: * For some reason (maybe because of a more modern linker) we need to specify the original PCRE library with `-lpcre` rather than `-lpcre-1` - the exact cause of this is not clear but thanks to @jmckisson for finding it (and using it in his attempt to solve the same problems this PR is doing). * It seems the Window building is now being done in the `C:` drive rather than the previous `D:` one, so a tweak to clean the colon containing file-system root specifier to the alternative that MSYS2+Mingw-w64 uses which instead uses a (POSIX) `/` root directory followed by a single lower-case letter to specify the drive needs to be extended to handle both drives. This is because the scripts use `rsync` and that treats any `:` as the separator between host and path and gets confused when it sees a "Windows" path containing it! #### Motivation for adding to Mudlet The default version - and the one needed for some packages like Luarocks is a 5.4 one - and that includes header files in the "default" `include` directory. So the headers that get pulled in are the wrong ones, which fail to work as they are not compatible with Lua 5.1; to get the 5.1 instead I believe we need to explicitly include the version specific sub-directory in the `#include` lines. Other tweaks are also needed "to get things working nowadays." #### Other info (issues closed, discussion etc) This should be simpler to do than what is being attempted by #7841. --------- Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
2025-05-17 22:34:05 +01:00
infrastructure: release resources in test fixture destructors (#9522) #### Brief overview of PR changes/additions - Convert the raw owning `LuaInterface*` member in the `TLuaInterfaceTest` and `TVariableEditorTest` Qt-Test fixtures to `std::unique_ptr<LuaInterface>` so the fixture's destructor frees it. - `TLuaInterfaceTest` also stops allocating the interface (and a `lua_State`) twice: members now init to `nullptr`/empty and are allocated only in `init()`; `cleanup()` resets the interface before closing the `lua_State`. #### Motivation for adding to Mudlet Keeps the test suite leak-clean and clears static-analysis warnings, per CLAUDE.md's "smart pointers for non-Qt classes". #### Other info (issues closed, discussion etc) - Clears 2 CodeQL `cpp/resource-not-released-in-destructor` warnings (`test/TVariableEditorTest.cpp`, `test/TLuaInterfaceTest.cpp`). - Also removes a real runtime leak in `TLuaInterfaceTest`: the old fixture never deleted the `interface` and double-allocated it (construction + `init()`), leaking a `LuaInterface` per test plus a construction-time `lua_State`/`LuaInterface`. Verified gone under LeakSanitizer (old binary leaked, new binary is leak-clean; `TVariableEditorTest` already deleted its interface so for it this is modernization). **Test case:** Build and run `ctest -R 'TLuaInterfaceTest|TVariableEditorTest'` - both pass (TLuaInterfaceTest 4/4, TVariableEditorTest 96 passed/13 skipped). Optionally run `./test/TLuaInterfaceTest` with `ASAN_OPTIONS=detect_leaks=1` to confirm no leaks are reported.
2026-07-29 13:45:22 +02:00
class TVarTest : public QObject
{
Q_OBJECT
private:
infrastructure: release resources in test fixture destructors (#9522) #### Brief overview of PR changes/additions - Convert the raw owning `LuaInterface*` member in the `TLuaInterfaceTest` and `TVariableEditorTest` Qt-Test fixtures to `std::unique_ptr<LuaInterface>` so the fixture's destructor frees it. - `TLuaInterfaceTest` also stops allocating the interface (and a `lua_State`) twice: members now init to `nullptr`/empty and are allocated only in `init()`; `cleanup()` resets the interface before closing the `lua_State`. #### Motivation for adding to Mudlet Keeps the test suite leak-clean and clears static-analysis warnings, per CLAUDE.md's "smart pointers for non-Qt classes". #### Other info (issues closed, discussion etc) - Clears 2 CodeQL `cpp/resource-not-released-in-destructor` warnings (`test/TVariableEditorTest.cpp`, `test/TLuaInterfaceTest.cpp`). - Also removes a real runtime leak in `TLuaInterfaceTest`: the old fixture never deleted the `interface` and double-allocated it (construction + `init()`), leaking a `LuaInterface` per test plus a construction-time `lua_State`/`LuaInterface`. Verified gone under LeakSanitizer (old binary leaked, new binary is leak-clean; `TVariableEditorTest` already deleted its interface so for it this is modernization). **Test case:** Build and run `ctest -R 'TLuaInterfaceTest|TVariableEditorTest'` - both pass (TLuaInterfaceTest 4/4, TVariableEditorTest 96 passed/13 skipped). Optionally run `./test/TLuaInterfaceTest` with `ASAN_OPTIONS=detect_leaks=1` to confirm no leaks are reported.
2026-07-29 13:45:22 +02:00
lua_State* L = nullptr;
std::unique_ptr<LuaInterface> interface;
private slots: // NOLINT(readability-redundant-access-specifiers)
void init()
{
L = luaL_newstate();
infrastructure: release resources in test fixture destructors (#9522) #### Brief overview of PR changes/additions - Convert the raw owning `LuaInterface*` member in the `TLuaInterfaceTest` and `TVariableEditorTest` Qt-Test fixtures to `std::unique_ptr<LuaInterface>` so the fixture's destructor frees it. - `TLuaInterfaceTest` also stops allocating the interface (and a `lua_State`) twice: members now init to `nullptr`/empty and are allocated only in `init()`; `cleanup()` resets the interface before closing the `lua_State`. #### Motivation for adding to Mudlet Keeps the test suite leak-clean and clears static-analysis warnings, per CLAUDE.md's "smart pointers for non-Qt classes". #### Other info (issues closed, discussion etc) - Clears 2 CodeQL `cpp/resource-not-released-in-destructor` warnings (`test/TVariableEditorTest.cpp`, `test/TLuaInterfaceTest.cpp`). - Also removes a real runtime leak in `TLuaInterfaceTest`: the old fixture never deleted the `interface` and double-allocated it (construction + `init()`), leaking a `LuaInterface` per test plus a construction-time `lua_State`/`LuaInterface`. Verified gone under LeakSanitizer (old binary leaked, new binary is leak-clean; `TVariableEditorTest` already deleted its interface so for it this is modernization). **Test case:** Build and run `ctest -R 'TLuaInterfaceTest|TVariableEditorTest'` - both pass (TLuaInterfaceTest 4/4, TVariableEditorTest 96 passed/13 skipped). Optionally run `./test/TLuaInterfaceTest` with `ASAN_OPTIONS=detect_leaks=1` to confirm no leaks are reported.
2026-07-29 13:45:22 +02:00
interface = std::make_unique<LuaInterface>(L);
}
infrastructure: release resources in test fixture destructors (#9522) #### Brief overview of PR changes/additions - Convert the raw owning `LuaInterface*` member in the `TLuaInterfaceTest` and `TVariableEditorTest` Qt-Test fixtures to `std::unique_ptr<LuaInterface>` so the fixture's destructor frees it. - `TLuaInterfaceTest` also stops allocating the interface (and a `lua_State`) twice: members now init to `nullptr`/empty and are allocated only in `init()`; `cleanup()` resets the interface before closing the `lua_State`. #### Motivation for adding to Mudlet Keeps the test suite leak-clean and clears static-analysis warnings, per CLAUDE.md's "smart pointers for non-Qt classes". #### Other info (issues closed, discussion etc) - Clears 2 CodeQL `cpp/resource-not-released-in-destructor` warnings (`test/TVariableEditorTest.cpp`, `test/TLuaInterfaceTest.cpp`). - Also removes a real runtime leak in `TLuaInterfaceTest`: the old fixture never deleted the `interface` and double-allocated it (construction + `init()`), leaking a `LuaInterface` per test plus a construction-time `lua_State`/`LuaInterface`. Verified gone under LeakSanitizer (old binary leaked, new binary is leak-clean; `TVariableEditorTest` already deleted its interface so for it this is modernization). **Test case:** Build and run `ctest -R 'TLuaInterfaceTest|TVariableEditorTest'` - both pass (TLuaInterfaceTest 4/4, TVariableEditorTest 96 passed/13 skipped). Optionally run `./test/TLuaInterfaceTest` with `ASAN_OPTIONS=detect_leaks=1` to confirm no leaks are reported.
2026-07-29 13:45:22 +02:00
void cleanup()
{
interface.reset();
lua_close(L);
}
infrastructure: release resources in test fixture destructors (#9522) #### Brief overview of PR changes/additions - Convert the raw owning `LuaInterface*` member in the `TLuaInterfaceTest` and `TVariableEditorTest` Qt-Test fixtures to `std::unique_ptr<LuaInterface>` so the fixture's destructor frees it. - `TLuaInterfaceTest` also stops allocating the interface (and a `lua_State`) twice: members now init to `nullptr`/empty and are allocated only in `init()`; `cleanup()` resets the interface before closing the `lua_State`. #### Motivation for adding to Mudlet Keeps the test suite leak-clean and clears static-analysis warnings, per CLAUDE.md's "smart pointers for non-Qt classes". #### Other info (issues closed, discussion etc) - Clears 2 CodeQL `cpp/resource-not-released-in-destructor` warnings (`test/TVariableEditorTest.cpp`, `test/TLuaInterfaceTest.cpp`). - Also removes a real runtime leak in `TLuaInterfaceTest`: the old fixture never deleted the `interface` and double-allocated it (construction + `init()`), leaking a `LuaInterface` per test plus a construction-time `lua_State`/`LuaInterface`. Verified gone under LeakSanitizer (old binary leaked, new binary is leak-clean; `TVariableEditorTest` already deleted its interface so for it this is modernization). **Test case:** Build and run `ctest -R 'TLuaInterfaceTest|TVariableEditorTest'` - both pass (TLuaInterfaceTest 4/4, TVariableEditorTest 96 passed/13 skipped). Optionally run `./test/TLuaInterfaceTest` with `ASAN_OPTIONS=detect_leaks=1` to confirm no leaks are reported.
2026-07-29 13:45:22 +02:00
void execLua(const QString& string)
{
luaL_loadstring(L, string.toUtf8().constData());
lua_pcall(L, 0, 0, 0);
}
void testRetrieveStrings()
{
execLua("test = '1'");
interface->getVars(false);
VarUnit* vu = interface->getVarUnit();
TVar* base = vu->getBase();
QList<TVar*> children = base->getChildren();
TVar* testVar = children.first();
QCOMPARE(testVar->getName(), "test");
QCOMPARE(testVar->getValue(), "1");
QCOMPARE(testVar->getValueType(), LUA_TSTRING);
}
void testRetrieveNumber()
{
execLua("test = 1");
interface->getVars(false);
VarUnit* vu = interface->getVarUnit();
TVar* base = vu->getBase();
QList<TVar*> children = base->getChildren();
TVar* testVar = children.first();
QCOMPARE(testVar->getName(), "test");
QCOMPARE(testVar->getValue(), "1");
QCOMPARE(testVar->getValueType(), LUA_TNUMBER);
}
};
#include "TLuaInterfaceTest.moc"
QTEST_MAIN(TVarTest)