CPUnit 0.7 (beta)
The REAL C++ port of JUnit.
|
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 "unittest_TestStore.hpp" 00033 #include "unittest_TestTreeNode.hpp" 00034 #include "unittest_TestUnit.hpp" 00035 #include "unittest_GlobMatcher.hpp" 00036 #include "unittest_trace.hpp" 00037 00038 unittest::TestStore *unittest::TestStore::INSTANCE(NULL); 00039 00040 unittest::TestStore::TestStore() 00041 : root(new TestTreeNode("")) 00042 {} 00043 00044 unittest::TestStore::~TestStore() 00045 {} 00046 00047 void 00048 unittest::TestStore::dispose() { 00049 UNITTEST_DTRACE("TestStore::dispose()"); 00050 delete INSTANCE; 00051 INSTANCE = NULL; 00052 UNITTEST_DTRACE("TestStore - disposed."); 00053 } 00054 00055 00056 unittest::TestStore& unittest::TestStore::get_instance() { 00057 if (INSTANCE == NULL) { 00058 INSTANCE = new TestStore; 00059 } 00060 return *INSTANCE; 00061 } 00062 00063 void 00064 unittest::TestStore::insert_set_up(Callable *su) { 00065 UNITTEST_ITRACE("TestStore::insert_set_up for "<<su->get_reg_info().get_path()); 00066 TestTreeNode *n = find_node(su->get_reg_info().get_path(), true); 00067 n->register_set_up(su); 00068 } 00069 00070 void 00071 unittest::TestStore::insert_tear_down(Callable *td) { 00072 UNITTEST_ITRACE("TestStore::insert_tear_down for "<<td->get_reg_info().get_path()); 00073 TestTreeNode *n = find_node(td->get_reg_info().get_path(), true); 00074 n->register_tear_down(td); 00075 } 00076 00077 void 00078 unittest::TestStore::insert_test(Callable *test) { 00079 UNITTEST_ITRACE("TestStore::insert_test in "<<test->get_reg_info().get_path()<<": "<<test->get_reg_info().get_name()); 00080 TestTreeNode *n = find_node(test->get_reg_info().get_path(), true); 00081 n->add_test(test); 00082 } 00083 00084 std::vector<unittest::TestUnit> 00085 unittest::TestStore::get_test_units(const std::string pattern) { 00086 std::vector<TestUnit> result; 00087 GlobMatcher m(pattern); 00088 root->extract_matches(result, m); 00089 return result; 00090 } 00091 00092 std::vector<unittest::RegInfo> 00093 unittest::TestStore::get_tests(const std::string pattern) { 00094 std::vector<TestUnit> tus = get_test_units(pattern); 00095 std::vector<RegInfo> result(tus.size()); 00096 for (std::size_t i=0; i<tus.size(); i++) { 00097 result[i] = tus[i].get_test()->get_reg_info(); 00098 } 00099 return result; 00100 } 00101 00102 std::vector<std::string> 00103 unittest::TestStore::decompose_path(const std::string& path) const { 00104 static const std::string sep("::"); 00105 00106 std::vector<std::string> result; 00107 00108 std::string str = path; 00109 if (str.find(sep) == 0) { 00110 str = str.substr(sep.length()); 00111 } 00112 00113 std::size_t index; 00114 while ((index = str.find(sep)) != std::string::npos) { 00115 result.push_back(str.substr(0, index)); 00116 str = str.substr(index + sep.length()); 00117 } 00118 if (str.length() > 0) { 00119 result.push_back(str); 00120 } 00121 return result; 00122 } 00123 00124 unittest::TestTreeNode* 00125 unittest::TestStore::find_node(const std::string& path, const bool create_nonexisting) { 00126 UNITTEST_DTRACE("TestStore::find_node("<<path<<", "<<create_nonexisting<<')'); 00127 std::vector<std::string> path_elts = decompose_path(path); 00128 TestTreeNode *current = root.get(); 00129 for (std::size_t i=0; i<path_elts.size(); i++) { 00130 UNITTEST_DTRACE("TestStore::find_node - i="<<i<<" path_elts["<<i<<"]='"<<path_elts[i]<<"' current='"<<current->get_path()<<'\''); 00131 std::map<std::string, TestTreeNode*> children = current->get_children(); 00132 std::map<std::string, TestTreeNode*>::iterator next = children.find(path_elts[i]); 00133 if (next != children.end()) { 00134 UNITTEST_DTRACE("TestStore::find_node - has next"); 00135 current = next->second; 00136 } else if (create_nonexisting) { 00137 UNITTEST_DTRACE("TestStore::find_node - creating next: '"<<path_elts[i]<<'\''); 00138 TestTreeNode *next_child = new TestTreeNode(path_elts[i]); 00139 current->add_child(next_child); 00140 current = next_child; 00141 } else { 00142 UNITTEST_DTRACE("TestStore::find_node - returning NULL."); 00143 return NULL; 00144 } 00145 } 00146 UNITTEST_DTRACE("TestStore::find_node - returning '"<<current->get_path()<<'\''); 00147 return current; 00148 } 00149