CPUnit 0.95 (beta)
The REAL C++ port of JUnit.
/Users/Shared/Development/cpp/sourceforge/cpunit_095/src/cpunit_TestTreeNode.cpp
Go to the documentation of this file.
00001 /*
00002    Copyright (c) 2011 Daniel Bakkelund.
00003    All rights reserved.
00004 
00005    Redistribution and use in source and binary forms, with or without
00006    modification, are permitted provided that the following conditions
00007    are met:
00008     1. Redistributions of source code must retain the above copyright
00009        notice, this list of conditions and the following disclaimer.
00010     2. Redistributions in binary form must reproduce the above copyright
00011        notice, this list of conditions and the following disclaimer in the
00012        documentation and/or other materials provided with the distribution.
00013     3. Neither the name of the copyright holders nor the names of its
00014        contributors may be used to endorse or promote products derived from
00015        this software without specific prior written permission.
00016 
00017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00027    THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 
00031 
00032 #include "cpunit_AssertionException.hpp"
00033 #include "cpunit_WrongSetupException.hpp"
00034 #include "cpunit_TestTreeNode.hpp"
00035 #include "cpunit_StringFlyweightStore.hpp"
00036 #include "cpunit_trace.hpp"
00037 
00038 #include <sstream>
00039 
00040 cpunit::TestTreeNode::TestTreeNode(const std::string &l_name)
00041   : tests()
00042   , children()
00043   , setUp(NULL)
00044   , tearDown(NULL)
00045   , parent(NULL)
00046   , local_name(StringFlyweightStore::get_instance().intern(l_name))
00047   , counter()
00048 {
00049   CPUNIT_ITRACE("TestTreeNode created - local name: '"<<*local_name<<'\'');
00050 }
00051 
00052 cpunit::TestTreeNode::~TestTreeNode() {
00053   TestMap::iterator tit = tests.begin();
00054   while (tit != tests.end()) {
00055     delete tit->second;
00056     tit++;
00057   }
00058 
00059   ChildMap::iterator cit = children.begin();
00060   while (cit != children.end()) {
00061     delete cit->second;
00062     cit++;
00063   }
00064 
00065   delete setUp;
00066   delete tearDown;
00067 }
00068 
00069 std::string 
00070 cpunit::TestTreeNode::get_path() const {
00071   std::string path("");
00072   if (parent != NULL) {
00073     path = parent->get_path() + "::";
00074   }
00075   path += *local_name;
00076   return path;
00077 }
00078 
00079 std::string 
00080 cpunit::TestTreeNode::get_local_name() const {
00081   return *local_name;
00082 }
00083 
00084 void 
00085 cpunit::TestTreeNode::register_set_up(Callable *s) {
00086   CPUNIT_DTRACE("TestTreeNode::register_set_up called in '"<<get_path()<<"' with method "<<s->get_reg_info().get_name());
00087   if (setUp != NULL) {
00088     std::stringstream msg;
00089     msg<<"The set up method '"<<s->get_reg_info().to_string()<<"' already exists in the namespace "<<get_path()<<", registered at "<<setUp->get_reg_info().to_string();
00090     delete s;
00091     throw WrongSetupException(msg.str());
00092   }
00093   setUp = s;
00094 }
00095 
00096 void 
00097 cpunit::TestTreeNode::register_tear_down(Callable *t) {
00098   CPUNIT_DTRACE("TestTreeNode::register_tear_down called in '"<<get_path()<<"' with method "<<t->get_reg_info().get_name());
00099   if (tearDown != NULL) {
00100     std::stringstream msg;
00101     msg<<"The tear down method '"<<t->get_reg_info().to_string()<<"' already exists in the namespace, registered at "<<tearDown->get_reg_info().to_string();
00102     delete t;
00103     throw WrongSetupException(msg.str());
00104   }
00105   tearDown = t;
00106 }
00107 
00108 void 
00109 cpunit::TestTreeNode::add_test(Callable *test) {
00110   CPUNIT_DTRACE("TestTreeNode::add_test called in '"<<get_path()<<"' with method "<<test->get_reg_info().get_name());
00111   if(tests.find(test->get_reg_info().get_name()) != tests.end()) {
00112     std::stringstream msg;
00113     msg<<"The test '"<<test->get_reg_info().to_string()<<"' already exists in the namespace, registered at "<<tests.find(test->get_reg_info().get_name())->second->get_reg_info().to_string();
00114     delete test;
00115     throw WrongSetupException(msg.str());
00116   }
00117   tests.insert(std::make_pair(test->get_reg_info().get_name(), test));
00118 }
00119 
00120 void cpunit::TestTreeNode::add_child(TestTreeNode *child) {
00121   if(children.find(child->get_local_name()) != children.end()) {
00122     std::stringstream msg;
00123     msg<<"The namespace '"<<child->get_local_name()<<"' already exists in the namespace "<<get_path();
00124     delete child;
00125     throw WrongSetupException(msg.str());
00126   }
00127   children.insert(std::make_pair(child->get_local_name(), child));
00128 }
00129 
00130 std::map<std::string, cpunit::TestTreeNode*> 
00131 cpunit::TestTreeNode::get_children() {
00132   return children;
00133 }
00134 
00135 void 
00136 cpunit::TestTreeNode::extract_matches(std::vector<TestUnit>& result, const GlobMatcher& m) {
00137   TestMap::iterator tit = tests.begin();
00138   while (tit != tests.end()) {
00139     std::string full_name = tit->second->get_reg_info().get_path() + "::" + tit->second->get_reg_info().get_name();
00140     CPUNIT_DTRACE("Checking match for '"<<full_name<<'\'');
00141     if (m.matches(full_name)) {
00142       CPUNIT_DTRACE("MATCH!");
00143       result.push_back(TestUnit(setUp, tearDown, tit->second));
00144     }
00145     tit++;
00146   }
00147 
00148   ChildMap::iterator cit = children.begin();
00149   while (cit != children.end()) {
00150     cit->second->extract_matches(result, m);
00151     cit++;
00152   }
00153 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines