Use explicit streaming ops instead of Boost.LexicalCast

This commit is contained in:
Alexander Grund 2020-07-12 13:38:54 +02:00
parent 898a5a4524
commit 04dff97fde
No known key found for this signature in database
GPG key ID: AA48A0760367A42B
5 changed files with 24 additions and 17 deletions

View file

@ -35,13 +35,15 @@ namespace
//] //]
//[ quick_constraint_solution //[ quick_constraint_solution
#include <boost/lexical_cast.hpp> #include <sstream>
namespace // in the same namespace as 'my_class' namespace // in the same namespace as 'my_class'
{ {
bool operator==( const my_class& actual, const std::string& expected ) // the first part of the trick is to compare to a string bool operator==( const my_class& actual, const std::string& expected ) // the first part of the trick is to compare to a string
{ {
return boost::lexical_cast< std::string >( actual ) == expected; std::ostringstream s;
s << actual;
return s.str() == expected;
} }
} // mock } // mock

View file

@ -8,14 +8,16 @@
#include <turtle/detail/type_name.hpp> #include <turtle/detail/type_name.hpp>
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#include <boost/lexical_cast.hpp> #include <sstream>
namespace namespace
{ {
template< typename T > template< typename T >
std::string to_string( const T& t) std::string to_string( const T& t)
{ {
return boost::lexical_cast< std::string >( mock::detail::make_type_name(t) ); std::ostringstream s;
s << mock::detail::make_type_name(t); // Typename can be streamed
return s.str();
} }
} }
@ -111,7 +113,7 @@ BOOST_AUTO_TEST_CASE( name_of_type_in_unnamed_inner_namespace_is_extracted )
BOOST_AUTO_TEST_CASE( name_of_local_type_is_extracted ) BOOST_AUTO_TEST_CASE( name_of_local_type_is_extracted )
{ {
struct my_local_type {}; struct my_local_type {};
BOOST_CHECK_EQUAL( "my_local_type", boost::lexical_cast< std::string >( mock::detail::make_type_name<my_local_type>() ) ); BOOST_CHECK_EQUAL( "my_local_type", to_string( my_local_type() ) );
} }
namespace namespace

View file

@ -11,9 +11,9 @@
#define MOCK_ERROR_POLICY mock_error #define MOCK_ERROR_POLICY mock_error
#include <turtle/detail/singleton.hpp> #include <turtle/detail/singleton.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <stdexcept> #include <stdexcept>
#include <sstream>
struct mock_error_data_t : mock::detail::singleton< mock_error_data_t > struct mock_error_data_t : mock::detail::singleton< mock_error_data_t >
{ {
@ -68,18 +68,17 @@ struct mock_error
{} {}
template< typename Context > template< typename Context >
static void call( const Context& /*context*/, static void call( const Context& /*context*/, const char* /*file*/, int /*line*/ )
const char* /*file*/, int /*line*/ )
{ {
mock_error_data.call(); mock_error_data.call();
} }
template< typename Context > template< typename Context >
static void fail( const std::string& message, const Context& context, static void fail( const std::string& message, const Context& context, const char* file = "", int line = 0 )
const char* file = "", int line = 0 )
{ {
mock_error_data.fail( message, std::ostringstream s;
boost::lexical_cast< std::string >( context ), file, line ); s << context; // Context can be streamed
mock_error_data.fail( message, s.str(), file, line );
} }
}; };

View file

@ -10,7 +10,6 @@
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#include <boost/assign.hpp> #include <boost/assign.hpp>
#include <boost/weak_ptr.hpp> #include <boost/weak_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
#pragma warning( push, 0 ) #pragma warning( push, 0 )
@ -55,13 +54,19 @@ BOOST_AUTO_TEST_CASE( pointer_yields_its_value_when_serialized )
{ {
{ {
int i = 0; int i = 0;
std::ostringstream s;
s << &i;
const std::string pointerValue = s.str();
BOOST_CHECK_NE( "?", to_string( &i ) ); BOOST_CHECK_NE( "?", to_string( &i ) );
BOOST_CHECK_EQUAL( boost::lexical_cast< std::string >( &i ), to_string( &i ) ); BOOST_CHECK_EQUAL( pointerValue, to_string( &i ) );
} }
{ {
const int i = 0; const int i = 0;
std::ostringstream s;
s << &i;
const std::string pointerValue = s.str();
BOOST_CHECK_NE( "?", to_string( &i ) ); BOOST_CHECK_NE( "?", to_string( &i ) );
BOOST_CHECK_EQUAL( boost::lexical_cast< std::string >( &i ), to_string( &i ) ); BOOST_CHECK_EQUAL( pointerValue, to_string( &i ) );
} }
} }
@ -630,7 +635,7 @@ BOOST_AUTO_TEST_CASE( mock_detail_template_template_streamable_yields_its_value_
BOOST_AUTO_TEST_CASE( unsigned_char_is_serialized_as_int ) BOOST_AUTO_TEST_CASE( unsigned_char_is_serialized_as_int )
{ {
BOOST_CHECK_EQUAL( boost::lexical_cast< std::string >( static_cast< int >( 'a' ) ), to_string< unsigned char >( 'a' ) ); BOOST_CHECK_EQUAL( std::to_string( static_cast< int >( 'a' ) ), to_string< unsigned char >( 'a' ) );
} }
namespace namespace

View file

@ -9,7 +9,6 @@
#include "mock_error.hpp" #include "mock_error.hpp"
#include <turtle/mock.hpp> #include <turtle/mock.hpp>
#include <boost/test/auto_unit_test.hpp> #include <boost/test/auto_unit_test.hpp>
#include <boost/lexical_cast.hpp>
#include <functional> #include <functional>
namespace namespace