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)
71 * Called when a test finished.
72 * @param tr Test results.
74 virtual void test_completed(const test_result& tr)
80 * Called when a group is completed
81 * @param name Name of the group
83 virtual void group_completed(const std::string& name)
89 * Called when all tests in run completed.
91 virtual void run_completed()
95 virtual bool all_ok() const
100 callback(const callback &);
101 void operator=(const callback&);
105 * Typedef for runner::list_groups()
107 typedef std::vector<std::string> groupnames;
108 typedef std::set<callback*> callbacks;
128 * Stores another group for getting by name.
129 * @param name new group object
130 * @param gr new callback object
132 void register_group(const std::string& name, group_base* gr)
136 throw tut_error("group shall be non-null");
139 if (groups_.find(name) != groups_.end())
141 std::string msg("attempt to add already existent group " + name);
142 throw tut_error(msg);
145 groups_.insert( std::make_pair(name, gr) );
149 * Stores one callback object.
150 * @param cb new callback object
152 void set_callback(callback *cb)
159 * Add callback object.
160 * @param cb new callback object
162 void insert_callback(callback* cb)
166 callbacks_.insert(cb);
171 * Remove callback object.
172 * @param cb callback to remove
174 void erase_callback(callback* cb)
176 callbacks_.erase(cb);
180 * Remove all callback objects.
182 void clear_callbacks()
188 * Returns callback list.
189 * @return callback list
191 const callbacks &get_callbacks() const
198 * @param cb new callback list
200 void set_callbacks(const callbacks &cb)
206 * Returns list of known test groups.
207 * @return groups list
209 const groupnames list_groups() const
212 for(const_iterator i = groups_.begin(); i != groups_.end(); ++i)
214 ret.push_back(i->first);
220 * Runs all tests in all groups.
222 void run_tests() const
226 const_iterator i = groups_.begin();
227 const_iterator e = groups_.end();
230 cb_group_started_(i->first);
231 run_all_tests_in_group_(i);
232 cb_group_completed_(i->first);
241 * Runs all tests in specified group.
242 * @param group_name group to test
244 void run_tests(const std::string& group_name) const
248 const_iterator i = groups_.find(group_name);
249 if (i == groups_.end())
252 throw no_such_group(group_name);
255 cb_group_started_(group_name);
256 run_all_tests_in_group_(i);
257 cb_group_completed_(group_name);
262 * Runs one test in specified group.
263 * @param group_name group to test
264 * @param n run case in test
265 * @param tr result of this case
266 * @return true if test is ok, otherwise false
268 bool run_test(const std::string& group_name, int n, test_result &tr) const
272 const_iterator i = groups_.find(group_name);
273 if (i == groups_.end())
276 throw no_such_group(group_name);
279 cb_group_started_(group_name);
281 bool t = i->second->run_test(n, tr);
283 if(t && tr.result != test_result::dummy)
285 cb_test_completed_(tr);
288 cb_group_completed_(group_name);
296 typedef std::map<std::string, group_base*> groups;
297 typedef groups::iterator iterator;
298 typedef groups::const_iterator const_iterator;
301 callbacks callbacks_;
304 friend class restartable_wrapper;
306 void cb_run_started_() const
308 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
314 void cb_run_completed_() const
316 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
318 (*i)->run_completed();
322 void cb_group_started_(const std::string &group_name) const
324 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
326 (*i)->group_started(group_name);
330 void cb_group_completed_(const std::string &group_name) const
332 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
334 (*i)->group_completed(group_name);
338 void cb_test_completed_(const test_result &tr) const
340 for(callbacks::const_iterator i = callbacks_.begin(); i != callbacks_.end(); ++i)
342 (*i)->test_completed(tr);
346 void run_all_tests_in_group_(const_iterator i) const
351 while(i->second->run_next(tr))
353 if(tr.result != test_result::dummy)
355 cb_test_completed_(tr);
358 if (tr.result == test_result::ex_ctor)
360 // test object ctor failed, skip whole group
368 * Singleton for test_runner implementation.
369 * Instance with name runner_singleton shall be implemented
372 class test_runner_singleton
376 static test_runner& get()
378 static test_runner tr;
383 extern test_runner_singleton runner;
387 #endif // TUT_RUNNER_H_GUARD