Newer
Older
libj / modules / j / cppunit / src / cppunit.cpp
#include <memory>
#include <sstream>
#include <iostream>
#include <iomanip>

#include <j/io/term.hpp>
#include <j/cppunit/cppunit.hpp>

using namespace j::io::Term;

namespace j
{
    namespace cppunit
    {

        ////////////////////////////////////////////////////////////////////////
        //
        // TestCase
        //

        /**
         * TestCase を構築します。
         */
        TestCase::TestCase() noexcept { /* NOP */ }

        /**
         * TestCase を破棄します。
         */
        TestCase::~TestCase() noexcept { /* NOP */ }

        /**
         * 各テストケース実施前に実行されます。
         */
        void TestCase::setUp() { /* NOP */ }

        /**
         * 各テストケース実施後に実行されます。
         */
        void TestCase::tearDown() { /* NOP */ }

        ////////////////////////////////////////////////////////////////////////
        //
        // TestManager
        //

        /**
         * TestManager を構築します。
         */
        TestManager::TestManager() : okCount(0), ngCount(0)
        {
            // NOP
        }

        /**
         * TestManager を破棄します。
         * 破棄するタイミングで、テスト結果を出力します。
         */
        TestManager::~TestManager()
        {
            // NOP
        }

        /**
         * テスト結果を追加します。
         *
         * @param file ファイル
         */
        void TestManager::addTestResult(const char *title, const char *func, const char *file, int line, bool result, const j::lang::AssertionError *e)
        {
            std::ostringstream msg;
            msg << title << " [" << func << "] ";
            std::cout << BLD << Color::CYN << std::setw(5) << std::setfill('0') << std::right << (okCount + ngCount + 1) << " ";
            std::cout << BLD << Color::CYN << std::setw(84) << std::setfill(' ') << std::left << msg.str() << Color::DEF << CLR;

            if (result)
            { // テスト成功
                std::cout << BLD << Color::CYN << "[   " << Color::GRN << "OK" << Color::CYN << "   ]" << Color::DEF << CLR << std::endl;
                okCount++;
            }
            else
            { // テスト失敗
                std::cout << BLD << Color::CYN << "[   " << Color::RED << "NG" << Color::CYN << "   ]" << Color::DEF << CLR << std::endl;
                ngCount++;
                if (e)
                {
                    std::cerr << BLD << Color::YEL << e->toString() << Color::DEF << CLR << std::endl;
                    std::cerr << BLD << Color::YEL << "\tat " << file << ":" << line << " [RUN_TEST]" << Color::DEF << CLR << std::endl;
                }
            }
        }

        /**
         * テスト結果を出力します。
         */
        void TestManager::printResult()
        {
            std::string line(80, '-');
            std::cout << BLD << Color::CYN << line << Color::DEF << CLR << std::endl;
            std::cout << BLD << Color::GRN << " Success : " << okCount << Color::DEF << CLR << std::endl;
            std::cout << BLD << Color::RED << " Failure : " << ngCount << Color::DEF << CLR << std::endl;
            std::cout << BLD << Color::CYN << " Total   : " << (okCount + ngCount) << Color::DEF << CLR << std::endl;
            std::cout << std::endl;
        }

        // テスト実施用インスタンス
        TestManager testManager;

    } // namespace cppunit
} // namespace j