Difference between revisions of "Patch TUT"
Jump to navigation
Jump to search
Line 255: | Line 255: | ||
result_type result; | result_type result; | ||
</pre> | </pre> | ||
Revision as of 10:45, 9 December 2008
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-2006-11-04
WARNING: This is a patch to a pretty old version of tut, but currently the version in use.
--- 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
TUT-2008-11-30
Internally, TUT has been migrated to the 2008-11-30. This patch will not work against the current code, but is expected to be in use soon.
--- tut.orig/tut_assert.hpp 2008-12-05 17:35:10.000000000 -0800 +++ tut.new/tut_assert.hpp 2008-12-05 17:37:56.000000000 -0800 @@ -154,9 +154,23 @@ throw failure(msg); } +/** + * Skip test because of known failure. + */ +void skip(const char* msg = "") +{ + throw skip_failure(msg); +} + +void skip(const std::string& msg) +{ + throw skip_failure(msg); +} + } // end of namespace } + #endif diff -u tut.orig/tut_exception.hpp tut.new/tut_exception.hpp --- tut.orig/tut_exception.hpp 2008-12-05 17:35:10.000000000 -0800 +++ tut.new/tut_exception.hpp 2008-12-05 17:37:56.000000000 -0800 @@ -201,6 +201,26 @@ const test_result tr; }; +/** + * Exception to be throwed when skip_fail() is called. + */ +struct skip_failure : public failure +{ + skip_failure(const std::string& msg) + : failure(msg) + { + } + + test_result::result_type result() const + { + return test_result::skip; + } + + ~skip_failure() throw() + { + } +}; + } #endif diff -u tut.orig/tut_result.hpp tut.new/tut_result.hpp --- tut.orig/tut_result.hpp 2008-12-05 17:35:10.000000000 -0800 +++ tut.new/tut_result.hpp 2008-12-05 17:37:56.000000000 -0800 @@ -51,6 +51,7 @@ * ex - test throwed an exceptions * warn - test finished successfully, but test destructor throwed * term - test forced test application to terminate abnormally + * skip - test skpped because it is a known failure case */ enum result_type { @@ -60,7 +61,8 @@ warn, term, ex_ctor, - rethrown + rethrown, + skip, }; result_type result;