X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/3156083fd0c328d46be22536720ae33e1ab48090..8358e7752bdb1edf2178f4f3c22018237eaf785f:/tests/tut/tut_cppunit_reporter.hpp?ds=sidebyside diff --git a/tests/tut/tut_cppunit_reporter.hpp b/tests/tut/tut_cppunit_reporter.hpp index 3b9f38c1..62646b09 100644 --- a/tests/tut/tut_cppunit_reporter.hpp +++ b/tests/tut/tut_cppunit_reporter.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace tut { @@ -16,72 +17,60 @@ namespace tut */ class cppunit_reporter : public tut::callback { - private: - std::vector failed_tests; - std::vector passed_tests; - std::string filename; + std::vector failed_tests_; + std::vector passed_tests_; + const std::string filename_; + std::auto_ptr stream_; - std::string encode(const std::string & text) - { - std::string out; - for (unsigned int i=0; i': - out += ">"; - break; - case '&': - out += "&"; - break; - case '\'': - out += "'"; - break; - case '"': - out += """; - break; - default: - out += c; - } - } - - return out; - } + cppunit_reporter(const cppunit_reporter &); + cppunit_reporter &operator=(const cppunit_reporter &); public: + explicit cppunit_reporter(const std::string &filename = "testResult.xml") + : failed_tests_(), + passed_tests_(), + filename_(filename), + stream_(new std::ofstream(filename_.c_str())) + { + if (!stream_->good()) { + throw tut_error("Cannot open output file `" + filename_ + "`"); + } + } - cppunit_reporter(const std::string & _filename = "") + explicit cppunit_reporter(std::ostream &stream) + : failed_tests_(), + passed_tests_(), + filename_(), + stream_(&stream) { - setFilename(_filename); } - void setFilename(const std::string & _filename) + ~cppunit_reporter() { - if (_filename == "") - { - filename = "testResult.xml"; - } - else + if(filename_.empty()) { - filename = _filename; + stream_.release(); } } void run_started() { - failed_tests.clear(); - passed_tests.clear(); + failed_tests_.clear(); + passed_tests_.clear(); } void test_completed(const tut::test_result& tr) { - if (tr.result == test_result::ok) { - passed_tests.push_back(tr); - } else { - failed_tests.push_back(tr); + assert(tr.result != test_result::dummy ); + if ( (tr.result == test_result::ok) || + (tr.result == test_result::skipped) ) + { + passed_tests_.push_back(tr); + } + else + { + failed_tests_.push_back(tr); } } @@ -91,23 +80,18 @@ public: int failures = 0; std::string failure_type; std::string failure_msg; - std::ofstream xmlfile; - - xmlfile.open(filename.c_str(), std::ios::in | std::ios::trunc); - if (!xmlfile.is_open()) { - throw (std::runtime_error("Cannot open file for output")); - } - /* *********************** header ***************************** */ - xmlfile << "" << std::endl - << "" << std::endl; + *stream_ << "" << std::endl + << "" << std::endl; - /* *********************** failed tests ***************************** */ - if (failed_tests.size() > 0) { - xmlfile << " " << std::endl; + if (failed_tests_.size() > 0) + { + *stream_ << " " << std::endl; - for (unsigned int i=0; i" << std::endl - << " " << encode(failed_tests[i].group) + "::" + encode(failed_tests[i].name) << "" << std::endl - << " " << failure_type << "" << std::endl - << " " << std::endl - << " Unknown" << std::endl - << " Unknown" << std::endl - << " " << std::endl - << " " << encode(failure_msg + failed_tests[i].message) << "" << std::endl - << " " << std::endl; + *stream_ << " " << std::endl + << " " << encode(failed_tests_[i].group) + "::" + encode(failed_tests_[i].name) << "" << std::endl + << " " << failure_type << "" << std::endl + << " " << std::endl + << " Unknown" << std::endl + << " Unknown" << std::endl + << " " << std::endl + << " " << encode(failure_msg + failed_tests_[i].message) << "" << std::endl + << " " << std::endl; } - xmlfile << " " << std::endl; + *stream_ << " " << std::endl; } /* *********************** passed tests ***************************** */ - if (passed_tests.size() > 0) { - xmlfile << " " << std::endl; - - for (unsigned int i=0; i" << std::endl - << " " << encode(passed_tests[i].group) + "::" + encode(passed_tests[i].name) << "" << std::endl - << " " << std::endl; + if (passed_tests_.size() > 0) { + *stream_ << " " << std::endl; + + for (unsigned int i=0; i" << std::endl + << " " << encode(passed_tests_[i].group) + "::" + encode(passed_tests_[i].name) << "" << std::endl + << " " << std::endl; } - xmlfile << " " << std::endl; + *stream_ << " " << std::endl; } /* *********************** statistics ***************************** */ - xmlfile << " " << std::endl - << " " << (failed_tests.size() + passed_tests.size()) << "" << std::endl - << " " << failed_tests.size() << "" << std::endl - << " " << errors << "" << std::endl - << " " << failures << "" << std::endl - << " " << std::endl; + *stream_ << " " << std::endl + << " " << (failed_tests_.size() + passed_tests_.size()) << "" << std::endl + << " " << failed_tests_.size() << "" << std::endl + << " " << errors << "" << std::endl + << " " << failures << "" << std::endl + << " " << std::endl; /* *********************** footer ***************************** */ - xmlfile << "" << std::endl; - - xmlfile.close(); + *stream_ << "" << std::endl; } - bool all_ok() const + virtual bool all_ok() const { - return failed_tests.empty(); - }; + return failed_tests_.empty(); + } + /** + * \brief Encodes text to XML + * XML-reserved characters (e.g. "<") are encoded according to specification + * @param text text to be encoded + * @return encoded string + */ + static std::string encode(const std::string & text) + { + std::string out; + + for (unsigned int i=0; i': + out += ">"; + break; + case '&': + out += "&"; + break; + case '\'': + out += "'"; + break; + case '"': + out += """; + break; + default: + out += c; + } + } + return out; + } }; }