📄 textuiobserver.hpp
字号:
//// This file is part of the "More for C++" library//// Copyright (c) 1999-2003 by Thorsten Goertz (thorsten@morefor.org)//// The "More for C++" library is free software; you can redistribute it and/or// modify it under the terms of the license that comes with this package.//// Read "license.txt" for more details.//// THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED// WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES// OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.////////////////////////////////////////////////////////////////////////////////#ifndef MORE_TEST_TEXTUIOBSERVER_HPP#define MORE_TEST_TEXTUIOBSERVER_HPP////////////////////////////////////////////////////////////////////////////////#include <cstring>#include <cstdio>#include <iostream.hpp>#include <more/test/testobserver.hpp>////////////////////////////////////////////////////////////////////////////////namespace more{ namespace test { class TextUIObserver: public TestObserver { public: virtual void onBeginTest( const char* pcNameOfTestSuite, size_t nNoOfTestCases, size_t nNestingLevel ); virtual void onTestCompleted( size_t nNestingLevel ); virtual void onTestCaseSucceeded( const char* pcNameOfTestSuite, const char* pcNameOfTestCase ); virtual void onSetupFailed( const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails); virtual void onTestCaseFailed( const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails ); virtual void onException( const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails ); virtual void onTeardownFailed( const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails); private: enum TypeOfFailure { TEST_UI_SETUP, TEST_UI_FAILURE, TEST_UI_EXCEPTION, TEST_UI_TEARDOWN }; struct Description { Description* pNextDescription; TypeOfFailure eTypeOfFailure; char* pcNameOfTestSuite; char* pcNameOfTestCase; char* pcMessage; char* pcDetails; }; virtual void addDescription( TypeOfFailure eTypeOfFailure, const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails ); virtual void printDescription( Description* ); virtual char* createDuplicatedString( const char* pcString ); virtual void deleteDuplicatedString( char* pcString ); bool m_bLastCalledTestCaseSucceded; size_t m_nNoOfSuccessfulTestCases; size_t m_nNoOfFailingSetups; size_t m_nNoOfFailingTestCases; size_t m_nNoOfExceptions; size_t m_nNoOfFailingTeardowns; Description* m_pFirstDescription; Description* m_pLastDescription; }; }}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::onBeginTest( const char* pcNameOfTestSuite, size_t nNoOfTestCases, size_t nNestingLevel){ if( nNestingLevel == 0 ) { std::cerr << "Starting test \"" << pcNameOfTestSuite; std::cerr << "\" with " << nNoOfTestCases; if( nNoOfTestCases == 1 ) { std::cerr << " test case:" << std::endl; } else { std::cerr << " test cases:" << std::endl; } std::cerr << std::endl; m_bLastCalledTestCaseSucceded = false; m_nNoOfSuccessfulTestCases = 0; m_nNoOfFailingSetups = 0; m_nNoOfFailingTestCases = 0; m_nNoOfExceptions = 0; m_nNoOfFailingTeardowns = 0; m_pFirstDescription = 0; m_pLastDescription = 0; }}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::onTestCompleted( size_t nNestingLevel){ if( nNestingLevel == 0 ) { std::cerr << std::endl << std::endl << std::endl; if( m_pFirstDescription != 0 ) { Description* pDescription = m_pFirstDescription; std::cerr << "Description:" << std::endl << std::endl; while( pDescription != 0 ) { Description* pNextDescription; printDescription( pDescription ); pNextDescription = pDescription -> pNextDescription; deleteDuplicatedString( pDescription -> pcNameOfTestSuite ); deleteDuplicatedString( pDescription -> pcNameOfTestCase ); deleteDuplicatedString( pDescription -> pcMessage ); deleteDuplicatedString( pDescription -> pcDetails ); delete pDescription; pDescription = pNextDescription; } std::cerr << std::endl; } std::cerr << "Conclusion:" << std::endl << std::endl; if( m_nNoOfSuccessfulTestCases == 1 ) { std::cerr << "1 test case was successful" << std::endl; } else { std::cerr << m_nNoOfSuccessfulTestCases << " test cases were successful" << std::endl; } if( m_nNoOfFailingSetups == 1 ) { std::cerr << "1 setup failed" << std::endl; } else if( m_nNoOfFailingSetups > 1 ) { std::cerr << m_nNoOfFailingSetups << " setups failed" << std::endl; } if( m_nNoOfFailingTestCases == 1 ) { std::cerr << "1 test case failed" << std::endl; } else { std::cerr << m_nNoOfFailingTestCases << " test cases failed" << std::endl; } if( m_nNoOfExceptions == 1 ) { std::cerr << "1 test case threw an exception" << std::endl << std::endl; } else { std::cerr << m_nNoOfExceptions << " test cases threw an exception" << std::endl; } if( m_nNoOfFailingTeardowns == 1 ) { std::cerr << "1 teardown failed" << std::endl; } else if( m_nNoOfFailingTeardowns > 1 ) { std::cerr << m_nNoOfFailingTeardowns << " teardowns failed" << std::endl; } std::cerr << std::endl << std::endl << "Press <Enter> to continue..." << std::endl; getchar( ); }}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::onTestCaseSucceeded( const char* /* pcNameOfTestSuite */, const char* /* pcNameOfTestCase */){ std::cerr << "."; m_bLastCalledTestCaseSucceded = true; m_nNoOfSuccessfulTestCases++;}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::onSetupFailed( const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails){ std::cerr << "S"; addDescription( TEST_UI_SETUP, pcNameOfTestSuite, pcNameOfTestCase, pcMessage, pcDetails ); m_bLastCalledTestCaseSucceded = false; m_nNoOfFailingSetups++;}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::onTestCaseFailed( const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails){ std::cerr << "F"; addDescription( TEST_UI_FAILURE, pcNameOfTestSuite, pcNameOfTestCase, pcMessage, pcDetails ); m_bLastCalledTestCaseSucceded = false; m_nNoOfFailingTestCases++;}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::onException( const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails){ std::cerr << "E"; addDescription( TEST_UI_EXCEPTION, pcNameOfTestSuite, pcNameOfTestCase, pcMessage, pcDetails ); m_bLastCalledTestCaseSucceded = false; m_nNoOfExceptions++;}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::onTeardownFailed( const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails){ std::cerr << "T"; addDescription( TEST_UI_TEARDOWN, pcNameOfTestSuite, pcNameOfTestCase, pcMessage, pcDetails ); m_bLastCalledTestCaseSucceded = false; m_nNoOfFailingTeardowns++;}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::addDescription( TypeOfFailure eTypeOfFailure, const char* pcNameOfTestSuite, const char* pcNameOfTestCase, const char* pcMessage, const char* pcDetails){ Description* pDescription = new Description( ); pDescription -> pNextDescription = 0; pDescription -> eTypeOfFailure = eTypeOfFailure; pDescription -> pcNameOfTestSuite = createDuplicatedString( pcNameOfTestSuite ); pDescription -> pcNameOfTestCase = createDuplicatedString( pcNameOfTestCase ); pDescription -> pcMessage = createDuplicatedString( pcMessage ); pDescription -> pcDetails = createDuplicatedString( pcDetails ); if( m_pFirstDescription == 0 ) { m_pFirstDescription = pDescription; m_pLastDescription = pDescription; } else { m_pLastDescription -> pNextDescription = pDescription; m_pLastDescription = pDescription; }}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::printDescription( Description* pDescription){ if( pDescription -> eTypeOfFailure == TEST_UI_SETUP ) { std::cerr << "setup of "; } else if( pDescription -> eTypeOfFailure == TEST_UI_TEARDOWN ) { std::cerr << "teardown of "; } std::cerr << "TestCase \"" << pDescription -> pcNameOfTestCase; std::cerr << "\" of Suite \"" << pDescription -> pcNameOfTestSuite; if( pDescription -> eTypeOfFailure == TEST_UI_EXCEPTION ) { std::cerr << "\" threw an exception:" << std::endl; } else { std::cerr << "\" failed:" << std::endl; } if( *pDescription -> pcMessage != 0 ) { std::cerr << pDescription -> pcMessage << std::endl; } if( *pDescription -> pcDetails != 0 ) { std::cerr << pDescription -> pcDetails << std::endl; } std::cerr << std::endl;}////////////////////////////////////////////////////////////////////////////////inline char* more::test::TextUIObserver::createDuplicatedString( const char* pcString){ char* pcResult; if( pcString == 0 ) { pcResult = new char[1]; pcResult[0] = '\0'; } else { int nLength = strlen( pcString ); pcResult = new char[nLength + 1]; strcpy( pcResult, pcString ); } return pcResult;}////////////////////////////////////////////////////////////////////////////////inline void more::test::TextUIObserver::deleteDuplicatedString( char* pcString){ delete[] pcString;}////////////////////////////////////////////////////////////////////////////////#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -