9#ifndef __KDAB__UNITTEST__TEST_H__
10#define __KDAB__UNITTEST__TEST_H__
12#ifndef KDAB_NO_UNIT_TESTS
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__ )
31#define assertNearEqual( x, y, z )
32#define assertEqualWithEpsilons( x, y, z ) _assertEqualWithEpsilons( (x), (y), (z), #x, #y, #z, __FILE__, __LINE__ )
34#define assertIsNaN( x ) _assertIsNaN( (x), #x, __FILE__, __LINE__ )
35#define assertIsNotNaN( x ) _assertIsNotNaN( (x), #x, __FILE__, __LINE__ )
38#define assertThrowsExceptionWithCode( x, E, code ) \
42 fail( __FILE__, __LINE__ ) \
43 << "\"" #x "\" didn't throw \"" #E "\"" << std::endl; \
44 } catch ( E & ppq_ut_thrown ) { \
46 ( void )ppq_ut_thrown; \
49 fail( __FILE__, __LINE__ ) \
50 << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \
54#define assertThrowsException( x, E ) assertThrowsExceptionWithCode( x, E, do{}while (0) )
56#define assertDoesNotThrowException( x, E ) \
62 fail( __FILE__, __LINE__ ) \
63 << "\"" #x "\" threw \"" #E "\", but shouldn't" << std::endl; \
65 fail( __FILE__, __LINE__ ) \
66 << "\"" #x "\" threw something, but it wasn't \"" #E "\"" << std::endl; \
72 const std::string mName;
73 unsigned int mFailed, mSucceeded;
75 Test(
const std::string & name );
78 const std::string & name()
const {
return mName; }
79 unsigned int failed()
const {
return mFailed; }
80 unsigned int succeeded()
const {
return mSucceeded; }
82 virtual void run() = 0;
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 );
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 );
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 );
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 );
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();
102 this->fail( file, line ) <<
'"' << expr1 <<
"\" yielded " << x1 <<
"; expected: " << x2 <<
"(\"" << expr2 <<
"\")" << std::endl;
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();
109 this->fail( file, line ) <<
'"' << expr1 <<
"\" yielded " << x1 <<
"; expected something not equal to: " << x2 <<
"(\"" << expr2 <<
"\")" << std::endl;
114 std::ostream & fail(
const char * file,
unsigned int line );
122 virtual ~TestFactory() {}
123 virtual Test * create()
const = 0;
129#include "testregistry.h"
134 template <
typename T_Test>
135 class GenericFactory :
public TestFactory {
137 GenericFactory(
const char * group =
nullptr ) {
138 TestRegistry::instance()->registerTestFactory(
this, group );
140 Test * create()
const override {
return new T_Test(); }
154#define KDAB_EXPORT_UNITTEST( Class, Group ) \
155 static const KDAB::UnitTest::GenericFactory< Class > __##Class##_unittest( Group ); \
156 KDAB_EXPORT_STATIC_SYMBOLS( Class )
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 )
163#define KDAB_IMPORT_UNITTEST( Class ) KDAB_IMPORT_STATIC_SYMBOLS( Class )
167#define KDAB_UNITTEST_SIMPLE( Class, Group ) \
168 class Class##Test : public KDAB::UnitTest::Test { \
170 Class##Test() : Test( #Class ) {} \
173 KDAB_EXPORT_UNITTEST( Class##Test, Group ) \
174 void Class##Test::run()
176#define KDAB_SCOPED_UNITTEST_SIMPLE( Namespace, Class, Group ) \
177 namespace Namespace { \
178 class Class##Test : public KDAB::UnitTest::Test { \
180 Class##Test() : Test( #Namespace "::" #Class ) {} \
181 void run() override; \
184 KDAB_EXPORT_SCOPED_UNITTEST( Namespace, Class##Test, Group ) \
185 void Namespace::Class##Test::run()
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.