mudlet/test/TLuaInterfaceTest.cpp
Vadim Peretokin 1dfd5c4605
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

96 lines
3.1 KiB
C++

/***************************************************************************
* 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>
#include <memory>
extern "C" {
#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
}
class TVarTest : public QObject
{
Q_OBJECT
private:
lua_State* L = nullptr;
std::unique_ptr<LuaInterface> interface;
private slots: // NOLINT(readability-redundant-access-specifiers)
void init()
{
L = luaL_newstate();
interface = std::make_unique<LuaInterface>(L);
}
void cleanup()
{
interface.reset();
lua_close(L);
}
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)