📄 test_tools.cpp
字号:
// (C) Copyright Gennadiy Rozental 2001-2003.
// (C) Copyright Ullrich Koethe 2001.
// Use, modification, and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org/libs/test for the library home page.
//
// File : $RCSfile: test_tools.cpp,v $
//
// Version : $Revision: 1.25 $
//
// Description : supplies offline implemtation for the Test Tools
// ***************************************************************************
// Boost.Test
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test_result.hpp>
// BOOST
#include <boost/config.hpp>
// STL
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
# ifdef BOOST_NO_STDC_NAMESPACE
namespace std { using ::strcmp; using ::strncmp; using ::strlen; using ::isprint; }
# endif
namespace boost {
namespace test_toolbox {
namespace detail {
// ************************************************************************** //
// ************** TOOL BOX Implementation ************** //
// ************************************************************************** //
void
checkpoint_impl( wrap_stringstream& message, c_string_literal file_name, std::size_t line_num )
{
BOOST_UT_LOG_BEGIN( file_name, line_num, unit_test_framework::log_test_suites )
unit_test_framework::checkpoint( message.str() )
BOOST_UT_LOG_END
}
//____________________________________________________________________________//
void
message_impl( wrap_stringstream& message, c_string_literal file_name, std::size_t line_num )
{
BOOST_UT_LOG_BEGIN( file_name, line_num, unit_test_framework::log_messages )
message.str()
BOOST_UT_LOG_END
}
//____________________________________________________________________________//
void
warn_and_continue_impl( bool predicate, wrap_stringstream& message,
c_string_literal file_name, std::size_t line_num, bool add_fail_pass )
{
if( !predicate ) {
BOOST_UT_LOG_BEGIN( file_name, line_num, unit_test_framework::log_warnings )
(add_fail_pass ? "condition " : "") << message.str() << (add_fail_pass ? " is not satisfied" : "" )
BOOST_UT_LOG_END
}
else {
BOOST_UT_LOG_BEGIN( file_name, line_num, unit_test_framework::log_successful_tests )
"condition " << message.str() << " is satisfied"
BOOST_UT_LOG_END
}
}
//____________________________________________________________________________//
void
warn_and_continue_impl( extended_predicate_value const& v, wrap_stringstream& message, c_string_literal file_name, std::size_t line_num,
bool add_fail_pass )
{
warn_and_continue_impl( !!v,
message << (add_fail_pass && !v ? " is not satisfied. " : "" ) << *(v.p_message),
file_name, line_num, false );
}
//____________________________________________________________________________//
bool
test_and_continue_impl( bool predicate, wrap_stringstream& message,
c_string_literal file_name, std::size_t line_num,
bool add_fail_pass, unit_test_framework::log_level loglevel )
{
if( !predicate ) {
unit_test_framework::unit_test_result::instance().inc_failed_assertions();
BOOST_UT_LOG_BEGIN( file_name, line_num, loglevel )
(add_fail_pass ? "test " : "") << message.str() << (add_fail_pass ? " failed" : "")
BOOST_UT_LOG_END
return true;
}
else {
unit_test_framework::unit_test_result::instance().inc_passed_assertions();
BOOST_UT_LOG_BEGIN( file_name, line_num, unit_test_framework::log_successful_tests )
(add_fail_pass ? "test " : "") << message.str() << (add_fail_pass ? " passed" : "")
BOOST_UT_LOG_END
return false;
}
}
//____________________________________________________________________________//
bool
test_and_continue_impl( extended_predicate_value const& v, wrap_stringstream& message,
c_string_literal file_name, std::size_t line_num,
bool add_fail_pass, unit_test_framework::log_level loglevel )
{
return test_and_continue_impl( !!v,
message << (add_fail_pass ? (!v ? " failed. " : " passed. ") : "") << *(v.p_message),
file_name, line_num, false, loglevel );
}
//____________________________________________________________________________//
void
test_and_throw_impl( bool predicate, wrap_stringstream& message,
c_string_literal file_name, std::size_t line_num,
bool add_fail_pass, unit_test_framework::log_level loglevel )
{
if( test_and_continue_impl( predicate, message, file_name, line_num, add_fail_pass, loglevel ) ) {
throw test_tool_failed(); // error already reported by test_and_continue_impl
}
}
//____________________________________________________________________________//
void
test_and_throw_impl( extended_predicate_value const& v, wrap_stringstream& message,
c_string_literal file_name, std::size_t line_num,
bool add_fail_pass, unit_test_framework::log_level loglevel )
{
if( test_and_continue_impl( v, message, file_name, line_num, add_fail_pass, loglevel ) ) {
throw test_tool_failed(); // error already reported by test_and_continue_impl
}
}
//____________________________________________________________________________//
bool
equal_and_continue_impl( c_string_literal left, c_string_literal right, wrap_stringstream& message,
c_string_literal file_name, std::size_t line_num,
unit_test_framework::log_level loglevel )
{
bool predicate = (left && right) ? std::strcmp( left, right ) == 0 : (left == right);
left = left ? left : "null string";
right = right ? right : "null string";
if( !predicate ) {
return test_and_continue_impl( false,
wrap_stringstream().ref() << "test " << message.str() << " failed [" << left << " != " << right << "]",
file_name, line_num, false, loglevel );
}
return test_and_continue_impl( true, message, file_name, line_num, true, loglevel );
}
//____________________________________________________________________________//
bool
is_defined_impl( c_string_literal symbol_name, c_string_literal symbol_value )
{
// return std::strncmp( symbol_name, symbol_value, std::strlen( symbol_name ) ) != 0;
return std::strcmp( symbol_name, symbol_value + 2 ) != 0;
}
//____________________________________________________________________________//
// ************************************************************************** //
// ************** log print helper ************** //
// ************************************************************************** //
void
print_log_value<char>::operator()( std::ostream& ostr, char t )
{
if( std::isprint( t ) )
ostr << '\'' << t << '\'';
else
ostr << std::hex
// showbase is only available for new style streams:
#ifndef BOOST_NO_STD_LOCALE
<< std::showbase
#else
<< "0x"
#endif
<< (int)t;
}
//____________________________________________________________________________//
void
print_log_value<unsigned char>::operator()( std::ostream& ostr, unsigned char t )
{
ostr << std::hex
// showbase is only available for new style streams:
#ifndef BOOST_NO_STD_LOCALE
<< std::showbase
#else
<< "0x"
#endif
<< (int)t;
}
//____________________________________________________________________________//
} // namespace detail
// ************************************************************************** //
// ************** output_test_stream ************** //
// ************************************************************************** //
struct output_test_stream::Impl
{
std::fstream m_pattern_to_match_or_save;
bool m_match_or_save;
std::string m_synced_string;
char get_char()
{
char res;
do {
m_pattern_to_match_or_save.get( res );
} while( res == '\r' &&
!m_pattern_to_match_or_save.fail() &&
!m_pattern_to_match_or_save.eof() );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -