| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| |
|
| | #include <Base/Console.h>
|
| | #include <Base/ConsoleObserver.h>
|
| | #include <Base/Interpreter.h>
|
| | #include <Gui/Language/Translator.h>
|
| |
|
| | #include "UnitTestImp.h"
|
| | #include "UnitTestPy.h"
|
| |
|
| |
|
| | class ILoggerBlockerTest: public Base::ILogger
|
| | {
|
| | public:
|
| | ~ILoggerBlockerTest() override
|
| | {
|
| | Base::Console().detachObserver(this);
|
| | }
|
| |
|
| | const char* name() override
|
| | {
|
| | return "ILoggerBlockerTest";
|
| | }
|
| |
|
| | void flush()
|
| | {
|
| | buffer.str("");
|
| | buffer.clear();
|
| | }
|
| |
|
| | void sendLog(
|
| | const std::string& notifiername,
|
| | const std::string& msg,
|
| | Base::LogStyle level,
|
| | Base::IntendedRecipient recipient,
|
| | Base::ContentType content
|
| | ) override
|
| | {
|
| | (void)notifiername;
|
| | (void)msg;
|
| | (void)recipient;
|
| | (void)content;
|
| |
|
| | switch (level) {
|
| | case Base::LogStyle::Warning:
|
| | buffer << "WRN";
|
| | break;
|
| | case Base::LogStyle::Message:
|
| | buffer << "MSG";
|
| | break;
|
| | case Base::LogStyle::Error:
|
| | buffer << "ERR";
|
| | break;
|
| | case Base::LogStyle::Log:
|
| | buffer << "LOG";
|
| | break;
|
| | case Base::LogStyle::Critical:
|
| | buffer << "CMS";
|
| | break;
|
| | default:
|
| | break;
|
| | }
|
| | }
|
| |
|
| | void runSingleTest(const char* comment, std::string expectedResult)
|
| | {
|
| | Base::Console().log(comment);
|
| | flush();
|
| | Base::Console().log("LOG");
|
| | Base::Console().message("MSG");
|
| | Base::Console().warning("WRN");
|
| | Base::Console().error("ERR");
|
| | Base::Console().critical("CMS");
|
| | if (buffer.str() != expectedResult) {
|
| | throw Py::RuntimeError(
|
| | "ILoggerTest: " + buffer.str() + " different from " + expectedResult
|
| | );
|
| | }
|
| | }
|
| |
|
| | void runTest()
|
| | {
|
| | runSingleTest("Print all message types", "LOGMSGWRNERRCMS");
|
| | {
|
| | Base::ILoggerBlocker blocker("ILoggerBlockerTest");
|
| | runSingleTest("All types blocked", "");
|
| | }
|
| | runSingleTest("Print all", "LOGMSGWRNERRCMS");
|
| | {
|
| | Base::ILoggerBlocker blocker(
|
| | "ILoggerBlockerTest",
|
| | Base::ConsoleSingleton::MsgType_Err | Base::ConsoleSingleton::MsgType_Wrn
|
| | );
|
| | runSingleTest("Error & Warning blocked", "LOGMSGCMS");
|
| | }
|
| | runSingleTest("Print all", "LOGMSGWRNERRCMS");
|
| | {
|
| | Base::ILoggerBlocker blocker(
|
| | "ILoggerBlockerTest",
|
| | Base::ConsoleSingleton::MsgType_Log | Base::ConsoleSingleton::MsgType_Txt
|
| | );
|
| | runSingleTest("Log & Message blocked", "WRNERRCMS");
|
| | }
|
| | runSingleTest("Print all", "LOGMSGWRNERRCMS");
|
| | {
|
| | Base::ILoggerBlocker blocker("ILoggerBlockerTest", Base::ConsoleSingleton::MsgType_Err);
|
| | runSingleTest("Nested : Error blocked", "LOGMSGWRNCMS");
|
| | {
|
| | Base::ILoggerBlocker blocker2(
|
| | "ILoggerBlockerTest",
|
| | Base::ConsoleSingleton::MsgType_Err | Base::ConsoleSingleton::MsgType_Wrn
|
| | );
|
| | runSingleTest(
|
| | "Nested : Warning blocked + Error (from nesting) + Error (redundancy)",
|
| | "LOGMSGCMS"
|
| | );
|
| | }
|
| | runSingleTest("Nested : Error still blocked", "LOGMSGWRNCMS");
|
| | }
|
| | runSingleTest("Print all", "LOGMSGWRNERRCMS");
|
| | {
|
| | Base::ILoggerBlocker blocker("ILoggerBlockerTest");
|
| | Base::Console()
|
| | .setEnabledMsgType("ILoggerBlockerTest", Base::ConsoleSingleton::MsgType_Log, true);
|
| | runSingleTest("Log is enabled but a warning is triggered in debug mode", "LOG");
|
| | }
|
| | runSingleTest("Print all", "LOGMSGWRNERRCMS");
|
| | }
|
| |
|
| | private:
|
| | std::ostringstream buffer;
|
| | };
|
| |
|
| |
|
| | namespace TestGui
|
| | {
|
| | class Module: public Py::ExtensionModule<Module>
|
| | {
|
| |
|
| | public:
|
| | Module()
|
| | : Py::ExtensionModule<Module>("QtUnitGui")
|
| | {
|
| | TestGui::UnitTestDialogPy::init_type();
|
| | add_varargs_method("UnitTest", &Module::new_UnitTest, "UnitTest");
|
| | add_varargs_method("setTest", &Module::setTest, "setTest");
|
| | add_varargs_method("addTest", &Module::addTest, "addTest");
|
| | add_varargs_method("runTest", &Module::runTest, "runTest");
|
| | add_varargs_method("testILoggerBlocker", &Module::testILoggerBlocker, "testILoggerBlocker");
|
| | initialize("This module is the QtUnitGui module");
|
| | }
|
| |
|
| | private:
|
| | Py::Object new_UnitTest(const Py::Tuple& args)
|
| | {
|
| | if (!PyArg_ParseTuple(args.ptr(), "")) {
|
| | throw Py::Exception();
|
| | }
|
| | return Py::asObject(new TestGui::UnitTestDialogPy());
|
| | }
|
| | Py::Object setTest(const Py::Tuple& args)
|
| | {
|
| | char* pstr = nullptr;
|
| | if (!PyArg_ParseTuple(args.ptr(), "|s", &pstr)) {
|
| | throw Py::Exception();
|
| | }
|
| |
|
| | TestGui::UnitTestDialog* dlg = TestGui::UnitTestDialog::instance();
|
| | if (pstr) {
|
| | dlg->setUnitTest(QString::fromLatin1(pstr));
|
| | }
|
| | dlg->show();
|
| | dlg->raise();
|
| | return Py::None();
|
| | }
|
| | Py::Object addTest(const Py::Tuple& args)
|
| | {
|
| | char* pstr = nullptr;
|
| | if (!PyArg_ParseTuple(args.ptr(), "|s", &pstr)) {
|
| | throw Py::Exception();
|
| | }
|
| |
|
| | TestGui::UnitTestDialog* dlg = TestGui::UnitTestDialog::instance();
|
| | if (pstr) {
|
| | dlg->addUnitTest(QString::fromLatin1(pstr));
|
| | }
|
| | dlg->show();
|
| | dlg->raise();
|
| | return Py::None();
|
| | }
|
| | Py::Object runTest(const Py::Tuple& args)
|
| | {
|
| | if (!PyArg_ParseTuple(args.ptr(), "")) {
|
| | throw Py::Exception();
|
| | }
|
| |
|
| | TestGui::UnitTestDialog* dlg = TestGui::UnitTestDialog::instance();
|
| | bool success = dlg->runCurrentTest();
|
| | return Py::Boolean(success);
|
| | }
|
| | Py::Object testILoggerBlocker(const Py::Tuple& args)
|
| | {
|
| | (void)args;
|
| | ILoggerBlockerTest iltest;
|
| | Base::Console().attachObserver(static_cast<Base::ILogger*>(&iltest));
|
| | Base::Console().setConnectionMode(Base::ConsoleSingleton::Direct);
|
| | iltest.runTest();
|
| | return Py::None();
|
| | }
|
| | };
|
| |
|
| | PyObject* initModule()
|
| | {
|
| | return Base::Interpreter().addModule(new Module);
|
| | }
|
| |
|
| | }
|
| |
|
| | void loadTestResource()
|
| | {
|
| |
|
| | Q_INIT_RESOURCE(Test);
|
| | Q_INIT_RESOURCE(Test_translation);
|
| | Gui::Translator::instance()->refresh();
|
| | }
|
| |
|
| |
|
| | PyMOD_INIT_FUNC(QtUnitGui)
|
| | {
|
| |
|
| |
|
| | PyObject* mod = TestGui::initModule();
|
| |
|
| | Base::Console().log("Loading GUI of Test module… done\n");
|
| |
|
| |
|
| | loadTestResource();
|
| | PyMOD_Return(mod);
|
| | }
|
| |
|