📄 test_tools.cpp
字号:
return res;
}
void check_and_fill( extended_predicate_value& res )
{
if( !res.p_predicate_value.get() )
*(res.p_message) << "Output content: \"" << m_synced_string << '\"';
}
};
//____________________________________________________________________________//
output_test_stream::output_test_stream( std::string const& pattern_file_name, bool match_or_save )
: m_pimpl( new Impl )
{
if( !pattern_file_name.empty() )
m_pimpl->m_pattern_to_match_or_save.open( pattern_file_name.c_str(), match_or_save ? std::ios::in : std::ios::out );
m_pimpl->m_match_or_save = match_or_save;
}
//____________________________________________________________________________//
output_test_stream::output_test_stream( c_string_literal pattern_file_name, bool match_or_save )
: m_pimpl( new Impl )
{
if( pattern_file_name && pattern_file_name[0] != '\0' )
m_pimpl->m_pattern_to_match_or_save.open( pattern_file_name, match_or_save ? std::ios::in : std::ios::out );
m_pimpl->m_match_or_save = match_or_save;
}
//____________________________________________________________________________//
output_test_stream::~output_test_stream()
{
}
//____________________________________________________________________________//
extended_predicate_value
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;
}
//____________________________________________________________________________//
extended_predicate_value
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;
}
//____________________________________________________________________________//
extended_predicate_value
output_test_stream::is_equal( c_string_literal arg, bool flush_stream )
{
sync();
result_type res( m_pimpl->m_synced_string == arg );
m_pimpl->check_and_fill( res );
if( flush_stream )
flush();
return res;
}
//____________________________________________________________________________//
extended_predicate_value
output_test_stream::is_equal( std::string const& arg, bool flush_stream )
{
sync();
result_type res( m_pimpl->m_synced_string == arg );
m_pimpl->check_and_fill( res );
if( flush_stream )
flush();
return res;
}
//____________________________________________________________________________//
extended_predicate_value
output_test_stream::is_equal( c_string_literal arg, std::size_t n, bool flush_stream )
{
sync();
result_type res( m_pimpl->m_synced_string == std::string( arg, n ) );
m_pimpl->check_and_fill( res );
if( flush_stream )
flush();
return res;
}
//____________________________________________________________________________//
extended_predicate_value
output_test_stream::match_pattern( bool flush_stream )
{
sync();
result_type result( true );
if( !m_pimpl->m_pattern_to_match_or_save.is_open() ) {
result = false;
*(result.p_message) << "Couldn't open pattern file for "
<< ( m_pimpl->m_match_or_save ? "reading" : "writing");
}
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_to_match_or_save.fail() &&
!m_pimpl->m_pattern_to_match_or_save.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.p_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_to_match_or_save.fail() ||
m_pimpl->m_pattern_to_match_or_save.eof() )
break;
*(result.p_message) << c;
}
*(result.p_message) << "...";
// skip rest of the bytes. May help for further matching
m_pimpl->m_pattern_to_match_or_save.ignore( m_pimpl->m_synced_string.length() - i - suffix_size);
break;
}
}
}
else {
m_pimpl->m_pattern_to_match_or_save.write( m_pimpl->m_synced_string.c_str(),
static_cast<std::streamsize>( m_pimpl->m_synced_string.length() ) );
m_pimpl->m_pattern_to_match_or_save.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 );
#else
m_pimpl->m_synced_string = str();
#endif
}
//____________________________________________________________________________//
} // namespace test_toolbox
} // namespace boost
// ***************************************************************************
// Revision History :
//
// $Log: test_tools.cpp,v $
// Revision 1.25 2003/12/01 00:42:37 rogeeff
// prerelease cleaning
//
// ***************************************************************************
// EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -