/////////////////////////////////////////////////////////////////////// // TestUtilitiesDemo.cpp - facilities for single-user test harness // // ver 1.1 // // Language: C++, Visual Studio 2017 // // Application: Most Projects, CSE687 - Object Oriented Design // // Author: Jim Fawcett, Syracuse University, CST 4-187 // // jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////// #include #include #include #include "TestUtilitiesDemo.h" std::ostream& out = std::cout; #ifdef TEST_TESTUTILITIESDEMO using namespace Utilities; /////////////////////////////////////////////////////////////////////// // define demo test functions bool test_always_passes() { return true; } bool test_always_fails() { return false; } bool test_always_throws() { std::exception ex("exception\n -- msg: this test always throws -- "); throw(ex); } // define a test functor that tests other code class CodeToTest { public: CodeToTest(const std::string& msg) : msg_(msg) {} std::string result() { return "result" + msg_; } ~CodeToTest() { out << "\n -- deleting CodeToTest\n"; } private: std::string msg_; }; class testCode // test functor { public: testCode(const std::string& msg) : msg_(msg) {} bool operator()() { out << "\n test input = \"" + msg_ << "\""; // logging test input CodeToTest ctt(msg_); std::string result = ctt.result(); out << "\n test output = \"" + result << "\""; //logging test output return (result == "result" + msg_); } private: std::string msg_; }; int main() { std::cout << "\n Testing TestUtilities Package"; std::cout << "\n ===============================\n"; TestExecutive ex; TestExecutive::TestItem ti1{ test_always_passes, "test_always_passes" }; TestExecutive::TestItem ti2{ test_always_fails, "test_always_fails" }; TestExecutive::TestItem ti3{ test_always_throws, "test_always_throws" }; TestExecutive::TestItem ti4{ testCode("#1"), "testCode #1" }; // creates temporary instance of functor TestExecutive::TestItem ti5{ testCode("#2"), "testCode #2" }; // ditto ex.registerTest(ti1); ex.registerTest(ti2); ex.registerTest(ti3); ex.registerTest(ti4); ex.registerTest(ti5); bool result = ex.doTests(); if (result == true) std::cout << "\n all tests passed"; else std::cout << "\n at least one test failed"; std::cout << "\n\n"; return 0; } #endif