1 #ifndef TUT_RUNNER_H_GUARD
2 #define TUT_RUNNER_H_GUARD
7 #include "tut_exception.hpp"
14 * Test group operations.
22 // execute tests iteratively
23 virtual void rewind() = 0;
24 virtual bool run_next(test_result &) = 0;
27 virtual bool run_test(int n, test_result &tr) = 0;
32 * Test runner callback interface.
33 * Can be implemented by caller to update
34 * tests results in real-time. User can implement
35 * any of callback methods, and leave unused
36 * in default implementation.
41 * Default constructor.
48 * Virtual destructor is a must for subclassed types.
55 * Called when new test run started.
57 virtual void run_started()
62 * Called when a group started
63 * @param name Name of the group
65 virtual void group_started(const std::string& /*name*/)
70 * Called when a test finished.
71 * @param tr Test results.
73 virtual void test_completed(const test_result& /*tr*/)
78 * Called when a group is completed
79 * @param name Name of the group
81 virtual void group_completed(const std::string& /*name*/)
86 * Called when all tests in run completed.
88 virtual void run_completed()
92 callback(const callback &);
93 void operator=(const callback&);
97 * Typedef for runner::list_groups()
99 typedef std::vector<std::string> groupnames;
100 typedef std::set<callback*> callbacks;
118 * Stores another group for getting by name.
120 void register_group(const std::string& name, group_base* gr)
124 throw tut_error("group shall be non-null");
127 if (groups_.find(name) != groups_.end())
129 std::string msg("attempt to add already existent group " + name);
130 // this exception terminates application so we use cerr also
131 // TODO: should this message appear in stream?
132 std::cerr << msg << std::endl;
133 throw tut_error(msg);
136 groups_.insert( std::make_pair(name, gr) );
139 void set_callback(callback *cb)
146 * Stores callback object.
148 void insert_callback(callback* cb)
152 callbacks_.insert(cb);
156 void erase_callback(callback* cb)
158 callbacks_.erase(cb);
161 void clear_callbacks()
167 * Returns callback list.
169 const callbacks &get_callbacks() const
174 void set_callbacks(const callbacks &cb)
180 * Returns list of known test groups.
182 const groupnames list_groups() const
185 const_iterator i = groups_.begin();
186 const_iterator e = groups_.end();
189 ret.push_back(i->first);
196 * Runs all tests in all groups.
197 * @param callback Callback object if exists; null otherwise
199 void run_tests() const
203 const_iterator i = groups_.begin();
204 const_iterator e = groups_.end();
207 cb_group_started_(i->first);
208 run_all_tests_in_group_(i);
209 cb_group_completed_(i->first);
218 * Runs all tests in specified group.
220 void run_tests(const std::string& group_name) const
224 const_iterator i = groups_.find(group_name);
225 if (i == groups_.end())
228 throw no_such_group(group_name);
231 cb_group_started_(group_name);
232 run_all_tests_in_group_(i);
233 cb_group_completed_(group_name);
238 * Runs one test in specified group.
240 bool run_test(const std::string& group_name, int n, test_result &tr) const
244 const_iterator i = groups_.find(group_name);
245 if (i == groups_.end())
248 throw no_such_group(group_name);
251 cb_group_started_(group_name);
253 bool t = i->second->run_test(n, tr);
255 if(t && tr.result != test_result::dummy)
257 cb_test_completed_(tr);
260 cb_group_completed_(group_name);
268 typedef std::map<std::string, group_base*> groups;
269 typedef groups::iterator iterator;
270 typedef groups::const_iterator const_iterator;
273 callbacks callbacks_;
276 friend class restartable_wrapper;
278 void cb_run_started_() const
280 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
286 void cb_run_completed_() const
288 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
290 (*i)->run_completed();
294 void cb_group_started_(const std::string &group_name) const
296 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
298 (*i)->group_started(group_name);
302 void cb_group_completed_(const std::string &group_name) const
304 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
306 (*i)->group_completed(group_name);
310 void cb_test_completed_(const test_result &tr) const
312 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
314 (*i)->test_completed(tr);
318 void run_all_tests_in_group_(const_iterator i) const
323 while(i->second->run_next(tr))
325 if(tr.result != test_result::dummy)
327 cb_test_completed_(tr);
330 if (tr.result == test_result::ex_ctor)
332 // test object ctor failed, skip whole group
340 * Singleton for test_runner implementation.
341 * Instance with name runner_singleton shall be implemented
344 class test_runner_singleton
348 static test_runner& get()
350 static test_runner tr;
355 extern test_runner_singleton runner;