• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KUnitTest

  • sources
  • kde-4.12
  • kdelibs
  • kde3support
  • kunittest
tester.h
Go to the documentation of this file.
1 /*
2  * tester.h
3  *
4  * Copyright (C) 2004 Zack Rusin <zack@kde.org>
5  * Copyright (C) 2005 Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef TESTER_H
30 #define TESTER_H
31 
320 #include <iostream>
321 using namespace std;
322 
323 #include <QtCore/QObject>
324 #include <QtCore/QStringList>
325 #include <QtCore/QHash>
326 #include <QtCore/QTextStream>
327 
328 #include "kunittest_export.h"
329 
335 #define CHECK( x, y ) check( __FILE__, __LINE__, #x, x, y, false )
336 
342 #define VERIFY( cond ) check( __FILE__, __LINE__, #cond, cond, true, false )
343 
352 #define XFAIL( x, y ) check( __FILE__, __LINE__, #x, x, y, true )
353 
359 #define SKIP( x ) skip( __FILE__, __LINE__, QLatin1String(#x))
360 
368 #define CHECK_EXCEPTION(exceptionCatch, expression) \
369  try \
370  { \
371  expression; \
372  } \
373  catch(exceptionCatch) \
374  { \
375  setExceptionRaised(true); \
376  } \
377  if(exceptionRaised()) \
378  { \
379  success(QString(__FILE__) + "[" + QString::number(__LINE__) + "]: passed " + #expression); \
380  } \
381  else \
382  { \
383  failure(QString(__FILE__) + "[" + QString::number(__LINE__) + QString("]: failed to throw " \
384  "an exception on: ") + #expression); \
385  } \
386  setExceptionRaised(false);
387 
392 #define XFAIL_EXCEPTION(exceptionCatch, expression) \
393  try \
394  { \
395  expression; \
396  } \
397  catch(exceptionCatch) \
398  { \
399  setExceptionRaised(true); \
400  } \
401  if(exceptionRaised()) \
402  { \
403  unexpectedSuccess(QString(__FILE__) + "[" + QString::number(__LINE__) + "]: unexpectedly threw an exception and passed: " + #expression); \
404  }\
405  else \
406  { \
407  expectedFailure(QString(__FILE__) + "[" + QString::number(__LINE__) + QString("]: failed to throw an exception on: ") + #expression); \
408  } \
409  setExceptionRaised(false);
410 
416 #define SKIP_EXCEPTION(exceptionCatch, expression) \
417  skip( __FILE__, __LINE__, QString("Exception catch: ")\
418  .arg(QString(#exceptionCatch)).arg(QString(" Test expression: ")).arg(QString(#expression)))
419 
420 namespace KUnitTest
421 {
426  class KUNITTEST_EXPORT TestResults
427  {
428  friend class Tester;
429 
430  public:
431  TestResults() : m_tests( 0 ) {}
432 
433  virtual ~TestResults() {}
434 
437  virtual void clear()
438  {
439  m_errorList.clear();
440  m_xfailList.clear();
441  m_xpassList.clear();
442  m_skipList.clear();
443  m_successList.clear();
444  m_debug = QLatin1String("");
445  m_tests = 0;
446  }
447 
451  virtual void addDebugInfo(const QString &debug)
452  {
453  m_debug += debug;
454  }
455 
458  QString debugInfo() const { return m_debug; }
459 
461  int testsFinished() const { return m_tests; }
462 
464  int errors() const { return m_errorList.count(); }
465 
467  int xfails() const { return m_xfailList.count(); }
468 
470  int xpasses() const { return m_xpassList.count(); }
471 
473  int skipped() const { return m_skipList.count(); }
474 
476  int passed() const { return m_successList.count(); }
477 
479  QStringList errorList() const { return m_errorList; }
480 
482  QStringList xfailList() const { return m_xfailList; }
483 
485  QStringList xpassList() const { return m_xpassList; }
486 
488  QStringList skipList() const { return m_skipList; }
489 
491  QStringList successList() const { return m_successList; }
492 
493  private:
494  QStringList m_errorList;
495  QStringList m_xfailList;
496  QStringList m_xpassList;
497  QStringList m_skipList;
498  QStringList m_successList;
499  QString m_debug;
500  int m_tests;
501  };
502 
503  typedef QHash<QByteArray, TestResults *> TestResultsList;
504 
506  //typedef TestResultsList::Iterator TestResultsListIteratorType;
507 
516  class KUNITTEST_EXPORT Tester : public QObject
517  {
518  public:
519  Tester(const char *name = 0L)
520  : QObject(0L), m_results(new TestResults()), m_exceptionState(false)
521  {
522  setObjectName( QLatin1String(name) );
523  }
524 
525  virtual ~Tester() { delete m_results; }
526 
527  public:
530  virtual void allTests() = 0;
531 
532  public:
535  virtual TestResults *results() const { return m_results; }
536 
537  protected:
543  void skip( const char *file, int line, QString msg )
544  {
545  QString skipEntry;
546  QTextStream ts( &skipEntry, QIODevice::WriteOnly );
547  ts << file << "["<< line <<"]: " << msg;
548  skipTest( skipEntry );
549  }
550 
559  template<typename T>
560  void check( const char *file, int line, const char *str,
561  const T &result, const T &expectedResult,
562  bool expectedFail )
563  {
564  cout << "check: " << file << "["<< line <<"]" << endl;
565 
566  if ( result != expectedResult )
567  {
568  QString error;
569  QTextStream ts( &error, QIODevice::WriteOnly );
570  ts << file << "["<< line <<"]: failed on \"" << str
571  <<"\" result = '" << result << "' expected = '" << expectedResult << "'";
572 
573  if ( expectedFail )
574  expectedFailure( error );
575  else
576  failure( error );
577 
578  }
579  else
580  {
581  // then the test passed, but we want to record it if
582  // we were expecting a failure
583  if (expectedFail)
584  {
585  QString err;
586  QTextStream ts( &err, QIODevice::WriteOnly );
587  ts << file << "["<< line <<"]: "
588  <<" unexpectedly passed on \""
589  << str <<"\"";
590  unexpectedSuccess( err );
591  }
592  else
593  {
594  QString succ;
595  QTextStream ts( &succ, QIODevice::WriteOnly );
596  ts << file << "["<< line <<"]: "
597  <<" passed \""
598  << str <<"\"";
599  success( succ );
600  }
601  }
602 
603  ++m_results->m_tests;
604  }
605 
613  void success(const QString &message) { m_results->m_successList.append(message); }
614 
622  void failure(const QString &message) { m_results->m_errorList.append(message); }
623 
631  void expectedFailure(const QString &message) { m_results->m_xfailList.append(message); }
632 
640  void unexpectedSuccess(const QString &message) { m_results->m_xpassList.append(message); }
641 
649  void skipTest(const QString &message) { m_results->m_skipList.append(message); }
650 
658  void setExceptionRaised(bool state) { m_exceptionState = state; }
659 
665  bool exceptionRaised() const
666  {
667  return m_exceptionState;
668  }
669 
670  protected:
671  TestResults *m_results;
672 
673  private:
674 
675  bool m_exceptionState;
676  };
677 
682  class KUNITTEST_EXPORT SlotTester : public Tester
683  {
684  Q_OBJECT
685 
686  public:
687  SlotTester();
688  virtual ~SlotTester();
689 
690  void allTests();
691 
692  virtual TestResults *results() const { return Tester::results(); }
693 
695  TestResults *results(const char *slotName);
696 
698  const TestResultsList &resultsList() const { return m_resultsList; }
699 
700  Q_SIGNALS:
701  void invoke();
702 
703  private:
704  void invokeMember(const QString &str);
705 
706  TestResultsList m_resultsList;
707  TestResults *m_total;
708  };
709 }
710 
711 class QRect;
712 KUNITTEST_EXPORT QTextStream& operator<<( QTextStream& str, const QRect& r );
713 
714 class QPoint;
715 KUNITTEST_EXPORT QTextStream& operator<<( QTextStream& str, const QPoint& r );
716 
717 class QSize;
718 KUNITTEST_EXPORT QTextStream& operator<<( QTextStream& str, const QSize& r );
719 
720 #endif
KUnitTest::Tester
Definition: tester.h:516
KUnitTest::TestResults::xpasses
int xpasses() const
Definition: tester.h:470
KUnitTest::TestResults::passed
int passed() const
Definition: tester.h:476
KUnitTest::Tester::results
virtual TestResults * results() const
Definition: tester.h:535
KUnitTest::TestResultsList
QHash< QByteArray, TestResults * > TestResultsList
Definition: tester.h:503
KUnitTest::Tester::setExceptionRaised
void setExceptionRaised(bool state)
Definition: tester.h:658
KUnitTest::TestResults::clear
virtual void clear()
Definition: tester.h:437
KUnitTest::Tester::success
void success(const QString &message)
Definition: tester.h:613
KUnitTest::TestResults::debugInfo
QString debugInfo() const
Definition: tester.h:458
QString
QHash< QByteArray, TestResults * >
QObject
KUnitTest::TestResults::TestResults
TestResults()
Definition: tester.h:431
KUnitTest::TestResults::xpassList
QStringList xpassList() const
Definition: tester.h:485
KUnitTest::Tester::check
void check(const char *file, int line, const char *str, const T &result, const T &expectedResult, bool expectedFail)
Definition: tester.h:560
KUnitTest::TestResults::testsFinished
int testsFinished() const
Definition: tester.h:461
KUnitTest::SlotTester::resultsList
const TestResultsList & resultsList() const
Return the list of results - used internally by Runner.
Definition: tester.h:698
kunittest_export.h
KUnitTest::Tester::failure
void failure(const QString &message)
Definition: tester.h:622
KUnitTest::TestResults::addDebugInfo
virtual void addDebugInfo(const QString &debug)
Definition: tester.h:451
QStringList
KUnitTest::TestResults::skipList
QStringList skipList() const
Definition: tester.h:488
KUnitTest::TestResults::errors
int errors() const
Definition: tester.h:464
KUnitTest::SlotTester
Definition: tester.h:682
KUnitTest::Tester::~Tester
virtual ~Tester()
Definition: tester.h:525
KUnitTest::Tester::skip
void skip(const char *file, int line, QString msg)
Definition: tester.h:543
KUnitTest::TestResults::errorList
QStringList errorList() const
Definition: tester.h:479
KUnitTest::TestResults
Definition: tester.h:426
KUnitTest::Tester::Tester
Tester(const char *name=0L)
Definition: tester.h:519
KUnitTest::TestResults::xfails
int xfails() const
Definition: tester.h:467
KUnitTest::TestResults::~TestResults
virtual ~TestResults()
Definition: tester.h:433
KUnitTest::TestResults::xfailList
QStringList xfailList() const
Definition: tester.h:482
QPoint
QRect
KUnitTest::Tester::unexpectedSuccess
void unexpectedSuccess(const QString &message)
Definition: tester.h:640
operator<<
QTextStream & operator<<(QTextStream &str, const QRect &r)
Definition: tester.cpp:102
QSize
KUnitTest::Tester::m_results
TestResults * m_results
Definition: tester.h:671
KUnitTest::SlotTester::results
virtual TestResults * results() const
Definition: tester.h:692
KUnitTest::TestResults::skipped
int skipped() const
Definition: tester.h:473
KUnitTest::Tester::expectedFailure
void expectedFailure(const QString &message)
Definition: tester.h:631
KUnitTest::Tester::exceptionRaised
bool exceptionRaised() const
Definition: tester.h:665
KUnitTest::Tester::skipTest
void skipTest(const QString &message)
Definition: tester.h:649
KUnitTest::TestResults::successList
QStringList successList() const
Definition: tester.h:491
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:52:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KUnitTest

Skip menu "KUnitTest"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal