corelib/tests/srd-preproc-cmd-common.hh

109 lines
2.9 KiB
C++
Raw Normal View History

#ifndef TESTS_SRDPREPROCCMDCOMMON_H_
#define TESTS_SRDPREPROCCMDCOMMON_H_
#include <lw/lib/SRDPreproc.hh>
#include <lw/lib/SRDText.hh>
#include <lw/lib/SRDIO.hh>
#include <lw/lib/MemoryStreams.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
// M_PRINTERR_( index ) - Print an error message
#define M_PRINTERR_( IDX ) \
do { \
auto const& _e( errors[ IDX ] ); \
char err[ _e.error( ).size( ) + 1 ]; \
err[ sizeof( err ) - 1 ] = 0; \
memcpy( err , _e.error( ).data( ) , \
sizeof( err ) - 1 ); \
printf( "ERR %s l. %u c. %lu\n" , err , \
_e.location( ).line( ) , \
_e.location( ).character( ) ); \
} while ( 0 )
// M_CKERR_( index , string , line , character ) - Check an error
#define M_CKERR_( IDX , STR , L , C ) \
do { \
auto const& _e( errors[ IDX ] ); \
CPPUNIT_ASSERT( T_String( STR ) == _e.error( ) ); \
CPPUNIT_ASSERT_EQUAL( uint32_t( L ) , \
_e.location( ).line( ) ); \
CPPUNIT_ASSERT_EQUAL( size_t( C ) , \
_e.location( ).character( ) ); \
} while ( 0 )
namespace {
void process( char const* input , T_SRDPreprocessor& preproc , T_SRDErrors& errors )
{
T_SRDLexer lexer( T_String( "test" ) , errors , preproc );
preproc.start( errors );
char const* ptr = input;
while ( *ptr != 0 ) {
lexer.processCharacter( *ptr ++ );
}
lexer.processEnd( );
}
T_SRDList process( char const* input , T_SRDErrors& errors )
{
T_SRDMemoryTarget mt( false );
mt.clearFlushToken( true );
mt.start( errors );
T_SRDPreprocessorConfig emptyConfig;
emptyConfig.addBuiltinCommands( );
T_SRDPreprocessor pp( emptyConfig , mt );
process( input , pp , errors );
mt.end( errors );
return mt.list( );
}
bool checkMatch( T_SRDList const& expected , T_SRDList const& actual )
{
const size_t nExpected( expected.size( ) );
const size_t nActual( actual.size( ) );
bool ok( nExpected == nActual );
const size_t nCheck( std::min( nExpected , nActual ) );
for ( size_t i = 0 ; i < nCheck ; i ++ ) {
T_SRDToken const& tExpected( expected[ i ] );
T_SRDToken const& tActual( actual[ i ] );
if ( tExpected.type( ) != tActual.type( ) ) {
ok = false;
} else if ( tExpected.stringValue( ) != tActual.stringValue( ) ) {
ok = false;
}
}
return ok;
}
bool check( char const* expected , T_SRDList const& actual )
{
T_SRDMemoryTarget mt( false );
T_SRDErrors errors;
T_SRDLexer lexer( T_String( "expected" ) , errors , mt );
mt.start( errors );
char const* ptr = expected;
while ( *ptr != 0 ) {
lexer.processCharacter( *ptr ++ );
}
lexer.processEnd( );
const bool ok( checkMatch( mt.list( ) , actual ) );
if ( ok ) {
return true;
}
T_Buffer< char > output;
T_MemoryOutputStream stream{ output };
lw::SRDWriteAsText( stream , actual );
stream.write( "" , 1 );
std::cerr << "\nExpected:\n\t" << expected << "\nActual:\n" << output.data( ) << '\n';
return false;
}
}
#endif // TESTS_SRDPREPROCCMDCOMMON_H_