///////////////////////////////////////////////////////////////////// // MT2Q6.cpp - TestResults Container // // // // Jim Fawcett, CSE687 - OOD : Midterm Exam, Fall 2018 // ///////////////////////////////////////////////////////////////////// #include #include #include #include #include "../Utilities/StringUtilities/StringUtilities.h" template class ITestResult { public: virtual bool test() = 0; virtual std::string name() = 0; virtual std::string author() = 0; virtual std::string date() = 0; virtual bool result() = 0; virtual AppType appType() = 0; virtual ~ITestResult() {} }; template class TestResult : public ITestResult { public: virtual bool test() { return true; } virtual std::string name() { return name_; } void name(const std::string& name) { name_ = name; } void author(const std::string& author) { author_ = author; } virtual std::string author() { return author_; } void date(const std::string& date) { date_ = date; } virtual std::string date() { return date_; } void result(bool rslt) { result_ = rslt; } virtual bool result() { return result_; } void appType(const AppType& appType) { appType_ = appType; } AppType appType() { return appType_; } private: std::string name_; std::string author_; std::string date_; bool result_; AppType appType_; }; template class TestFormatter { public: void name(const std::string& name); void author(const std::string& author); void date(const std::string& date); void result(bool rslt); void appType(const AppType& appType); std::string format(); std::string format(ITestResult* pTestResult); private: std::string name_; std::string author_; std::string date_; bool result_; AppType appType_; }; template void TestFormatter::name(const std::string& name) { name_ = name; } template void TestFormatter::author(const std::string& author) { author_ = author; } template void TestFormatter::date(const std::string& date) { date_ = date; } template void TestFormatter::result(bool rslt) { result_ = rslt; } template void TestFormatter::appType(const AppType& appType) { appType_ = appType; } template std::string TestFormatter::format() { TestResult test; test.name(name_); test.author(author_); test.date(date_); test.result(result_); test.appType(appType_); return format(&test); } template std::string TestFormatter::format(ITestResult* pTestResult) { std::ostringstream frmted; frmted << std::left << "\n "; frmted << std::setw(20) << pTestResult->name(); frmted << std::setw(10) << pTestResult->author(); frmted << std::setw(14) << pTestResult->date(); std::string resultStr; if (pTestResult->result()) frmted << std::setw(8) << "passed"; else frmted << std::setw(8) << "failed"; frmted << " " << (pTestResult->appType()).toString(); return frmted.str(); } class LogContainer { public: LogContainer& add(const std::string& msg) { if (atString_.size() > 0) atString_ += ", " + msg; else atString_ += msg; return *this; } std::string toString() { return atString_; } private: std::string atString_; }; int main() { Utilities::Title("MT2Q6 - Test Formatter"); LogContainer lc; lc.add("first item").add("second item"); TestFormatter tf; tf.name("Testing formatter"); tf.author("Fawcett"); tf.date("01 Nov 2018"); tf.result(true); tf.appType(lc); std::cout << tf.format(); std::cout << "\n\n"; }