Fixes and tests.

This commit is contained in:
Mikaël Capelle
2022-05-02 18:56:44 +02:00
parent 6ea9d92b26
commit 30a01cf783
15 changed files with 308 additions and 51 deletions
+39 -5
View File
@@ -1,8 +1,42 @@
cmake_minimum_required(VERSION 3.16)
cmake_minimum_required(VERSION 3.22)
add_executable(pythonrunner-tests)
mo2_configure_tests(pythonrunner-tests
WARNINGS OFF)
mo2_add_dependencies(pythonrunner-tests PUBLIC uibase)
# setting-up the tests for the runner is a bit complex because we need a tons of
# things
# first we configure the tests as with other tests
add_executable(pythonrunner-tests EXCLUDE_FROM_ALL)
mo2_configure_tests(pythonrunner-tests WARNINGS OFF)
# add mocks
target_include_directories(pythonrunner-tests
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../mocks)
# link to pythonrunner - we set PYTHONRUNNER_LIBRARY to not export the symbol but
# loads it directly, we are going to add the DLL path below
target_compile_definitions(pythonrunner-tests PUBLIC PYTHONRUNNER_LIBRARY)
target_link_libraries(pythonrunner-tests PUBLIC pythonrunner)
set(PYLIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/pylibs)
mo2_python_pip_install(pythonrunner-tests
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/pylibs
PACKAGES pytest PyQt6==6.3.0)
add_dependencies(pythonrunner-tests mobase)
# we set multiple properties, including:
# - updated PATH for DLLs (Qt, etc.), Python and pythonrunner
# - PYTHONPATH
set(pythoncore "${PYTHON_ROOT}/PCbuild/amd64/pythoncore")
file(GLOB pythoncorezip "${pythoncore}/python*.zip")
set(PYTHONPATH "${PYLIB_DIR}\\;$<TARGET_FILE_DIR:mobase>\\;${pythoncore}\\;${pythoncorezip}")
set(extra_paths "${MO2_INSTALL_PATH}/bin/dlls")
string(APPEND extra_paths "\\;${PYTHON_ROOT}/PCbuild/amd64")
string(APPEND extra_paths "\\;$<TARGET_FILE_DIR:pythonrunner>")
set_tests_properties(${pythonrunner-tests_gtests}
PROPERTIES
WORKING_DIRECTORY "${MO2_INSTALL_PATH}/bin"
ENVIRONMENT "PLUGIN_DIR=${CMAKE_CURRENT_SOURCE_DIR}/plugins"
ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:${extra_paths};PYTHONPATH=set:${PYTHONPATH}")
+31
View File
@@ -0,0 +1,31 @@
# -*- encoding: utf-8 -*-
import mobase
class DummyPlugin(mobase.IPlugin):
def init(self, organizer: mobase.IOrganizer) -> bool:
return True
def author(self) -> str:
return "The Author"
def name(self) -> str:
return "The Name"
def description(self) -> str:
return "The Description"
def version(self) -> mobase.VersionInfo:
return mobase.VersionInfo("1.3.0")
def settings(self) -> list[mobase.PluginSetting]:
return [
mobase.PluginSetting(
"a setting", "the setting description", default_value=12
)
]
def createPlugin() -> mobase.IPlugin:
return DummyPlugin()
+42
View File
@@ -0,0 +1,42 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "MockOrganizer.h"
#include "iplugin.h"
#include "pythonrunner.h"
#include <QCoreApplication>
using namespace MOBase;
TEST(IPlugin, Basic)
{
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
std::unique_ptr<IPythonRunner> runner(CreatePythonRunner());
runner->initialize();
// load objects
const auto objects = runner->load(plugins_folder + "/dummy-iplugin.py");
EXPECT_EQ(objects.size(), 1);
// load the IPlugin
const IPlugin* plugin = qobject_cast<IPlugin*>(objects[0]);
EXPECT_NE(plugin, nullptr);
EXPECT_EQ(plugin->author(), "The Author");
EXPECT_EQ(plugin->name(), "The Name");
EXPECT_EQ(plugin->version(), VersionInfo(1, 3, 0));
EXPECT_EQ(plugin->description(), "The Description");
// settings
const auto settings = plugin->settings();
EXPECT_EQ(settings.size(), 1);
EXPECT_EQ(settings[0].key, "a setting");
EXPECT_EQ(settings[0].description, "the setting description");
EXPECT_EQ(settings[0].defaultValue.userType(), QVariant::Type::Int);
EXPECT_EQ(settings[0].defaultValue.toInt(), 12);
// no translation, no custom implementation -> name()
EXPECT_EQ(plugin->localizedName(), "The Name");
}
+47
View File
@@ -0,0 +1,47 @@
#include "gtest/gtest.h"
#include "MockOrganizer.h"
#include "pythonrunner.h"
#include <QCoreApplication>
TEST(Lifetime, Plugins)
{
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
std::unique_ptr<IPythonRunner> runner(CreatePythonRunner());
runner->initialize();
{
const auto objects = runner->load(plugins_folder + "/dummy-iplugin.py");
// we found one plugin
EXPECT_EQ(objects.size(), 1);
// check that deleting the object actually destroys it
bool destroyed = false;
QObject::connect(objects[0], &QObject::destroyed, [&destroyed]() {
destroyed = true;
});
delete objects[0];
EXPECT_EQ(destroyed, true);
}
// same things but with a parent
{
QObject* dummy_parent = new QObject();
const auto objects = runner->load(plugins_folder + "/dummy-iplugin.py");
// we found one plugin
EXPECT_EQ(objects.size(), 1);
objects[0]->setParent(dummy_parent);
// check that deleting the object actually destroys it
bool destroyed = false;
QObject::connect(objects[0], &QObject::destroyed, [&destroyed]() {
destroyed = true;
});
delete dummy_parent;
EXPECT_EQ(destroyed, true);
}
}
-13
View File
@@ -1,13 +0,0 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "MockOrganizer.h"
using ::testing::Eq;
using ::testing::NaggyMock;
using ::testing::Return;
TEST(Organizer, Basic)
{
MockOrganizer mock;
}