File size: 8,908 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/***************************************************************************

 *   Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net>     *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  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 Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/


#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");  // register with Python
    }

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);
}

}  // namespace TestGui

void loadTestResource()

{
    // add resources and reloads the translators
    Q_INIT_RESOURCE(Test);
    Q_INIT_RESOURCE(Test_translation);
    Gui::Translator::instance()->refresh();
}

/* Python entry */
PyMOD_INIT_FUNC(QtUnitGui)
{
    // the following constructor call registers our extension module
    // with the Python runtime system
    PyObject* mod = TestGui::initModule();

    Base::Console().log("Loading GUI of Test module… done\n");

    // add resources and reloads the translators
    loadTestResource();
    PyMOD_Return(mod);
}