• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeunittest
 

tdeunittest

  • tdeunittest
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 
325 #include <iostream>
326 using namespace std;
327 
328 #include <tqobject.h>
329 #include <tqstringlist.h>
330 #include <tqasciidict.h>
331 
332 #include <tdelibs_export.h>
333 
339 #define CHECK( x, y ) check( __FILE__, __LINE__, #x, x, y, false )
340 
342 #define COMPARE CHECK
343 
345 #define VERIFY( x ) CHECK( x, true )
346 
355 #define XFAIL( x, y ) check( __FILE__, __LINE__, #x, x, y, true )
356 
362 #define SKIP( x ) skip( __FILE__, __LINE__, TQString::fromLatin1(#x))
363 
371 #define CHECK_EXCEPTION(exceptionCatch, expression) \
372  try \
373  { \
374  expression; \
375  } \
376  catch(exceptionCatch) \
377  { \
378  setExceptionRaised(true); \
379  } \
380  if(exceptionRaised()) \
381  { \
382  success(TQString(__FILE__) + "[" + TQString::number(__LINE__) + "]: passed " + #expression); \
383  } \
384  else \
385  { \
386  failure(TQString(__FILE__) + "[" + TQString::number(__LINE__) + TQString("]: failed to throw " \
387  "an exception on: ") + #expression); \
388  } \
389  setExceptionRaised(false);
390 
395 #define XFAIL_EXCEPTION(exceptionCatch, expression) \
396  try \
397  { \
398  expression; \
399  } \
400  catch(exceptionCatch) \
401  { \
402  setExceptionRaised(true); \
403  } \
404  if(exceptionRaised()) \
405  { \
406  unexpectedSuccess(TQString(__FILE__) + "[" + TQString::number(__LINE__) + "]: unexpectedly threw an exception and passed: " + #expression); \
407  }\
408  else \
409  { \
410  expectedFailure(TQString(__FILE__) + "[" + TQString::number(__LINE__) + TQString("]: failed to throw an exception on: ") + #expression); \
411  } \
412  setExceptionRaised(false);
413 
419 #define SKIP_EXCEPTION(exceptionCatch, expression) \
420  skip( __FILE__, __LINE__, TQString("Exception catch: ")\
421  .arg(TQString(#exceptionCatch)).arg(TQString(" Test expression: ")).arg(TQString(#expression)))
422 
426 namespace KUnitTest
427 {
432  class TDEUNITTEST_EXPORT TestResults
433  {
434  friend class Tester;
435 
436  public:
437  TestResults() : m_tests( 0 ) {}
438 
439  virtual ~TestResults() {}
440 
443  virtual void clear()
444  {
445  m_errorList.clear();
446  m_xfailList.clear();
447  m_xpassList.clear();
448  m_skipList.clear();
449  m_successList.clear();
450  m_debug = "";
451  m_tests = 0;
452  }
453 
457  virtual void addDebugInfo(const TQString &debug)
458  {
459  m_debug += debug;
460  }
461 
464  TQString debugInfo() const { return m_debug; }
465 
467  int testsFinished() const { return m_tests; }
468 
470  int errors() const { return m_errorList.count(); }
471 
473  int xfails() const { return m_xfailList.count(); }
474 
476  int xpasses() const { return m_xpassList.count(); }
477 
479  int skipped() const { return m_skipList.count(); }
480 
482  int passed() const { return m_successList.count(); }
483 
485  TQStringList errorList() const { return m_errorList; }
486 
488  TQStringList xfailList() const { return m_xfailList; }
489 
491  TQStringList xpassList() const { return m_xpassList; }
492 
494  TQStringList skipList() const { return m_skipList; }
495 
497  TQStringList successList() const { return m_successList; }
498 
499  private:
500  TQStringList m_errorList;
501  TQStringList m_xfailList;
502  TQStringList m_xpassList;
503  TQStringList m_skipList;
504  TQStringList m_successList;
505  TQString m_debug;
506  int m_tests;
507  };
508 
509  typedef TQAsciiDict<TestResults> TestResultsListType;
510 
512  typedef TQAsciiDictIterator<TestResults> TestResultsListIteratorType;
513 
522  class TDEUNITTEST_EXPORT Tester : public TQObject
523  {
524  public:
525  Tester(const char *name = 0L)
526  : TQObject(0L, name), m_results(new TestResults()), m_exceptionState(false)
527  {}
528 
529  virtual ~Tester() { delete m_results; }
530 
531  public:
534  virtual void allTests() = 0;
535 
536  public:
539  virtual TestResults *results() { return m_results; }
540 
541  protected:
547  void skip( const char *file, int line, TQString msg )
548  {
549  TQString skipEntry;
550  TQTextStream ts( &skipEntry, IO_WriteOnly );
551  ts << file << "["<< line <<"]: " << msg;
552  skipTest( skipEntry );
553  }
554 
563  template<typename T>
564  void check( const char *file, int line, const char *str,
565  const T &result, const T &expectedResult,
566  bool expectedFail )
567  {
568  cout << "check: " << file << "["<< line <<"]" << endl;
569 
570  if ( result != expectedResult )
571  {
572  TQString error;
573  TQTextStream ts( &error, IO_WriteOnly );
574  ts << file << "["<< line <<"]: failed on \"" << str
575  <<"\" result = '" << result << "' expected = '" << expectedResult << "'";
576 
577  if ( expectedFail )
578  expectedFailure( error );
579  else
580  failure( error );
581 
582  }
583  else
584  {
585  // then the test passed, but we want to record it if
586  // we were expecting a failure
587  if (expectedFail)
588  {
589  TQString err;
590  TQTextStream ts( &err, IO_WriteOnly );
591  ts << file << "["<< line <<"]: "
592  <<" unexpectedly passed on \""
593  << str <<"\"";
594  unexpectedSuccess( err );
595  }
596  else
597  {
598  TQString succ;
599  TQTextStream ts( &succ, IO_WriteOnly );
600  ts << file << "["<< line <<"]: "
601  <<" passed \""
602  << str <<"\"";
603  success( succ );
604  }
605  }
606 
607  ++m_results->m_tests;
608  }
609 
617  void success(const TQString &message) { m_results->m_successList.append(message); }
618 
626  void failure(const TQString &message) { m_results->m_errorList.append(message); }
627 
635  void expectedFailure(const TQString &message) { m_results->m_xfailList.append(message); }
636 
644  void unexpectedSuccess(const TQString &message) { m_results->m_xpassList.append(message); }
645 
653  void skipTest(const TQString &message) { m_results->m_skipList.append(message); }
654 
662  void setExceptionRaised(bool state) { m_exceptionState = state; }
663 
669  bool exceptionRaised() const
670  {
671  return m_exceptionState;
672  }
673 
674  protected:
675  TestResults *m_results;
676 
677  private:
678 
679  bool m_exceptionState;
680  };
681 
686  class TDEUNITTEST_EXPORT SlotTester : public Tester
687  {
688  TQ_OBJECT
689 
690  public:
691  SlotTester(const char *name = 0L);
692 
693  void allTests();
694 
695  TestResults *results(const char *sl);
696 
697  TestResultsListType &resultsList() { return m_resultsList; }
698 
699  signals:
700  void invoke();
701 
702  private:
703  void invokeMember(const TQString &str);
704 
705  TestResultsListType m_resultsList;
706  TestResults *m_total;
707  };
708 }
709 
710 TDEUNITTEST_EXPORT TQTextStream& operator<<( TQTextStream& str, const TQRect& r );
711 
712 TDEUNITTEST_EXPORT TQTextStream& operator<<( TQTextStream& str, const TQPoint& r );
713 
714 TDEUNITTEST_EXPORT TQTextStream& operator<<( TQTextStream& str, const TQSize& r );
715 
716 #endif
KUnitTest::SlotTester
Definition: tester.h:687
KUnitTest::TestResults
Definition: tester.h:433
KUnitTest::TestResults::xpasses
int xpasses() const
Definition: tester.h:476
KUnitTest::TestResults::skipList
TQStringList skipList() const
Definition: tester.h:494
KUnitTest::TestResults::xfails
int xfails() const
Definition: tester.h:473
KUnitTest::TestResults::xpassList
TQStringList xpassList() const
Definition: tester.h:491
KUnitTest::TestResults::clear
virtual void clear()
Definition: tester.h:443
KUnitTest::TestResults::errorList
TQStringList errorList() const
Definition: tester.h:485
KUnitTest::TestResults::errors
int errors() const
Definition: tester.h:470
KUnitTest::TestResults::skipped
int skipped() const
Definition: tester.h:479
KUnitTest::TestResults::addDebugInfo
virtual void addDebugInfo(const TQString &debug)
Definition: tester.h:457
KUnitTest::TestResults::successList
TQStringList successList() const
Definition: tester.h:497
KUnitTest::TestResults::testsFinished
int testsFinished() const
Definition: tester.h:467
KUnitTest::TestResults::xfailList
TQStringList xfailList() const
Definition: tester.h:488
KUnitTest::TestResults::debugInfo
TQString debugInfo() const
Definition: tester.h:464
KUnitTest::TestResults::passed
int passed() const
Definition: tester.h:482
KUnitTest::Tester
Definition: tester.h:523
KUnitTest::Tester::results
virtual TestResults * results()
Definition: tester.h:539
KUnitTest::Tester::expectedFailure
void expectedFailure(const TQString &message)
Definition: tester.h:635
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::Tester::exceptionRaised
bool exceptionRaised() const
Definition: tester.h:669
KUnitTest::Tester::success
void success(const TQString &message)
Definition: tester.h:617
KUnitTest::Tester::failure
void failure(const TQString &message)
Definition: tester.h:626
KUnitTest::Tester::skipTest
void skipTest(const TQString &message)
Definition: tester.h:653
KUnitTest::Tester::skip
void skip(const char *file, int line, TQString msg)
Definition: tester.h:547
KUnitTest::Tester::unexpectedSuccess
void unexpectedSuccess(const TQString &message)
Definition: tester.h:644
KUnitTest::Tester::setExceptionRaised
void setExceptionRaised(bool state)
Definition: tester.h:662
KUnitTest::Tester::allTests
virtual void allTests()=0
KUnitTest
Namespace for Unit testing classes.
Definition: module.h:40
KUnitTest::TestResultsListIteratorType
TQAsciiDictIterator< TestResults > TestResultsListIteratorType
Definition: tester.h:512

tdeunittest

Skip menu "tdeunittest"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

tdeunittest

Skip menu "tdeunittest"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeunittest by doxygen 1.9.1
This website is maintained by Timothy Pearson.