00001
00002
00003 #include "TestSuite.hh"
00004 #include "TestSuiteFormatter.hh"
00005 #include <iostream>
00006 #include <stdexcept>
00007 #include <cassert>
00008
00009 namespace ccunit
00010 {
00011
00012 class TestSuiteError : public logic_error
00013 {
00014 public:
00015 TestSuiteError(const string& s = "")
00016 : logic_error(s)
00017 {}
00018 };
00019
00020 TestSuite::TestSuite(const string& name, TestSuiteFormatterPtr formatter) :
00021 TestComponent(name),
00022 mp_formatter(formatter)
00023 {
00024 }
00025
00026 void
00027 TestSuite::run_tests()
00028 {
00029
00030 reset();
00031
00032
00033 for(TEST_LIST_t::const_iterator ii = m_tests.begin();
00034 ii != m_tests.end();
00035 ++ii) {
00036
00037 m_nPass += (*ii)->getNumPassed();
00038 m_nFail += (*ii)->getNumFailed();
00039 }
00040
00041 m_hasRun = true;
00042 }
00043
00044 void
00045 TestSuite::report(ostream &ostr)
00046 {
00047 checkRun();
00048
00049
00050 mp_formatter->header(ostr, *this);
00051
00052
00053
00054
00055
00056 for(TEST_LIST_t::iterator ii = m_tests.begin();
00057 ii != m_tests.end();
00058 ++ii) {
00059 mp_formatter->printComponent(ostr, *ii);
00060 }
00061
00062 mp_formatter->footer(ostr, *this);
00063 }
00064
00066 void
00067 TestSuite::setFormatter(TestSuiteFormatterPtr fmt_p)
00068 {
00069 mp_formatter = fmt_p;
00070 }
00071
00072 void
00073 TestSuite::reset()
00074 {
00075 TestComponent::reset();
00076
00077 for(TEST_LIST_t::iterator ii = m_tests.begin();
00078 ii != m_tests.end();
00079 ++ii) {
00080
00081 (*ii)->reset();
00082 }
00083 }
00084
00085
00086 void
00087 TestSuite::Add(TestComponentPtr child)
00088 {
00089
00090
00091 if (child.get() == this) {
00092
00093 throw string("RecursiveAdd");
00094 }
00095 if (child->CheckRecursion(this)) {
00096
00097 throw string("RecursiveAdd");
00098
00099 } else {
00100
00101
00102 m_tests.push_back(child);
00103
00104
00105 }
00106 }
00107
00108
00109 void
00110 TestSuite::Remove(TestComponentPtr child)
00111 {
00112 m_tests.remove(child);
00113 }
00114
00115
00116 bool
00117 TestSuite::CheckRecursion(TestComponent *target) const
00118 {
00119 TEST_LIST_t::const_iterator i;
00120
00121 for (i = m_tests.begin();
00122 i != m_tests.end();
00123 i++) {
00124
00125
00126
00127
00128 if (i->get() == target) {
00129 return(true);
00130 } else {
00131 if ((*i)->CheckRecursion(target)) {
00132 return(true);
00133 }
00134 }
00135 }
00136
00137 return(false);
00138 }
00139
00140 }