forked from Alepha/Alepha
IOStream filter stage.
This permits creation of custom filtration stages in the IOStream streambuf stack.
This commit is contained in:
128
IOStreams/Filter.test/0.cc
Normal file
128
IOStreams/Filter.test/0.cc
Normal file
@ -0,0 +1,128 @@
|
||||
static_assert( __cplusplus > 2020'99 );
|
||||
|
||||
#include "../Filter.h"
|
||||
|
||||
#include <Alepha/Testing/test.h>
|
||||
#include <Alepha/Testing/TableTest.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
static auto init= Alepha::Utility::enroll <=[]
|
||||
{
|
||||
using namespace std::literals::string_literals;
|
||||
using namespace Alepha::Testing::literals;
|
||||
|
||||
using namespace Alepha::Testing::exports;
|
||||
|
||||
[]( const std::string &s )
|
||||
{
|
||||
std::stringstream iss{ s };
|
||||
|
||||
//iss >> Filter{ std::make_unique< CapitalizationFilter >() };
|
||||
|
||||
std::string rv;
|
||||
iss << "bob";
|
||||
int x;
|
||||
iss >> x;
|
||||
};
|
||||
|
||||
"Simple Filter"_test <=TableTest
|
||||
<
|
||||
[]( const std::string &s )
|
||||
-> std::string
|
||||
{
|
||||
using namespace Alepha::IOStreams::exports::Filter_m;
|
||||
struct CapitalizationFilter : StreamFilter
|
||||
{
|
||||
public:
|
||||
Emitter
|
||||
nextChar( const char ch ) override
|
||||
{
|
||||
struct EmitterImpl
|
||||
: public Emitter::Impl
|
||||
{
|
||||
char ch;
|
||||
bool read= false;
|
||||
|
||||
explicit EmitterImpl( const char ch ) : ch( ch ) {}
|
||||
|
||||
bool empty() const final { return read; }
|
||||
char
|
||||
next() final
|
||||
{
|
||||
read= true;
|
||||
std::cerr << "Emitted `" << ch << "`" << std::endl;
|
||||
return ch;
|
||||
}
|
||||
};
|
||||
|
||||
std::cerr << "Emitter built for `" << ch << "`" << std::endl;
|
||||
return Emitter{ std::make_unique< EmitterImpl >( ::toupper( ch ) ) };
|
||||
}
|
||||
};
|
||||
|
||||
std::ostringstream oss;
|
||||
|
||||
oss << Filter{ std::make_unique< CapitalizationFilter >() };
|
||||
|
||||
oss << s;
|
||||
|
||||
oss << Alepha::IOStreams::PopStack{};
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
>
|
||||
::Cases
|
||||
{
|
||||
{ "Hello", { "hello" }, "HELLO" },
|
||||
};
|
||||
|
||||
auto streamWriter= []( const std::string &s )
|
||||
{
|
||||
using namespace Alepha::IOStreams::exports::Filter_m;
|
||||
struct CapitalizationFilter : StreamFilter
|
||||
{
|
||||
public:
|
||||
Emitter
|
||||
nextChar( const char ch ) override
|
||||
{
|
||||
struct EmitterImpl
|
||||
: public Emitter::Impl
|
||||
{
|
||||
char ch;
|
||||
bool read= false;
|
||||
|
||||
explicit EmitterImpl( const char ch ) : ch( ch ) {}
|
||||
|
||||
bool empty() const final { return read; }
|
||||
char
|
||||
next() final
|
||||
{
|
||||
read= true;
|
||||
std::cerr << "Emitted `" << ch << "`" << std::endl;
|
||||
return ch;
|
||||
}
|
||||
};
|
||||
std::cerr << "Emitter built for `" << ch << "`" << std::endl;
|
||||
return Emitter{ std::make_unique< EmitterImpl >( ::toupper( ch ) ) };
|
||||
}
|
||||
};
|
||||
|
||||
std::stringstream iss{ s };
|
||||
|
||||
iss >> Filter{ std::make_unique< CapitalizationFilter >() };
|
||||
|
||||
std::string rv;
|
||||
iss >> rv;
|
||||
|
||||
iss >> Alepha::IOStreams::PopStack{};
|
||||
|
||||
return rv;
|
||||
};
|
||||
|
||||
"Simple Input Filter"_test <=TableTest< streamWriter >::Cases
|
||||
{
|
||||
{ "Hello", { "hello" }, "HELLO" },
|
||||
};
|
||||
};
|
1
IOStreams/Filter.test/CMakeLists.txt
Normal file
1
IOStreams/Filter.test/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
unit_test( 0 )
|
Reference in New Issue
Block a user