Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

TestSuite.cc

Go to the documentation of this file.
00001 /* -*-Mode:C++ -*- */
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   // reset the test counters
00030   reset();
00031 
00032   // iterator over tests
00033   for(TEST_LIST_t::const_iterator ii = m_tests.begin();
00034       ii != m_tests.end();
00035       ++ii) {
00036     // Tabulate the results
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   // show the header
00050   mp_formatter->header(ostr, *this);
00051 
00052   // print the failures
00053   //  for_each(m_tests.begin(), m_tests.end(), mp_formatter->printFailure);
00054 
00055   // show the footer
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     // assume nothing!
00081     (*ii)->reset();
00082   }
00083 }
00084 
00085 // add a component to a composite
00086 void
00087 TestSuite::Add(TestComponentPtr child)
00088 {
00089   //  debug(17) << "Adding " << child->getName() << " to " << getName() << endd;
00090 
00091   if (child.get() == this) {   // Check for adding composite to itself
00092     //    debug(15) << "Recursion found; trying to add composite to itself" << endd;
00093     throw string("RecursiveAdd");
00094   } 
00095   if (child->CheckRecursion(this)) {  // Check if composite is already in children
00096     //    debug(15) << "Recursion found; add disallowed" << endd;
00097     throw string("RecursiveAdd");
00098     
00099   } else {
00100     
00101     //    debug(12) << "Adding child" << endd;
00102     m_tests.push_back(child);
00103     
00104     //    debug(12) << "No recursion found.  Child added. " << endd;
00105   }
00106 }
00107 
00108 // remove a component from a composite
00109 void
00110 TestSuite::Remove(TestComponentPtr child)
00111 {
00112   m_tests.remove(child);
00113 }
00114 
00115 // return true for attempts to add self to composite
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     //    debug(25) << "iterating through components checking for recursion" << endd;
00125     //    debug(22) << "Comparing \"" << (target->getName()) 
00126     //             << "\" to \"" << ((*i)->getName()) << "\" "
00127     //       << endd;
00128     if (i->get() == target) {
00129       return(true);  // recursion found
00130     } else {
00131       if ((*i)->CheckRecursion(target)) {
00132         return(true);
00133       }
00134     }
00135   }
00136 
00137   return(false);  
00138 }
00139 
00140 } // namespace ccunit

Generated on Tue Apr 2 15:33:23 2002 for libccunit by doxygen 1.2.14 written by Dimitri van Heesch, © 1997-2002

Project hosted on sourceforge SourceForge Logo