Added formatting specializations for common std types

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@106 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2010-01-31 22:50:44 +00:00
parent b9f352e2b0
commit cf9bb5d89b
2 changed files with 127 additions and 5 deletions

View file

@ -13,6 +13,7 @@
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <boost/utility/enable_if.hpp> #include <boost/utility/enable_if.hpp>
#include <boost/type_traits/integral_constant.hpp> #include <boost/type_traits/integral_constant.hpp>
#include <boost/detail/container_fwd.hpp>
#include <sstream> #include <sstream>
#include <ostream> #include <ostream>
@ -67,14 +68,68 @@ namespace detail
{ {
return detail::serialize( t ); return detail::serialize( t );
} }
inline std::string format( const std::string& s )
{
return '"' + s + '"';
}
inline std::string format( const char* s ) inline std::string format( const char* s )
{ {
return '"' + std::string( s ) + '"'; return '"' + std::string( s ) + '"';
} }
#ifndef MOCK_NO_STL_FORMAT
inline std::string format( const std::string& s )
{
return '"' + s + '"';
}
template< typename T1, typename T2 >
inline std::string format( const std::pair< T1, T2 >& p )
{
return '(' + format( p.first ) + ',' + format( p.second ) + ')';
}
template< typename T >
std::string format( const T& begin, const T& end )
{
std::stringstream s;
s << '(';
for( T it = begin; it != end; ++it )
s << (it == begin ? "" : ",") << format( *it );
s << ')';
return s.str();
}
template< typename T, typename A >
std::string format( const std::deque< T, A >& d )
{
return format( d.begin(), d.end() );
}
template< typename T, typename A >
std::string format( const std::list< T, A >& l )
{
return format( l.begin(), l.end() );
}
template< typename T, typename A >
std::string format( const std::vector< T, A >& v )
{
return format( v.begin(), v.end() );
}
template< typename K, typename T, typename C, typename A >
std::string format( const std::map< K, T, C, A >& m )
{
return format( m.begin(), m.end() );
}
template< typename K, typename T, typename C, typename A >
std::string format( const std::multimap< K, T, C, A >& m )
{
return format( m.begin(), m.end() );
}
template< typename T, typename C, typename A >
std::string format( const std::set< T, C, A >& s )
{
return format( s.begin(), s.end() );
}
template< typename T, typename C, typename A >
std::string format( const std::multiset< T, C, A >& s )
{
return format( s.begin(), s.end() );
}
#endif // MOCK_NO_STL_FORMAT
} }
#endif // #ifndef MOCK_FORMAT_HPP_INCLUDED #endif // #ifndef MOCK_FORMAT_HPP_INCLUDED

View file

@ -8,6 +8,12 @@
#include <turtle/format.hpp> #include <turtle/format.hpp>
#include <ostream> #include <ostream>
#include <deque>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <boost/assign.hpp>
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework #define BOOST_LIB_NAME boost_unit_test_framework
@ -78,8 +84,69 @@ BOOST_AUTO_TEST_CASE( booleans_are_serialized_as_booleans )
BOOST_CHECK_EQUAL( "false", mock::format( false ) ); BOOST_CHECK_EQUAL( "false", mock::format( false ) );
} }
BOOST_AUTO_TEST_CASE( strings_are_surrounded_with_double_quotes ) BOOST_AUTO_TEST_CASE( strings_are_serialized_with_double_quotes )
{ {
BOOST_CHECK_EQUAL( "\"string\"", mock::format( "string" ) ); BOOST_CHECK_EQUAL( "\"string\"", mock::format( "string" ) );
BOOST_CHECK_EQUAL( "\"string\"", mock::format( std::string( "string" ) ) ); BOOST_CHECK_EQUAL( "\"string\"", mock::format( std::string( "string" ) ) );
} }
BOOST_AUTO_TEST_CASE( std_pairs_are_serialized )
{
BOOST_CHECK_EQUAL( "(3,42)", mock::format( std::make_pair( 3, 42.f ) ) );
}
BOOST_AUTO_TEST_CASE( std_deques_are_serialized )
{
std::deque< int > d;
d.push_back( 12 );
d.push_back( 42 );
BOOST_CHECK_EQUAL( "(12,42)", mock::format( d ) );
}
BOOST_AUTO_TEST_CASE( std_lists_are_serialized )
{
std::list< int > l;
l.push_back( 12 );
l.push_back( 42 );
BOOST_CHECK_EQUAL( "(12,42)", mock::format( l ) );
}
BOOST_AUTO_TEST_CASE( std_vectors_are_serialized )
{
std::vector< int > v;
v.push_back( 12 );
v.push_back( 42 );
BOOST_CHECK_EQUAL( "(12,42)", mock::format( v ) );
}
BOOST_AUTO_TEST_CASE( std_maps_are_serialized )
{
std::map< int, std::string > m;
m[ 12 ] = "12";
m[ 42 ] = "42";
BOOST_CHECK_EQUAL( "((12,\"12\"),(42,\"42\"))", mock::format( m ) );
}
BOOST_AUTO_TEST_CASE( std_multimaps_are_serialized )
{
std::multimap< int, std::string > m;
m.insert( std::make_pair( 12, "12" ));
m.insert( std::make_pair( 42, "42" ));
BOOST_CHECK_EQUAL( "((12,\"12\"),(42,\"42\"))", mock::format( m ) );
}
BOOST_AUTO_TEST_CASE( std_sets_are_serialized )
{
std::set< int > s;
s.insert( 12 );
s.insert( 42 );
BOOST_CHECK_EQUAL( "(12,42)", mock::format( s ) );
}
BOOST_AUTO_TEST_CASE( std_multisets_are_serialized )
{
std::multiset< int > s;
s.insert( 12 );
s.insert( 42 );
BOOST_CHECK_EQUAL( "(12,42)", mock::format( s ) );
}