corelib/tests/srd-parser-defs.cc

880 lines
24 KiB
C++

#include <lw/lib/SRDDefinitions.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
class SRDInputItemTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SRDInputItemTest );
CPPUNIT_TEST( testWordFromTString );
CPPUNIT_TEST( testWordFromCString );
CPPUNIT_TEST( testEnumFromTString );
CPPUNIT_TEST( testEnumFromCString );
CPPUNIT_TEST( testWordToken );
CPPUNIT_TEST( testStringToken );
CPPUNIT_TEST( testStartToken );
CPPUNIT_TEST( testEndToken );
CPPUNIT_TEST( testIntToken );
CPPUNIT_TEST( testLongToken );
CPPUNIT_TEST( testFloatToken );
CPPUNIT_TEST( testBinaryToken );
CPPUNIT_TEST( testAlt );
CPPUNIT_TEST( testInteger );
CPPUNIT_TEST( testNumeric );
CPPUNIT_TEST( testText );
CPPUNIT_TEST( testOptNoArg );
CPPUNIT_TEST( testOptArg );
CPPUNIT_TEST( testAtLeast0 );
CPPUNIT_TEST( testAtLeast1 );
CPPUNIT_TEST( testAtLeast2 );
CPPUNIT_TEST( testAtMost1 );
CPPUNIT_TEST( testAtMost2 );
CPPUNIT_TEST( testTimes1 );
CPPUNIT_TEST( testTimes2 );
CPPUNIT_TEST( testBetween );
CPPUNIT_TEST( testBetweenSame );
CPPUNIT_TEST( testBetweenInverted );
CPPUNIT_TEST( testRepeatSequence );
CPPUNIT_TEST( testList );
CPPUNIT_TEST_SUITE_END( );
public:
void testWordFromTString( );
void testWordFromCString( );
void testEnumFromTString( );
void testEnumFromCString( );
void testWordToken( );
void testStringToken( );
void testStartToken( );
void testEndToken( );
void testIntToken( );
void testLongToken( );
void testFloatToken( );
void testBinaryToken( );
void testAlt( );
void testInteger( );
void testNumeric( );
void testText( );
void testOptNoArg( );
void testOptArg( );
void testAtLeast0( );
void testAtLeast1( );
void testAtLeast2( );
void testAtMost1( );
void testAtMost2( );
void testTimes1( );
void testTimes2( );
void testBetween( );
void testBetweenSame( );
void testBetweenInverted( );
void testRepeatSequence( );
void testList( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( SRDInputItemTest );
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testWordFromTString( )
{
T_String const word( "some-word" );
auto item( SRD::Word( word ) );
CPPUNIT_ASSERT( E_SRDInputItem::WORD == item.type( ) );
CPPUNIT_ASSERT( word == item.word( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( word == sb );
}
void SRDInputItemTest::testWordFromCString( )
{
char const* const word = "some-word";
auto item( SRD::Word( word ) );
CPPUNIT_ASSERT( E_SRDInputItem::WORD == item.type( ) );
CPPUNIT_ASSERT( item.word( ) == T_String( word ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( word ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testEnumFromTString( )
{
T_String const word( "EnumType" );
auto item( SRD::Enum( word ) );
CPPUNIT_ASSERT( E_SRDInputItem::ENUM == item.type( ) );
CPPUNIT_ASSERT( word == item.word( ) );
T_StringBuilder sb, sb2;
sb << item;
sb2 << '[' << word << "::*]";
CPPUNIT_ASSERT( sb2 == sb );
}
void SRDInputItemTest::testEnumFromCString( )
{
char const* const word = "EnumType";
auto item( SRD::Enum( word ) );
CPPUNIT_ASSERT( E_SRDInputItem::ENUM == item.type( ) );
CPPUNIT_ASSERT( item.word( ) == T_String( word ) );
T_StringBuilder sb, sb2;
sb << item;
sb2 << '[' << word << "::*]";
CPPUNIT_ASSERT( sb == sb2 );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testWordToken( )
{
auto item( SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::WORD == item.token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD" ) );
}
void SRDInputItemTest::testStringToken( )
{
auto item( SRD::String( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::STRING == item.token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "STRING" ) );
}
void SRDInputItemTest::testStartToken( )
{
auto item( SRD::LStart( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::START == item.token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "(" ) );
}
void SRDInputItemTest::testEndToken( )
{
auto item( SRD::LEnd( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::END == item.token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( ")" ) );
}
void SRDInputItemTest::testIntToken( )
{
auto item( SRD::Int32( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::INT == item.token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "INT" ) );
}
void SRDInputItemTest::testLongToken( )
{
auto item( SRD::Int64( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::LONG == item.token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "LONG" ) );
}
void SRDInputItemTest::testFloatToken( )
{
auto item( SRD::Float( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::FLOAT == item.token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "FLOAT" ) );
}
void SRDInputItemTest::testBinaryToken( )
{
auto item( SRD::Binary( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::BINARY == item.token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "BINARY" ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testAlt( )
{
auto item( SRD::Alt( ) << SRD::Int32( ) << SRD::Int64( ) );
CPPUNIT_ASSERT( E_SRDInputItem::ALTERNATIVE == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.items( ).size( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 0 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::INT == item.items( )[ 0 ].token( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 1 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::LONG == item.items( )[ 1 ].token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "[ INT | LONG ]" ) );
}
void SRDInputItemTest::testInteger( )
{
auto item( SRD::Integer( ) );
CPPUNIT_ASSERT( E_SRDInputItem::ALTERNATIVE == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.items( ).size( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 0 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::INT == item.items( )[ 0 ].token( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 1 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::LONG == item.items( )[ 1 ].token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "[ INT | LONG ]" ) );
}
void SRDInputItemTest::testNumeric( )
{
auto item( SRD::Numeric( ) );
CPPUNIT_ASSERT( E_SRDInputItem::ALTERNATIVE == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , item.items( ).size( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 0 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::INT == item.items( )[ 0 ].token( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 1 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::LONG == item.items( )[ 1 ].token( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 2 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::FLOAT == item.items( )[ 2 ].token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "[ INT | LONG | FLOAT ]" ) );
}
void SRDInputItemTest::testText( )
{
auto item( SRD::Text( ) );
CPPUNIT_ASSERT( E_SRDInputItem::ALTERNATIVE == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.items( ).size( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 0 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::STRING == item.items( )[ 0 ].token( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 1 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::WORD == item.items( )[ 1 ].token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "[ STRING | WORD ]" ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testOptNoArg( )
{
auto item( SRD::Opt( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "[ ]?" ) );
}
void SRDInputItemTest::testOptArg( )
{
auto item( SRD::Opt( SRD::Word( ) ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.max( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == item.items( )[ 0 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::WORD == item.items( )[ 0 ].token( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD?" ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testAtLeast0( )
{
auto item( SRD::AtLeast( 0 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( UINT32_MAX ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD*" ) );
}
void SRDInputItemTest::testAtLeast1( )
{
auto item( SRD::AtLeast( 1 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( UINT32_MAX ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD+" ) );
}
void SRDInputItemTest::testAtLeast2( )
{
auto item( SRD::AtLeast( 2 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( UINT32_MAX ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD{2+}" ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testAtMost1( )
{
auto item( SRD::AtMost( 1 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD?" ) );
}
void SRDInputItemTest::testAtMost2( )
{
auto item( SRD::AtMost( 2 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD{0,2}" ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testTimes1( )
{
auto item( SRD::Times( 1 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD" ) );
}
void SRDInputItemTest::testTimes2( )
{
auto item( SRD::Times( 2 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD{2}" ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testBetween( )
{
auto item( SRD::Between( 2 , 4 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 4 ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD{2,4}" ) );
}
void SRDInputItemTest::testBetweenSame( )
{
auto item( SRD::Between( 2 , 2 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD{2}" ) );
}
void SRDInputItemTest::testBetweenInverted( )
{
auto item( SRD::Between( 4 , 2 ) << SRD::Word( ) );
CPPUNIT_ASSERT( E_SRDInputItem::REPETITION == item.type( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , item.items( ).size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , item.min( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 4 ) , item.max( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "WORD{2,4}" ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testRepeatSequence( )
{
auto item( SRD::AtLeast( 0 ) << SRD::Word( ) << SRD::Word( ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "[ WORD WORD ]*" ) );
}
/*----------------------------------------------------------------------------*/
void SRDInputItemTest::testList( )
{
auto item( SRD::Sub( ) << ( SRD::List( ) << SRD::Word( ) ) );
T_StringBuilder sb;
sb << item;
CPPUNIT_ASSERT( sb == T_String( "( WORD )" ) );
}
/*============================================================================*/
class SRDInputRuleTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SRDInputRuleTest );
CPPUNIT_TEST( testEmpty );
CPPUNIT_TEST( testAddItem );
CPPUNIT_TEST( testAddList );
CPPUNIT_TEST( testSetExecutor );
CPPUNIT_TEST( testSetContext );
CPPUNIT_TEST( testSetOnEnter );
CPPUNIT_TEST( testSetOnExit );
CPPUNIT_TEST( testStringNoContext );
CPPUNIT_TEST( testStringContext );
CPPUNIT_TEST_SUITE_END( );
public:
void testEmpty( );
void testAddItem( );
void testAddList( );
void testSetExecutor( );
void testSetContext( );
void testSetOnEnter( );
void testSetOnExit( );
void testStringNoContext( );
void testStringContext( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( SRDInputRuleTest );
/*----------------------------------------------------------------------------*/
void SRDInputRuleTest::testEmpty( )
{
auto rule( SRD::Rule( ) );
CPPUNIT_ASSERT( !rule.handler( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , rule.rule( ).size( ) );
CPPUNIT_ASSERT( !rule.context( ) );
CPPUNIT_ASSERT( !rule.onEnter( ) );
CPPUNIT_ASSERT( !rule.onExit( ) );
}
void SRDInputRuleTest::testAddItem( )
{
auto rule( SRD::Rule( ) << SRD::Word( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , rule.rule( ).size( ) );
}
void SRDInputRuleTest::testAddList( )
{
auto rule( SRD::Rule( ) << ( SRD::List( ) << SRD::Word( ) ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , rule.rule( ).size( ) );
}
void SRDInputRuleTest::testSetExecutor( )
{
bool check = false;
F_SRDHandler exec = [&check]( auto const& ) {
check = true;
return false;
};
auto rule( SRD::Rule( ) << exec );
T_SRDParserData * x = nullptr;
( rule.handler( ) )( *x );
CPPUNIT_ASSERT( check );
}
void SRDInputRuleTest::testSetContext( )
{
auto rule( SRD::Rule( ) << SRD::EnterContext( "ctx" ) );
CPPUNIT_ASSERT( rule.context( ) == T_String( "ctx" ) );
}
void SRDInputRuleTest::testSetOnEnter( )
{
bool check = false;
F_SRDHandler exec = [&check]( auto const& ) {
check = true;
return false;
};
auto rule( SRD::Rule( ) << SRD::OnEnter( exec ) );
T_SRDParserData * x = nullptr;
( rule.onEnter( ) )( *x );
CPPUNIT_ASSERT( check );
}
void SRDInputRuleTest::testSetOnExit( )
{
bool check = false;
F_SRDHandler exec = [&check]( auto const& ) {
check = true;
return false;
};
auto rule( SRD::Rule( ) << SRD::OnExit( exec ) );
T_SRDParserData * x = nullptr;
( rule.onExit( ) )( *x );
CPPUNIT_ASSERT( check );
}
void SRDInputRuleTest::testStringNoContext( )
{
auto rule( SRD::Rule( ) << SRD::Word( ) << SRD::Integer( ) );
T_StringBuilder sb;
sb << rule;
CPPUNIT_ASSERT( sb == T_String( "( WORD [ INT | LONG ] )" ) );
}
void SRDInputRuleTest::testStringContext( )
{
auto rule( SRD::Rule( ) << SRD::Word( ) << SRD::Integer( )
<< SRD::EnterContext( "ctx" ) );
T_StringBuilder sb;
sb << rule;
CPPUNIT_ASSERT( sb == T_String( "( WORD [ INT | LONG ] ...ctx... )" ) );
}
/*----------------------------------------------------------------------------*/
class SRDEnumTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SRDEnumTest );
CPPUNIT_TEST( testEmpty );
CPPUNIT_TEST( testItems );
CPPUNIT_TEST_SUITE_END( );
public:
void testEmpty( );
void testItems( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( SRDEnumTest );
/*----------------------------------------------------------------------------*/
void SRDEnumTest::testEmpty( )
{
auto e( SRD::CreateEnum( "test" ) );
CPPUNIT_ASSERT( e.name( ) == T_String( "test" ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , e.size( ) );
}
void SRDEnumTest::testItems( )
{
auto e( SRD::CreateEnum( "test" ) << "a" << "b" );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , e.size( ) );
CPPUNIT_ASSERT( e.contains( "a" ) );
CPPUNIT_ASSERT( e.contains( "b" ) );
CPPUNIT_ASSERT( !e.contains( "c" ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , e[ "a" ] );
CPPUNIT_ASSERT( T_String( "a" ) == e[ 0 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , e[ "b" ] );
CPPUNIT_ASSERT( T_String( "b" ) == e[ 1 ] );
}
/*============================================================================*/
class SRDContextTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SRDContextTest );
CPPUNIT_TEST( testEmpty );
CPPUNIT_TEST( testParent );
CPPUNIT_TEST( testRules );
CPPUNIT_TEST( testDump );
CPPUNIT_TEST_SUITE_END( );
public:
void testEmpty( );
void testParent( );
void testRules( );
void testDump( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( SRDContextTest );
/*----------------------------------------------------------------------------*/
void SRDContextTest::testEmpty( )
{
auto e( SRD::Context( "test" ) );
CPPUNIT_ASSERT( e.name( ) == T_String( "test" ) );
CPPUNIT_ASSERT( !e.parent( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , e.rules( ).size( ) );
}
void SRDContextTest::testParent( )
{
auto e( SRD::Context( "test" , "parent" ) );
CPPUNIT_ASSERT( e.name( ) == T_String( "test" ) );
CPPUNIT_ASSERT( e.parent( ) == T_String( "parent" ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , e.rules( ).size( ) );
}
void SRDContextTest::testRules( )
{
using namespace SRD;
auto e( Context( "test" )
<< ( Rule( ) << Word( ) )
<< ( Rule( ) << Int32( ) ) );
auto const& rules( e.rules( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , rules.size( ) );
auto const& r0( rules[ 0 ].rule( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , r0.size( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == r0[ 0 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::WORD == r0[ 0 ].token( ) );
auto const& r1( rules[ 1 ].rule( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , r1.size( ) );
CPPUNIT_ASSERT( E_SRDInputItem::TOKEN == r1[ 0 ].type( ) );
CPPUNIT_ASSERT( E_SRDTokenType::INT == r1[ 0 ].token( ) );
}
void SRDContextTest::testDump( )
{
using namespace SRD;
auto e( Context( "test" )
<< ( Rule( ) << Word( ) )
<< ( Rule( ) << Int32( ) ) );
T_StringBuilder sb;
e.dump( sb , T_String::Pooled( "\n" ) );
CPPUNIT_ASSERT( sb == T_String( "( WORD )\n( INT )" ) );
}
/*============================================================================*/
class SRDParserDefsTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SRDParserDefsTest );
CPPUNIT_TEST( testDefault );
CPPUNIT_TEST( testGetContextParentOK );
CPPUNIT_TEST( testGetContextBadParent );
CPPUNIT_TEST( testGetContextNew );
CPPUNIT_TEST( testGetContextNewWithParent );
CPPUNIT_TEST( testGetEnum );
CPPUNIT_TEST( testSetDefaultContextSame );
CPPUNIT_TEST( testSetDefaultContextOther );
CPPUNIT_TEST( testSetDefaultContextMissing );
CPPUNIT_TEST_SUITE_END( );
public:
void testDefault( );
void testGetContextParentOK( );
void testGetContextBadParent( );
void testGetContextNew( );
void testGetContextNewWithParent( );
void testGetEnum( );
void testSetDefaultContextSame( );
void testSetDefaultContextOther( );
void testSetDefaultContextMissing( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( SRDParserDefsTest );
/*----------------------------------------------------------------------------*/
void SRDParserDefsTest::testDefault( )
{
T_String dc( T_String::Pooled( "test" ) );
T_SRDParserDefs e( dc );
CPPUNIT_ASSERT( e.defaultContext( ) == dc );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , e.contexts( ) );
CPPUNIT_ASSERT( e[ 0 ].name( ) == dc );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , e.contextId( dc ) );
CPPUNIT_ASSERT( &e[ 0 ] == &e.context( dc ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , e.enums( ) );
}
/*----------------------------------------------------------------------------*/
void SRDParserDefsTest::testGetContextParentOK( )
{
T_String dc( T_String::Pooled( "test" ) );
T_SRDParserDefs e( dc );
auto const& c( e.context( dc , T_String( ) ) );
CPPUNIT_ASSERT( c.name( ) == dc );
CPPUNIT_ASSERT( &e[ 0 ] == &c );
}
void SRDParserDefsTest::testGetContextBadParent( )
{
T_String dc( T_String::Pooled( "test" ) );
T_SRDParserDefs e( dc );
CPPUNIT_ASSERT_THROW( e.context( dc , T_String( "nope" ) ) ,
std::invalid_argument );
}
void SRDParserDefsTest::testGetContextNew( )
{
T_String dc( T_String::Pooled( "test" ) );
T_String cn2( T_String::Pooled( "other" ) );
T_SRDParserDefs e( dc );
auto const& c( e.context( cn2 ) );
CPPUNIT_ASSERT( c.name( ) == cn2 );
CPPUNIT_ASSERT( !c.parent( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , e.contexts( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , e.contextId( cn2 ) );
CPPUNIT_ASSERT( &e[ 1 ] == &c );
CPPUNIT_ASSERT( &e.context( cn2 ) == &c );
}
void SRDParserDefsTest::testGetContextNewWithParent( )
{
T_String dc( T_String::Pooled( "test" ) );
T_String cn2( T_String::Pooled( "other" ) );
T_SRDParserDefs e( dc );
auto const& c( e.context( cn2 , dc ) );
CPPUNIT_ASSERT( c.name( ) == cn2 );
CPPUNIT_ASSERT( c.parent( ) == dc );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , e.contexts( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , e.contextId( cn2 ) );
CPPUNIT_ASSERT( &e[ 1 ] == &c );
CPPUNIT_ASSERT( &e.context( cn2 ) == &c );
}
/*----------------------------------------------------------------------------*/
void SRDParserDefsTest::testGetEnum( )
{
T_String dc( T_String::Pooled( "test" ) );
T_SRDParserDefs d( dc );
auto const& e( d.enumeration( dc ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , d.enums( ) );
CPPUNIT_ASSERT( e.name( ) == dc );
CPPUNIT_ASSERT( d.hasEnum( dc ) );
CPPUNIT_ASSERT( &d.enumeration( dc ) == &e );
}
/*----------------------------------------------------------------------------*/
void SRDParserDefsTest::testSetDefaultContextSame( )
{
T_String dc( T_String::Pooled( "test" ) );
T_SRDParserDefs e( dc );
e.defaultContext( dc );
CPPUNIT_ASSERT( e.defaultContext( ) == dc );
}
void SRDParserDefsTest::testSetDefaultContextOther( )
{
T_String dc( T_String::Pooled( "test" ) );
T_String dc2( T_String::Pooled( "test2" ) );
T_SRDParserDefs e( dc );
e.context( dc2 );
e.defaultContext( dc2 );
CPPUNIT_ASSERT( e.defaultContext( ) == dc2 );
}
void SRDParserDefsTest::testSetDefaultContextMissing( )
{
T_String dc( T_String::Pooled( "test" ) );
T_String dc2( T_String::Pooled( "test2" ) );
T_SRDParserDefs e( dc );
CPPUNIT_ASSERT_THROW( e.defaultContext( dc2 ) ,
std::invalid_argument );
}