Patch TUT
Our unit test framework did not have any method for skipping tests. Sometimes known failure cases are found which require temporarily skipping tests. This patch adds that functionality.
--- tut-orig/tut.h 2007-05-23 16:37:00.000000000 -0700
+++ tut-new/tut.h 2007-05-23 16:40:18.000000000 -0700
@@ -79,6 +79,15 @@
};
/**
+ * Exception to be throwed when skip_fail() is called.
+ */
+ class skip_failure : public std::logic_error
+ {
+ public:
+ skip_failure(const std::string& msg) : std::logic_error(msg){};
+ };
+
+ /**
* Exception to be throwed when test desctructor throwed an exception.
*/
class warning : public std::logic_error
@@ -120,8 +129,20 @@
* ex - test throwed an exceptions
* warn - test finished successfully, but test destructor throwed
* term - test forced test application to terminate abnormally
+ * skip - test skipped
+ * skip_fail - test skpped because it is a known failure case
*/
- typedef enum { ok, fail, ex, warn, term, ex_ctor } result_type;
+ typedef enum
+ {
+ ok,
+ fail,
+ ex,
+ warn,
+ term,
+ ex_ctor,
+ skip,
+ skip_fail,
+ } result_type;
result_type result;
/**
@@ -168,7 +189,7 @@
// execute tests iteratively
virtual void rewind() = 0;
- virtual test_result run_next() = 0;
+ virtual test_result run_next(int skip_test_id = 0) = 0;
// execute one test
virtual test_result run_test(int n) = 0;
@@ -316,7 +337,7 @@
/**
* Runs all tests in specified group.
*/
- void run_tests(const std::string& group_name) const
+ void run_tests(const std::string& group_name, int skip_test_id = 0) const
{
callback_->run_started();
@@ -328,7 +349,7 @@
try
{
- run_all_tests_in_group_(i);
+ run_all_tests_in_group_(i, skip_test_id);
}
catch( const no_more_tests& )
{
@@ -371,12 +392,13 @@
}
private:
- void run_all_tests_in_group_(const_iterator i) const
+ void run_all_tests_in_group_(const_iterator i, int skip_test_id = 0) const
{
i->second->rewind();
for( ;; )
{
- test_result tr = i->second->run_next();
+ test_result tr = i->second->run_next(skip_test_id);
+
callback_->test_completed(tr);
if( tr.result == test_result::ex_ctor )
@@ -436,13 +458,11 @@
}
};
- namespace
- {
/**
* Tests provided condition.
* Throws if false.
*/
- void ensure(bool cond)
+ inline void ensure(bool cond)
{
if( !cond ) throw failure("");
}
@@ -511,10 +531,19 @@
/**
* Unconditionally fails with message.
*/
- void fail(const char* msg="")
+ template <class T>
+ void fail(const T msg)
{
throw failure(msg);
}
+
+ /**
+ * Unconditionally fails with message.
+ */
+ template <class T>
+ void skip_fail(const T msg)
+ {
+ throw skip_failure(msg);
}
/**
@@ -705,7 +734,7 @@
/**
* Runs next test.
*/
- test_result run_next()
+ test_result run_next(int skip_test_id = 0)
{
if( current_test_ == tests_.end() )
{
@@ -718,8 +747,17 @@
{
try
{
+ if (current_test_->first == skip_test_id)
+ {
+ test_result tr(name_,current_test_->first,test_result::skip);
+ current_test_++;
+ return tr;
+ }
+ else
+ {
return run_test_(current_test_++,obj);
}
+ }
catch( const no_such_test& )
{
continue;
@@ -774,6 +812,12 @@
test_result tr(name_,ti->first,test_result::fail,ex);
return tr;
}
+ catch(const skip_failure& ex)
+ {
+ // test failed because of ensure() or similar method
+ test_result tr(name_,ti->first,test_result::skip_fail,ex);
+ return tr;
+ }
catch(const seh& ex)
{
// test failed with sigsegv, divide by zero, etc
Save this patch as tut.patch in the same directory as tut.h and apply it from a terminal program like xterm or cygwin with the command:
$ patch -p1 < tut.patch