edbee - Qt Editor Library
test.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <QObject>
9 #include <QString>
10 #include <QSharedPointer>
11 #include <QTextStream>
12 
13 
16 //=============================================================================
17 // MACROS
18 //=============================================================================
19 
20 #define TEST_CONCATENATE_DETAIL(x, y) x##y
21 #define TEST_CONCATENATE(x, y) TEST_CONCATENATE_DETAIL(x, y)
22 #define TEST_MAKE_UNIQUE(x) TEST_CONCATENATE(x, __COUNTER__)
23 
24 #define DECLARE_TEST(className) static edbee::test::Test<className> TEST_MAKE_UNIQUE(t)(#className)
25 #define DECLARE_NAMED_TEST(name,className) static edbee::test::Test<className> TEST_MAKE_UNIQUE(name)(#className)
26 
27 
28 namespace edbee { namespace test {
29 
30 
31 class OutputHandler;
32 class TestEngine;
33 class TestCase;
34 
35 //=============================================================================
36 // Test Results
37 //=============================================================================
38 
39 template <class T>
40 QString toQString( const T& obj )
41 {
42  return QString("%1").arg(obj);
43 }
44 
45 
48 {
49 public:
50  enum Status {
52  };
53 
54  explicit TestResult( TestCase* testCase, const QString& methodName, const QString& description, const char* file, int lineNumber );
55  virtual ~TestResult() {}
56 
57  virtual void setBooleanResult( bool result, const char *statement );
58  //virtual void setCompareResult( const QString& actualValue, const QString& expectedValue, const char* actualStatement, const char* expectedStatement );
59 
60  virtual void setCompareResult( bool result, const QString& actualValue, const QString& expectedValue, const char* actualStatement, const char* expectedStatement );
61 
62 // template <class T>
63 // void setCompareResult( const T& actualValue, const T& expectedValue, const char* actualStatement, const char* expectedStatement )
64 // {
65 // setCompareResult( actualValue == expectedValue, toQString(actualValue), toQString(expectedValue), actualStatement, expectedStatement );
66 // }
67 
68  virtual void setSkip();
69 
70  virtual TestCase* testCae() { return testCaseRef_; }
71  virtual QString methodName() { return methodName_; }
72  virtual QString description() { return description_; }
73  virtual const char* fileName() { return fileNameRef_; }
74  virtual int lineNumber() { return lineNumber_; }
75 
76  virtual bool compareStatement() { return compareStatement_; }
77  virtual const char* actualStatement() { return actualStatementRef_; }
78  virtual const char* statement() { return actualStatementRef_; }
79  virtual const char* expectedStatement() { return expectedStatementRef_; }
80  virtual QString actualValue() { return actualValue_; }
81  virtual QString expectedValue() { return expectedValue_; }
82 
83  virtual Status status() { return status_; }
84 
85 
86 private:
87 
88  // method / line defionition
89  TestCase* testCaseRef_;
90  QString methodName_;
91  QString description_;
92  const char* fileNameRef_;
93  int lineNumber_;
94 
95  // code definition
96  bool compareStatement_;
97  const char* actualStatementRef_;
98  const char* expectedStatementRef_;
99  QString actualValue_;
100  QString expectedValue_;
101 
102  // the result
103  Status status_;
104 };
105 
106 
107 
108 
109 //=============================================================================
110 // Test Unit Class
111 //=============================================================================
112 
113 #define GET_3TH_ARG(arg1, arg2, arg3, ...) arg3
114 #define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
115 
116 
117 #define testTrue_1(statement) testTrueImpl( ( statement ) ? true : false, #statement, "", __FILE__, __LINE__)
118 #define testTrue_2(statement, message) testTrueImpl( ( statement ) ? true : false, #statement, (message), __FILE__, __LINE__ )
119 #define testTrue_MACRO_CHOOSER(...) \
120  GET_3TH_ARG(__VA_ARGS__, testTrue_2, testTrue_1, )
121 #define testTrue(...) testTrue_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
122 
123 
124 #define testFalse(statement) testTrue( !statement )
125 
126 
127 #define testEqual_1(actual,expected) do { \
128  QString actualStr = edbee::test::toQString(actual); \
129  QString expectedStr = edbee::test::toQString(expected); \
130  testEqualImpl( ( actualStr ) == ( expectedStr ), actualStr, expectedStr, #actual, #expected, "", __FILE__, __LINE__ ); \
131 } while( false )
132 
133 #define testEqual_2(actual,expected, message) do \
134  QString actualStr = edbee::test::toQString(actual); \
135  QString expectedStr = edbee::test::toQString(expected); \
136  testEqualImpl( ( actualStr ) == ( expectedStr ), actualStr, expectedStr, #actual, #expected, (message), __FILE__, __LINE__ ); \
137 } while( false )
138 
139 
140 #define testEqual_MACRO_CHOOSER(...) \
141  GET_4TH_ARG(__VA_ARGS__, testEqual_2, testEqual_1, )
142 #define testEqual(...) testEqual_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
143 
144 
145 #define testSkip(msg) testSkipImpl( msg, __FILE__, __LINE__)
146 
147 
148 
149 
150 
158 
161 class TestCase : public QObject
162 {
163  Q_OBJECT
164 public:
165  explicit TestCase();
166 
168  virtual TestEngine* engine() { return engineRef_; }
169  virtual void setEngine( TestEngine *engine ) { engineRef_ = engine; }
170 
171  virtual OutputHandler* out();
172 
173 
174  virtual void testTrueImpl( bool condition, const char* statement, const QString& description, const char* file, int line );
175 
176 
177  virtual void testEqualImpl( bool result, const QString& actual, const QString& expected,
178  const char* actualStatement, const char* expectedStatement,
179  const QString& description, const char* file, int line );
180 
181  virtual void testSkipImpl( const QString& description, const char* file, int line );
182 
183 
184  virtual TestCase* currentTest(); // usually returns this :P
185  virtual QString currentMethodName();
186  virtual void giveTestResultToEngine( TestResult* result );
187 
188 
189 signals:
190 
191 public slots:
192 
193 private slots:
194 
195 private:
196 
197  TestEngine *engineRef_;
198 };
199 
200 
201 //=============================================================================
202 // Output Handling
203 //=============================================================================
204 
208 {
209 public:
210 
211  explicit OutputHandler();
212  virtual ~OutputHandler();
213 
214  // writes down the start of a multiple test run
215  virtual void startTestRun( TestEngine* engine );
216  virtual void endTestRun( TestEngine* engine );
217 
218 
219  // writes down the 'category' of the test
220  virtual void startTestCase( TestEngine* engine );
221  virtual void endTestCase( TestEngine* engine );
222 
223 
224  // method output of the test case
225  virtual void startTestMethod( TestEngine* engine );
226  virtual void endTestMethod( TestEngine* engine );
227 
228  virtual void testResultAdded( TestEngine* engine, TestResult* testResult );
229 
230 
231 
232 private:
233 
234  QString buffer_;
235  QString failBuffer_;
236  QString outBuffer_;
237 
238  // this method notifies that a value is wrong
239 // void writeSuccess( TestCase & testCase, const char *methodName );
240 
241  // fails with the given messages
242  //void fail( const QString & message, const QString & details = "" ) ;
243 };
244 
245 
246 
247 
248 //=============================================================================
249 // Test Engine
250 //=============================================================================
251 
253 class TestEngine : public QObject
254 {
255  Q_OBJECT
256 public:
257 
258  explicit TestEngine();
259  virtual ~TestEngine();
260 
261  virtual bool hasTest(TestCase* object);
262  virtual void addTest(TestCase* object);
263  virtual int runAll();
264  virtual int run( TestCase* object );
265  virtual int run( const QString& name );
266  virtual void startRun();
267  virtual void endRun();
268 
269  virtual OutputHandler* outputHandler() { return outputHandlerRef_; }
270  virtual void setOutputHandler( OutputHandler* handler ) { outputHandlerRef_ = handler; }
271 
273  virtual TestCase* currentTest() { return currentTestRef_; }
274 
276  virtual const char* currentClassName() { return currentTestRef_->metaObject()->className(); }
277 
279  virtual QString currentMethodName() { return currentMethodName_; }
280 
281  virtual void giveTestResult( TestResult* testResult );
282 
284  virtual QList<TestResult*> testResultList() { return testResultList_; }
285 
286 private:
287 
288  QList<TestCase*> testRefList_;
289  OutputHandler* outputHandlerRef_;
290 
291  // the current state
292  TestCase* currentTestRef_;
293  QString currentMethodName_;
294 // const char* currentMethodNameRef_; ///< A reference to the current method
295 
296  // test results
297  QList<TestResult*> testResultList_;
298 };
299 
300 
301 //=============================================================================
302 // Appending Tests via a Macro
303 //=============================================================================
304 
305 // this method returns the test engine
307 {
308  static TestEngine engine;
309  return engine;
310 }
311 
312 
313 template <class T>
314 class Test
315 {
316 public:
317  QSharedPointer<T> child;
318 
319  Test(const QString& name) : child(new T)
320  {
321  child->setObjectName(name);
322  engine().addTest(child.data());
323  }
324 };
325 
326 
327 }} // edbee::test
virtual int lineNumber()
Definition: test.h:74
virtual void setSkip()
A skip test result.
Definition: test.cpp:70
virtual QString actualValue()
Definition: test.h:80
This method represents a test result.
Definition: test.h:47
virtual const char * currentClassName()
This method returns the current class name.
Definition: test.h:276
virtual const char * fileName()
Definition: test.h:73
virtual const char * statement()
Definition: test.h:78
Definition: test.h:51
virtual bool compareStatement()
Definition: test.h:76
virtual QString expectedValue()
Definition: test.h:81
virtual TestCase * testCae()
Definition: test.h:70
virtual const char * expectedStatement()
Definition: test.h:79
virtual Status status()
Definition: test.h:83
virtual void setOutputHandler(OutputHandler *handler)
Definition: test.h:270
QString toQString(const T &obj)
Definition: test.h:40
I really really hate the QTestLib output on my Mac. It&#39;s a very ugly xwindows console result...
Definition: test.h:161
virtual void setBooleanResult(bool result, const char *statement)
Sets the result and the state of the test.
Definition: test.cpp:44
virtual QString methodName()
Definition: test.h:71
Test(const QString &name)
Definition: test.h:319
virtual void setCompareResult(bool result, const QString &actualValue, const QString &expectedValue, const char *actualStatement, const char *expectedStatement)
Sets a compare result.
Definition: test.cpp:57
#define new
Definition: debug_new.h:33
Status
Definition: test.h:50
virtual QString currentMethodName()
This method returns the current method name.
Definition: test.h:279
Definition: test.h:51
Copyright 2011-2013 - Reliable Bits Software by Blommers IT.
Definition: commentcommand.cpp:22
This is the main test engine.
Definition: test.h:253
virtual QList< TestResult * > testResultList()
This method returns a list of test results.
Definition: test.h:284
QSharedPointer< T > child
Definition: test.h:317
TestEngine & engine()
Definition: test.h:306
virtual const char * actualStatement()
Definition: test.h:77
This is the basic outputhandler. The basic outputhandler simply executes a qDebug with the given info...
Definition: test.h:207
virtual ~TestResult()
Definition: test.h:55
TestResult(TestCase *testCase, const QString &methodName, const QString &description, const char *file, int lineNumber)
A single test result.
Definition: test.cpp:25
virtual QString description()
Definition: test.h:72
virtual TestEngine * engine()
This method returns the current engine.
Definition: test.h:168
virtual void addTest(TestCase *object)
this metohd adds a test
Definition: test.cpp:339
virtual void setEngine(TestEngine *engine)
Definition: test.h:169
virtual OutputHandler * outputHandler()
Definition: test.h:269
virtual TestCase * currentTest()
This method returns the current test.
Definition: test.h:273
Definition: test.h:314