tester.h
Go to the documentation of this file.
/*
* tester.h
*
* Copyright (C) 2004 Zack Rusin <zack@kde.org>
* Copyright (C) 2005 Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TESTER_H
#define TESTER_H
#include <iostream>
using namespace std;
#include <qobject.h>
#include <qstringlist.h>
#include <qasciidict.h>
#include <kdelibs_export.h>
#define CHECK( x, y ) check( __FILE__, __LINE__, #x, x, y, false )
#define COMPARE CHECK
#define VERIFY( x ) CHECK( x, true )
#define XFAIL( x, y ) check( __FILE__, __LINE__, #x, x, y, true )
#define SKIP( x ) skip( __FILE__, __LINE__, QString::fromLatin1(#x))
#define CHECK_EXCEPTION(exceptionCatch, expression) \
try \
{ \
expression; \
} \
catch(exceptionCatch) \
{ \
setExceptionRaised(true); \
} \
if(exceptionRaised()) \
{ \
success(QString(__FILE__) + "[" + QString::number(__LINE__) + "]: passed " + #expression); \
} \
else \
{ \
failure(QString(__FILE__) + "[" + QString::number(__LINE__) + QString("]: failed to throw " \
"an exception on: ") + #expression); \
} \
setExceptionRaised(false);
#define XFAIL_EXCEPTION(exceptionCatch, expression) \
try \
{ \
expression; \
} \
catch(exceptionCatch) \
{ \
setExceptionRaised(true); \
} \
if(exceptionRaised()) \
{ \
unexpectedSuccess(QString(__FILE__) + "[" + QString::number(__LINE__) + "]: unexpectedly threw an exception and passed: " + #expression); \
}\
else \
{ \
expectedFailure(QString(__FILE__) + "[" + QString::number(__LINE__) + QString("]: failed to throw an exception on: ") + #expression); \
} \
setExceptionRaised(false);
#define SKIP_EXCEPTION(exceptionCatch, expression) \
skip( __FILE__, __LINE__, QString("Exception catch: ")\
.arg(QString(#exceptionCatch)).arg(QString(" Test expression: ")).arg(QString(#expression)))
namespace KUnitTest
{
class KUNITTEST_EXPORT TestResults
{
friend class Tester;
public:
TestResults() : m_tests( 0 ) {}
virtual ~TestResults() {}
virtual void clear()
{
m_errorList.clear();
m_xfailList.clear();
m_xpassList.clear();
m_skipList.clear();
m_successList.clear();
m_debug = "";
m_tests = 0;
}
virtual void addDebugInfo(const QString &debug)
{
m_debug += debug;
}
QString debugInfo() const { return m_debug; }
int testsFinished() const { return m_tests; }
int errors() const { return m_errorList.count(); }
int xfails() const { return m_xfailList.count(); }
int xpasses() const { return m_xpassList.count(); }
int skipped() const { return m_skipList.count(); }
int passed() const { return m_successList.count(); }
QStringList errorList() const { return m_errorList; }
QStringList xfailList() const { return m_xfailList; }
QStringList xpassList() const { return m_xpassList; }
QStringList skipList() const { return m_skipList; }
QStringList successList() const { return m_successList; }
private:
QStringList m_errorList;
QStringList m_xfailList;
QStringList m_xpassList;
QStringList m_skipList;
QStringList m_successList;
QString m_debug;
int m_tests;
};
typedef QAsciiDict<TestResults> TestResultsListType;
typedef QAsciiDictIterator<TestResults> TestResultsListIteratorType;
class KUNITTEST_EXPORT Tester : public QObject
{
public:
Tester(const char *name = 0L)
: QObject(0L, name), m_results(new TestResults()), m_exceptionState(false)
{}
virtual ~Tester() { delete m_results; }
public:
virtual void allTests() = 0;
public:
virtual TestResults *results() { return m_results; }
protected:
void skip( const char *file, int line, QString msg )
{
QString skipEntry;
QTextStream ts( &skipEntry, IO_WriteOnly );
ts << file << "["<< line <<"]: " << msg;
skipTest( skipEntry );
}
template<typename T>
void check( const char *file, int line, const char *str,
const T &result, const T &expectedResult,
bool expectedFail )
{
cout << "check: " << file << "["<< line <<"]" << endl;
if ( result != expectedResult )
{
QString error;
QTextStream ts( &error, IO_WriteOnly );
ts << file << "["<< line <<"]: failed on \"" << str
<<"\" result = '" << result << "' expected = '" << expectedResult << "'";
if ( expectedFail )
expectedFailure( error );
else
failure( error );
}
else
{
// then the test passed, but we want to record it if
// we were expecting a failure
if (expectedFail)
{
QString err;
QTextStream ts( &err, IO_WriteOnly );
ts << file << "["<< line <<"]: "
<<" unexpectedly passed on \""
<< str <<"\"";
unexpectedSuccess( err );
}
else
{
QString succ;
QTextStream ts( &succ, IO_WriteOnly );
ts << file << "["<< line <<"]: "
<<" passed \""
<< str <<"\"";
success( succ );
}
}
++m_results->m_tests;
}
void success(const QString &message) { m_results->m_successList.append(message); }
void failure(const QString &message) { m_results->m_errorList.append(message); }
void expectedFailure(const QString &message) { m_results->m_xfailList.append(message); }
void unexpectedSuccess(const QString &message) { m_results->m_xpassList.append(message); }
void skipTest(const QString &message) { m_results->m_skipList.append(message); }
void setExceptionRaised(bool state) { m_exceptionState = state; }
bool exceptionRaised() const
{
return m_exceptionState;
}
protected:
TestResults *m_results;
private:
bool m_exceptionState;
};
class KUNITTEST_EXPORT SlotTester : public Tester
{
Q_OBJECT
public:
SlotTester(const char *name = 0L);
void allTests();
TestResults *results(const char *sl);
TestResultsListType &resultsList() { return m_resultsList; }
signals:
void invoke();
private:
void invokeMember(const QString &str);
TestResultsListType m_resultsList;
TestResults *m_total;
};
}
KUNITTEST_EXPORT QTextStream& operator<<( QTextStream& str, const QRect& r );
KUNITTEST_EXPORT QTextStream& operator<<( QTextStream& str, const QPoint& r );
KUNITTEST_EXPORT QTextStream& operator<<( QTextStream& str, const QSize& r );
#endif
KUnitTest::TestResultsListIteratorType
QAsciiDictIterator< TestResults > TestResultsListIteratorType
Definition: tester.h:512
KUnitTest::Tester
Definition: tester.h:522
KUnitTest::TestResults::xpasses
int xpasses() const
Definition: tester.h:476
KUnitTest::TestResults::passed
int passed() const
Definition: tester.h:482
KUnitTest::Tester::setExceptionRaised
void setExceptionRaised(bool state)
Definition: tester.h:662
KUnitTest::TestResults::clear
virtual void clear()
Definition: tester.h:443
KUnitTest::Tester::success
void success(const QString &message)
Definition: tester.h:617
KUnitTest::TestResults::debugInfo
QString debugInfo() const
Definition: tester.h:464
KUnitTest::TestResults::xpassList
QStringList xpassList() const
Definition: tester.h:491
KUnitTest::Tester::check
void check(const char *file, int line, const char *str, const T &result, const T &expectedResult, bool expectedFail)
Definition: tester.h:564
KUnitTest::TestResults::testsFinished
int testsFinished() const
Definition: tester.h:467
KUnitTest::Tester::failure
void failure(const QString &message)
Definition: tester.h:626
KUnitTest::TestResults::addDebugInfo
virtual void addDebugInfo(const QString &debug)
Definition: tester.h:457
KUnitTest::TestResults::skipList
QStringList skipList() const
Definition: tester.h:494
KUnitTest::TestResults::errors
int errors() const
Definition: tester.h:470
KUnitTest::SlotTester
Definition: tester.h:686
KUnitTest::Tester::skip
void skip(const char *file, int line, QString msg)
Definition: tester.h:547
KUnitTest::TestResults::errorList
QStringList errorList() const
Definition: tester.h:485
KUnitTest::TestResults
Definition: tester.h:432
KUnitTest::TestResults::xfails
int xfails() const
Definition: tester.h:473
KUnitTest::TestResults::xfailList
QStringList xfailList() const
Definition: tester.h:488
KUnitTest::Tester::unexpectedSuccess
void unexpectedSuccess(const QString &message)
Definition: tester.h:644
KUnitTest::Tester::results
virtual TestResults * results()
Definition: tester.h:539
KUnitTest::TestResults::skipped
int skipped() const
Definition: tester.h:479
KUnitTest::Tester::expectedFailure
void expectedFailure(const QString &message)
Definition: tester.h:635
KUnitTest::Tester::exceptionRaised
bool exceptionRaised() const
Definition: tester.h:669
KUnitTest::Tester::skipTest
void skipTest(const QString &message)
Definition: tester.h:653
KUnitTest::TestResults::successList
QStringList successList() const
Definition: tester.h:497

KDE Home | KDE Accessibility Home | Description of Access Keys