Newer
Older
c-interpreter / modules / libkcpp / src / kcpp_unittest.cpp
Nomura Kei on 9 Aug 2023 2 KB UPDATE
////////////////////////////////////////////////////////////////////////////////
//
// Unit Test Module
//

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

#include <kcpp_unittest.hpp>


namespace kcpp
{

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

	/*
	 * テストケースクラスを構築します。
	 */
	TestCase::TestCase()
	{
		// NOP
	}


	/*
	 * テストケースクラスを破棄します。
	 */
	TestCase::~TestCase()
	{
		// NOP
	}


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


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



	////////////////////////////////////////////////////////////////////////////
	//
	// UnittestManager
	//

	/**
	 * テスト管理クラスを構築します。
	 */
	UnittestManager::UnittestManager() : okCount(0), ngCount(0)
	{
		// NOP
	}


	/**
	 * テスト管理クラスを破棄します。
	 */
	UnittestManager::~UnittestManager()
	{
		// NOP
	}


	/**
	 * テスト結果を出力します。
	 *
	 * @param msg      メッセージ
	 * @param funcName テスト関数名
	 * @param file     テスト実行呼び出し元ファイル名
	 * @param line     テスト実行呼び出し元行番号
	 * @param result   テスト結果
	 * @param e        エラー情報
	 */
	void UnittestManager::addResult(const char* msg, const char* funcName,
		const char* file, int line, bool result, const AssertError* e) noexcept
	{
		std::ostringstream testInfo;
		testInfo << file << ":" << line << " (" << funcName << ") " << msg;
		std::cout << "["
			<< std::setw(5)  << std::setfill('0') << std::right << (okCount + ngCount + 1) << "] "
			<< std::setw(64) << std::setfill(' ') << std::left  << testInfo.str();
		if (result)
		{
			std::cout << " [  OK  ]" << std::endl;
			okCount++;
		}
		else
		{
			std::cout << " [  NG  ]" << std::endl;
			if (e != nullptr)
			{
				std::cout << "AssertError:" << e->what() << std::endl;
				std::cout << "  at " << e->getFile()
							<< ":" << e->getLine()
							<< " (" << e->getFunc() << ")" << std::endl;
			}
			ngCount++;
		}
	}


	/**
	 * テスト結果まとめを出力します。
	 */
	void UnittestManager::printResult()
	{
		std::cout << std::endl;
		std::cout << "----------------" << std::endl;
		std::cout << "     OK : " << std::setw(5) << std::right << okCount << std::endl;
		std::cout << "     NG : " << std::setw(5) << std::right << ngCount << std::endl;
		std::cout << "  Total : " << std::setw(5) << std::right << (okCount + ngCount) << std::endl;
		std::cout << "----------------" << std::endl;
		std::cout << std::endl;

	}



	/**
	 * UnittestManager のインスタンス。
	 */
	UnittestManager utManager;
}