- ////////////////////////////////////////////////////////////////////////////////
- //
- // KCPP UNITTEST Header File
- //
- #ifndef KCPP_UNITTEST_HPP
- #define KCPP_UNITTEST_HPP
-
- #include <vector>
-
- #include <kcpp_assert.hpp>
-
-
- namespace kcpp
- {
-
- /**
- * テストケース。
- * 各テストクラスは、本クラスを継承して作成ください。
- *
- * 以下実装例
- *
- * @code
- * #include <kcpp_unittest.hpp>
- *
- * using namespace kcpp;
- * class SampleTest : public TestCase
- * {
- * public:
- * SampleTest() {}
- * ~SampleTest() {}
- * void setUp()
- * { // 各テストケース実行前に実施する内容を記載する。
- * }
- * void tearDown()
- * { // 各テストケース実行後に実施する内容を記載する。
- * }
- * void testSample1()
- * { // テスト内容を記述する。
- * std::string tmp("ABC");
- * Asssert::assertEquals("ABC", tmp);
- * }
- * void testSample2()
- * { // テスト内容を記述する。
- * }
- * void run()
- * {
- * RUN_TEST(testSample1, "sample1 test");
- * RUN_TEST(testSample2, "sample2 test");
- * }
- * };
- * @endcode
- */
- class TestCase
- {
- public:
- TestCase();
- virtual ~TestCase();
- virtual void setUp();
- virtual void tearDown();
- virtual void run() = 0;
- };
-
-
-
- /**
- * 単体試験を管理するクラス。
- * 本クラスは、単体テストにて使用されます。
- * 通常、本クラスのインスタンスを生成する必要はありません。
- */
- class UnittestManager
- {
- public:
- UnittestManager();
- virtual ~UnittestManager();
- void addResult(const char* msg, const char* funcName,
- const char* file, int line, bool result, const AssertError* e = nullptr) noexcept;
- void printResult();
- private:
- int okCount;
- int ngCount;
- };
-
-
- // UnittestManager
- extern UnittestManager utManager;
-
-
- #define RUN_TEST(func, msg) { \
- setUp(); \
- try \
- { \
- func(); \
- kcpp::utManager.addResult(msg, #func, __FILE__, __LINE__, true); \
- } \
- catch (kcpp::AssertError& e) \
- { \
- kcpp::utManager.addResult(msg, #func, __FILE__, __LINE__, false, &e); \
- } \
- catch (...) \
- { \
- kcpp::utManager.addResult(msg, #func, __FILE__, __LINE__, false); \
- } \
- tearDown(); \
- }
-
- }
-
-
- #endif // KCPP_UNITTEST_HPP