1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifndef _U2_UNIT_TEST_H_
23 #define _U2_UNIT_TEST_H_
24 
25 #include <QMetaType>
26 #include <QString>
27 
28 #define _STR(c) #c
29 #define TEST_CLASS(suite, name) suite##_##name
30 #define TEST_CLASS_STR(suite, name) _STR(suite##_##name)
31 
32 /** Macros for writing tests without repeating too much */
33 
34 /** For each test place this in header file, inside namespace U2 to declare test class */
35 #define DECLARE_TEST(suite, name) \
36     class TEST_CLASS(suite, name) : public UnitTest { \
37     public: \
38         virtual void Test(); \
39     }
40 
41 /** For each test place this in header file, outside (!) namespace U2 to register Qt meta type */
42 #define DECLARE_METATYPE(suite, name) \
43     Q_DECLARE_METATYPE(U2::suite##_##name)
44 
45 /** Place this in cpp file and define test body in subsequent {} block */
46 #define IMPLEMENT_TEST(suite, name) \
47     static const int _##suite##_##name##_type ATTR_UNUSED = qRegisterMetaType<U2::TEST_CLASS(suite, name)>(TEST_CLASS_STR(suite, name)); \
48     void TEST_CLASS(suite, name)::Test()
49 
50 /** Macros to be used in test body: they all check for some condition,
51     and if check fails then set test error and break its execution */
52 
53 #define CHECK_NO_ERROR(os) CHECK_OP_EXT(os, SetError(os.getError()), )
54 #define CHECK_TRUE(condition, error) CHECK_EXT(condition, SetError(error), )
55 #define CHECK_FALSE(condition, error) CHECK_EXT(!(condition), SetError(error), )
56 
57 /** To use CHECK_EQUAL and CHECK_NOT_EQUAL for class that is not convertible to QString,
58     define partial specialization of template function toString<T>(const T &t) */
59 #define CHECK_EQUAL(expected, actual, what) CHECK_TRUE(expected == actual, QString("unexpected %1: expected '%2', got '%3'").arg(what).arg(toString(expected)).arg(toString(actual)))
60 #define CHECK_NOT_EQUAL(notExpected, actual, what) CHECK_TRUE(notExpected != actual, QString("unexpected %1: expected not to be '%2', but got '%3'").arg(what).arg(toString(notExpected)).arg(toString(actual)))
61 
62 namespace U2 {
63 
64 class UnitTest {
65 public:
~UnitTest()66     virtual ~UnitTest() {
67     }
68 
69     virtual void Test() = 0;
SetUp()70     virtual void SetUp() {
71     }
TearDown()72     virtual void TearDown() {
73     }
SetError(const QString & err)74     virtual void SetError(const QString &err) {
75         error = err;
76     }
GetError()77     virtual QString GetError() {
78         return error;
79     }
80 
81 protected:
82     QString error;
83 };
84 
85 template<class T>
toString(const T & t)86 QString toString(const T &t) {
87     return QString("%1").arg(t);
88 }
89 
90 }  // namespace U2
91 
92 #endif
93