00001
00002 #include "UnitTest.hh"
00003 #include <iostream>
00004 #include <typeinfo>
00005
00006 #ifdef _MSC_VER
00007
00008 #pragma warning(disable: 4541)
00009 #endif
00010
00011 namespace ccunit
00012 {
00013
00014 bool
00015 UnitTest::do_test(bool cond, const std::string& lbl,
00016 const char* fname, const char* fnt_name, long lineno)
00017 {
00018 if (!cond) {
00019 do_fail(lbl, fname, fnt_name, lineno);
00020 return false;
00021 } else {
00022 _succeed();
00023 return true;
00024 }
00025 }
00026
00027
00028 void
00029 UnitTest::do_fail(const std::string& lbl,
00030 const char* fname, const char* fnt_name, long lineno)
00031 {
00032 ++m_nFail;
00033 m_failures.push_back(ErrorReport(typeid(*this).name(), lbl, fname, fnt_name, lineno));
00034 }
00035
00036 void
00037 UnitTest::report(ostream &ostr)
00038 {
00039 assert(false);
00040
00041 checkRun();
00042
00043 assert(m_hasRun);
00044
00045 if(!m_failures.empty()) {
00046 ostr << "The following tests failed: \n";
00047 copy(m_failures.begin(), m_failures.end(), ostream_iterator<ErrorReport>(ostr, "\n"));
00048 }
00049
00050 ostr << "UnitTest \""
00051 << typeid(*this).name() << "\":\n"
00052 << "\tPassed: " << m_nPass
00053 << "\tFailed: " << m_nFail;
00054 }
00055
00056
00057 ostream &
00058 operator<<(ostream &ostr, UnitTest &my_test)
00059 {
00060 my_test.report(ostr);
00061 return ostr;
00062 }
00063
00064 }