KGantt

test.h
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KGantt library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#ifndef __KDAB__UNITTEST__TEST_H__
10#define __KDAB__UNITTEST__TEST_H__
11
12#ifndef KDAB_NO_UNIT_TESTS
13
14#include "kganttglobal.h"
15
16#include <QtGlobal>
17
18#include <string>
19#include <iostream>
20
21namespace KDAB {
22namespace UnitTest {
23
24#define assertNotNull( x ) _assertNotNull( ( x ), #x, __FILE__, __LINE__ )
25#define assertNull( x ) _assertNull( ( x ), #x, __FILE__, __LINE__ )
26#define assertTrue( x ) _assertTrue( (x), #x, __FILE__, __LINE__ )
27#define assertFalse( x ) _assertFalse( (x), #x, __FILE__, __LINE__ )
28#define assertEqual( x, y ) _assertEqual( (x), (y), #x, #y, __FILE__, __LINE__ )
29#define assertNotEqual( x, y ) _assertNotEqual( (x), (y), #x, #y, __FILE__, __LINE__ )
30// to be phased out:
31#define assertNearEqual( x, y, z )
32#define assertEqualWithEpsilons( x, y, z ) _assertEqualWithEpsilons( (x), (y), (z), #x, #y, #z, __FILE__, __LINE__ )
33#if 0
34#define assertIsNaN( x ) _assertIsNaN( (x), #x, __FILE__, __LINE__ )
35#define assertIsNotNaN( x ) _assertIsNotNaN( (x), #x, __FILE__, __LINE__ )
36#endif
37
38#define assertThrowsExceptionWithCode( x, E, code ) \
39do { \
40 try { \
41 x; \
42 fail( __FILE__, __LINE__ ) \
43 << "\"" #x "\" didn't throw \"" #E "\"" << std::endl; \
44 } catch ( E & ppq_ut_thrown ) { \
45 success(); \
46 ( void )ppq_ut_thrown; \
47 code; \
48 } catch ( ... ) { \
49 fail( __FILE__, __LINE__ ) \
50 << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \
51 } \
52} while ( false )
53
54#define assertThrowsException( x, E ) assertThrowsExceptionWithCode( x, E, do{}while (0) )
55
56#define assertDoesNotThrowException( x, E ) \
57do { \
58 try { \
59 x; \
60 success(); \
61 } catch ( E & ) { \
62 fail( __FILE__, __LINE__ ) \
63 << "\"" #x "\" threw \"" #E "\", but shouldn't" << std::endl; \
64 } catch ( ... ) { \
65 fail( __FILE__, __LINE__ ) \
66 << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \
67 } \
68} while ( false )
69
70
71 class Test {
72 const std::string mName;
73 unsigned int mFailed, mSucceeded;
74 public:
75 Test( const std::string & name );
76 virtual ~Test() {}
77
78 const std::string & name() const { return mName; }
79 unsigned int failed() const { return mFailed; }
80 unsigned int succeeded() const { return mSucceeded; }
81
82 virtual void run() = 0;
83
84 protected:
85 void _assertNotNull( const void * x, const char * expression, const char * file, unsigned int line );
86 void _assertNull( const void * x, const char * expression, const char * file, unsigned int line );
87#if 0
88 void _assertIsNaN( qreal v, const char * expression, const char * file, unsigned int line );
89 void _assertIsNotNaN( qreal v, const char * expression, const char * file, unsigned int line );
90#endif
91 void _assertTrue( bool x, const char * expression, const char * file, unsigned int line );
92 void _assertFalse( bool x, const char * expression, const char * file, unsigned int line );
93
94 void _assertEqualWithEpsilons( float x1, float x2, int prec, const char * expr1, const char * expr2, const char * exprPrec, const char * file, unsigned int line );
95 void _assertEqualWithEpsilons( qreal x1, qreal x2, int prec, const char * expr1, const char * expr2, const char * exprPrec, const char * file, unsigned int line );
96 void _assertEqualWithEpsilons( long double x1, long double x2, int prec, const char * expr1, const char * expr2, const char * exprPrec, const char * file, unsigned int line );
97
98 template <typename T, typename S>
99 void _assertEqual( const T & x1, const S & x2, const char * expr1, const char * expr2, const char * file, unsigned int line ) {
100 if ( x1 == x2 ) this->success();
101 else {
102 this->fail( file, line ) << '"' << expr1 << "\" yielded " << x1 << "; expected: " << x2 << "(\"" << expr2 << "\")" << std::endl;
103 }
104 }
105 template <typename T, typename S>
106 void _assertNotEqual( const T & x1, const S & x2, const char * expr1, const char * expr2, const char * file, unsigned int line ) {
107 if ( x1 != x2 ) this->success();
108 else {
109 this->fail( file, line ) << '"' << expr1 << "\" yielded " << x1 << "; expected something not equal to: " << x2 << "(\"" << expr2 << "\")" << std::endl;
110 }
111 }
112
113 protected:
114 std::ostream & fail( const char * file, unsigned int line );
115 void success() {
116 ++mSucceeded;
117 }
118 };
119
120 class TestFactory {
121 public:
122 virtual ~TestFactory() {}
123 virtual Test * create() const = 0;
124 };
125
126}
127}
128
129#include "testregistry.h"
130
131namespace KDAB {
132namespace UnitTest {
133
134 template <typename T_Test>
135 class GenericFactory : public TestFactory {
136 public:
137 GenericFactory( const char * group = nullptr ) {
138 TestRegistry::instance()->registerTestFactory( this, group );
139 }
140 Test * create() const override { return new T_Test(); }
141 };
142
143}
144}
145
146#include "libutil.h"
147
148// Use these macros to export your UnitTest class so that it gets executed by the test runner.
149// Use the second macro if your class is within a namespace.
150// Arguments :
151// - Namespace (unquoted) : the namespace in which the test class in contained
152// - Class (unquoted) : the test class, without namespace
153// - Group (quoted) : the name of the group this unit test belongs to
154#define KDAB_EXPORT_UNITTEST( Class, Group ) \
155 static const KDAB::UnitTest::GenericFactory< Class > __##Class##_unittest( Group ); \
156 KDAB_EXPORT_STATIC_SYMBOLS( Class )
157
158#define KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class, Group ) \
159 static const KDAB::UnitTest::GenericFactory< Namespace::Class > __##Class##_unittest( Group ); \
160 KDAB_EXPORT_STATIC_SYMBOLS( Class )
161
162// Use this macro to import the test explicitly (for windows static libs only)
163#define KDAB_IMPORT_UNITTEST( Class ) KDAB_IMPORT_STATIC_SYMBOLS( Class )
164
165// Convenience macros that create a simple test class for a single test and export it.
166// Usage : KDAB_UNITTEST_SIMPLE( MyClass, "mygroup" ) { doSomething(); assertEqual(...); }
167#define KDAB_UNITTEST_SIMPLE( Class, Group ) \
168 class Class##Test : public KDAB::UnitTest::Test { \
169 public: \
170 Class##Test() : Test( #Class ) {} \
171 void run(); \
172 }; \
173 KDAB_EXPORT_UNITTEST( Class##Test, Group ) \
174 void Class##Test::run()
175
176#define KDAB_SCOPED_UNITTEST_SIMPLE( Namespace, Class, Group ) \
177 namespace Namespace { \
178 class Class##Test : public KDAB::UnitTest::Test { \
179 public: \
180 Class##Test() : Test( #Namespace "::" #Class ) {} \
181 void run() override; \
182 }; \
183 } \
184 KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class##Test, Group ) \
185 void Namespace::Class##Test::run()
186
187#endif // KDAB_NO_UNIT_TESTS
188
189#endif // __KDAB__UNITTEST__TEST_H__
Contains KGantt macros.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.