📄 test_tools.ipp
字号:
is_defined_impl( const_string symbol_name, const_string symbol_value )
{
symbol_value.trim_left( 2 );
return symbol_name != symbol_value;
}
//____________________________________________________________________________//
// ************************************************************************** //
// ************** 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;
}
//____________________________________________________________________________//
void
print_log_value<char const*>::operator()( std::ostream& ostr, char const* t )
{
ostr << ( t ? t : "null string" );
}
//____________________________________________________________________________//
void
print_log_value<wchar_t const*>::operator()( std::ostream& ostr, wchar_t const* t )
{
ostr << ( t ? t : L"null string" );
}
//____________________________________________________________________________//
} // namespace tt_detail
// ************************************************************************** //
// ************** output_test_stream ************** //
// ************************************************************************** //
struct output_test_stream::Impl
{
std::fstream m_pattern;
bool m_match_or_save;
std::string m_synced_string;
char get_char()
{
char res;
do {
m_pattern.get( res );
} while( res == '\r' && !m_pattern.fail() && !m_pattern.eof() );
return res;
}
void check_and_fill( predicate_result& res )
{
if( !res.p_predicate_value )
res.message() << "Output content: \"" << m_synced_string << '\"';
}
};
//____________________________________________________________________________//
output_test_stream::output_test_stream( const_string pattern_file_name, bool match_or_save )
: m_pimpl( new Impl )
{
if( !pattern_file_name.is_empty() ) {
m_pimpl->m_pattern.open( pattern_file_name.begin(), match_or_save ? std::ios::in : std::ios::out );
BOOST_WARN_MESSAGE( m_pimpl->m_pattern.is_open(),
"Couldn't open pattern file " << pattern_file_name
<< " for " << ( m_pimpl->m_match_or_save ? "reading" : "writing") );
}
m_pimpl->m_match_or_save = match_or_save;
}
//____________________________________________________________________________//
output_test_stream::~output_test_stream()
{
delete m_pimpl;
}
//____________________________________________________________________________//
predicate_result
output_test_stream::is_empty( bool flush_stream )
{
sync();
result_type res( m_pimpl->m_synced_string.empty() );
m_pimpl->check_and_fill( res );
if( flush_stream )
flush();
return res;
}
//____________________________________________________________________________//
predicate_result
output_test_stream::check_length( std::size_t length_, bool flush_stream )
{
sync();
result_type res( m_pimpl->m_synced_string.length() == length_ );
m_pimpl->check_and_fill( res );
if( flush_stream )
flush();
return res;
}
//____________________________________________________________________________//
predicate_result
output_test_stream::is_equal( const_string arg, bool flush_stream )
{
sync();
result_type res( const_string( m_pimpl->m_synced_string ) == arg );
m_pimpl->check_and_fill( res );
if( flush_stream )
flush();
return res;
}
//____________________________________________________________________________//
predicate_result
output_test_stream::match_pattern( bool flush_stream )
{
sync();
result_type result( true );
if( !m_pimpl->m_pattern.is_open() ) {
result = false;
result.message() << "I/O failure";
}
else {
if( m_pimpl->m_match_or_save ) {
for ( std::string::size_type i = 0; i < m_pimpl->m_synced_string.length(); ++i ) {
char c = m_pimpl->get_char();
result = !m_pimpl->m_pattern.fail() &&
!m_pimpl->m_pattern.eof() &&
(m_pimpl->m_synced_string[i] == c);
if( !result ) {
std::string::size_type suffix_size = (std::min)( m_pimpl->m_synced_string.length() - i,
static_cast<std::string::size_type>(5) );
// try to log area around the mismatch
result.message() << "Mismatch at position " << i << '\n'
<< "..." << m_pimpl->m_synced_string.substr( i, suffix_size ) << "..." << '\n'
<< "..." << c;
std::string::size_type counter = suffix_size;
while( --counter ) {
char c = m_pimpl->get_char();
if( m_pimpl->m_pattern.fail() || m_pimpl->m_pattern.eof() )
break;
result.message() << c;
}
result.message() << "...";
// skip rest of the bytes. May help for further matching
m_pimpl->m_pattern.ignore( m_pimpl->m_synced_string.length() - i - suffix_size );
break;
}
}
}
else {
m_pimpl->m_pattern.write( m_pimpl->m_synced_string.c_str(),
static_cast<std::streamsize>( m_pimpl->m_synced_string.length() ) );
m_pimpl->m_pattern.flush();
}
}
if( flush_stream )
flush();
return result;
}
//____________________________________________________________________________//
void
output_test_stream::flush()
{
m_pimpl->m_synced_string.erase();
#ifndef BOOST_NO_STRINGSTREAM
str( std::string() );
#else
seekp( 0, std::ios::beg );
#endif
}
//____________________________________________________________________________//
std::size_t
output_test_stream::length()
{
sync();
return m_pimpl->m_synced_string.length();
}
//____________________________________________________________________________//
void
output_test_stream::sync()
{
#ifdef BOOST_NO_STRINGSTREAM
m_pimpl->m_synced_string.assign( str(), pcount() );
freeze( false );
#elif BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x530) )
m_pimpl->m_synced_string.assign( str().c_str(), tellp() );
#else
m_pimpl->m_synced_string = str();
#endif
}
//____________________________________________________________________________//
} // namespace test_tools
} // namespace boost
//____________________________________________________________________________//
#include <boost/test/detail/enable_warnings.hpp>
// ***************************************************************************
// Revision History :
//
// $Log: test_tools.ipp,v $
// Revision 1.9 2005/06/22 22:03:05 dgregor
// More explicit scoping needed for GCC 2.95.3
//
// Revision 1.8 2005/04/30 17:56:31 rogeeff
// switch to stdarg.h to workarround como issues
//
// Revision 1.7 2005/03/23 21:02:23 rogeeff
// Sunpro CC 5.3 fixes
//
// Revision 1.6 2005/02/21 10:14:04 rogeeff
// CHECK_SMALL tool implemented
//
// Revision 1.5 2005/02/20 08:27:07 rogeeff
// This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
//
// Revision 1.4 2005/02/02 12:08:14 rogeeff
// namespace log added for log manipulators
//
// Revision 1.3 2005/02/01 06:40:07 rogeeff
// copyright update
// old log entries removed
// minor stilistic changes
// depricated tools removed
//
// Revision 1.2 2005/01/30 03:18:27 rogeeff
// test tools implementation completely reworked. All tools inplemented through single vararg function
//
// Revision 1.1 2005/01/22 19:22:12 rogeeff
// implementation moved into headers section to eliminate dependency of included/minimal component on src directory
//
// Revision 1.43 2005/01/19 06:40:05 vawjr
// deleted redundant \r in many \r\r\n sequences of the source. VC8.0 doesn't like them
//
// Revision 1.42 2005/01/18 08:30:08 rogeeff
// unit_test_log rework:
// eliminated need for ::instance()
// eliminated need for << end and ...END macro
// straitend interface between log and formatters
// change compiler like formatter name
// minimized unit_test_log interface and reworked to use explicit calls
//
// ***************************************************************************
#endif // BOOST_TEST_TEST_TOOLS_IPP_012205GER
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -