Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
test.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_TEST_HPP
19 #define NURIA_TEST_HPP
20 
21 #include <functional>
22 #include <string>
23 
24 namespace Nuria {
25 
26 typedef std::function< bool() > TestFunction;
27 
32 void addTestCase (const std::string &file, const std::string &name,
33  TestFunction func);
34 
40 bool runSingleTest (const std::string &file, const std::string &name);
41 
46 bool runTestCases (int argc, char **argv);
47 
48 }
49 
50 #if defined(NURIA_TEST_RUN)
51 
52 // Helper macros
53 #define NURIA_TEST_CLASS_NAME(Name) TestClass_ ## Name
54 #define NURIA_TEST_CLASS_INSTANCE(Name) \
55  static TestClass_ ## Name TestInstance_ ## Name
56 
57 #if defined(QT_WIDGETS_LIB)
58 #include <QApplication>
59 #define NURIA_TEST_MAIN_PROLOGUE QApplication a (argc, argv);
60 #elif defined(QT_GUI_LIB)
61 #include <QGuiApplication>
62 #define NURIA_TEST_MAIN_PROLOGUE QGuiApplication a (argc, argv);
63 #elif defined(QT_CORE_LIB)
64 #include <QCoreApplication>
65 #define NURIA_TEST_MAIN_PROLOGUE QCoreApplication a (argc, argv);
66 #else
67 #define NURIA_TEST_MAIN_PROLOGUE
68 #endif
69 
91 #define NURIA_TEST(Name, ...) \
92  namespace NuriaTest { \
93  struct NURIA_TEST_CLASS_NAME(Name) { \
94  NURIA_TEST_CLASS_NAME(Name) () { \
95  Nuria::addTestCase (__FILE__, #Name, test); \
96  } \
97  static bool test () __VA_ARGS__ \
98  }; \
99  NURIA_TEST_CLASS_INSTANCE(Name); \
100  }
101 
109 #define NURIA_TEST_MAIN() \
110  int main (int argc, char **argv) { \
111  NURIA_TEST_MAIN_PROLOGUE \
112  if (!Nuria::runTestCases (argc, argv)) { \
113  return 1; \
114  } \
115  return 0; \
116  }
117 
118 #else
119 
120 // Dummy macros
121 #define NURIA_TEST(Name, Code, ...)
122 #define NURIA_TEST_MAIN()
123 
124 #endif
125 
126 #endif // NURIA_TEST_HPP