1
0
forked from Alepha/Alepha

Stream builder notation for strings.

This should be less boilerplate than repeated `lexical_cast`
or `stringify` calls when building strings.
This commit is contained in:
2023-10-25 04:03:39 -04:00
parent bacaa9fcee
commit c2bc8dae2e
5 changed files with 98 additions and 13 deletions

View File

@ -10,6 +10,7 @@ static_assert( __cplusplus > 2020'00 );
#include <Alepha/auto_comparable.h>
#include <Alepha/IOStreams/delimiters.h>
#include <Alepha/IOStreams/Stream.h>
namespace
{
@ -27,7 +28,7 @@ namespace
auto
stringify( const Agg &agg, const std::string delim )
stringify_specific( const Agg &agg, const std::string delim )
{
std::ostringstream oss;
Alepha::IOStreams::setGlobalFieldDelimiter( "YOU SHOULD NOT SEE THIS" );
@ -58,6 +59,7 @@ static auto init= Alepha::Utility::enroll <=[]
{
using namespace Alepha::Testing::exports;
using namespace Alepha::Testing::literals::test_literals;
using namespace Alepha::IOStreams::exports::stream;
"Simple OStream (default delimiter)"_test <=TableTest< stringify_default >
::Cases
@ -65,7 +67,24 @@ static auto init= Alepha::Utility::enroll <=[]
{ "smoke test", { { 1, 2, 3 } }, { "1\t2\t3" } },
};
"Simple OStream (specific delimiter)"_test <=TableTest< stringify >
"Simple OStream (specific delimiter)"_test <=TableTest< stringify_specific >
::Cases
{
{ "smoke test", { { 1, 2, 3 }, "\t" }, { "1\t2\t3" } },
{ "smoke test", { { 1, 2, 3 }, "," }, { "1,2,3" } },
{ "smoke test", { { 1, 2, 3 }, ";;" }, { "1;;2;;3" } },
{ "smoke test", { { 1, 2, 3 }, ", " }, { "1, 2, 3" } },
};
"Simple OStream (stream builder)"_test <=TableTest
<
[]( const Agg agg, const std::string delim )
{
using Alepha::IOStreams::Stream;
using Alepha::IOStreams::setFieldDelimiter;
return Stream{} << setFieldDelimiter( delim ) << agg << FinishString;
}
>
::Cases
{
{ "smoke test", { { 1, 2, 3 }, "\t" }, { "1\t2\t3" } },