332 lines
7.9 KiB
C++
332 lines
7.9 KiB
C++
|
#include <lw/lib/SRDText.hh>
|
||
|
#include <lw/lib/MemoryStreams.hh>
|
||
|
#include <cmath>
|
||
|
#include <cppunit/extensions/HelperMacros.h>
|
||
|
using namespace lw;
|
||
|
|
||
|
|
||
|
class SRDTextWriterTest : public CppUnit::TestFixture
|
||
|
{
|
||
|
CPPUNIT_TEST_SUITE( SRDTextWriterTest );
|
||
|
CPPUNIT_TEST( testEmpty );
|
||
|
|
||
|
CPPUNIT_TEST( testList );
|
||
|
CPPUNIT_TEST( testListRecursive );
|
||
|
CPPUNIT_TEST( testListUnbalanced );
|
||
|
|
||
|
CPPUNIT_TEST( testWord );
|
||
|
CPPUNIT_TEST( testString );
|
||
|
CPPUNIT_TEST( testStringEscapes );
|
||
|
CPPUNIT_TEST( testStringUTF8Short );
|
||
|
CPPUNIT_TEST( testStringUTF8Long );
|
||
|
|
||
|
CPPUNIT_TEST( testTextWord );
|
||
|
CPPUNIT_TEST( testTextString );
|
||
|
|
||
|
CPPUNIT_TEST( testCommentLine );
|
||
|
CPPUNIT_TEST( testCommentMultiLines );
|
||
|
|
||
|
CPPUNIT_TEST( testVar );
|
||
|
|
||
|
CPPUNIT_TEST( testInt );
|
||
|
CPPUNIT_TEST( testInt32 );
|
||
|
CPPUNIT_TEST( testInt64 );
|
||
|
CPPUNIT_TEST( testFloat );
|
||
|
|
||
|
CPPUNIT_TEST( testBinaryEmpty );
|
||
|
CPPUNIT_TEST( testBinaryShort );
|
||
|
CPPUNIT_TEST( testBinaryLong );
|
||
|
|
||
|
CPPUNIT_TEST( testIndentation );
|
||
|
CPPUNIT_TEST_SUITE_END( );
|
||
|
|
||
|
T_Buffer< char > buffer;
|
||
|
OP_MemoryOutputStream stream;
|
||
|
OP_SRDTextWriter writer;
|
||
|
T_String result;
|
||
|
|
||
|
T_String getResult( );
|
||
|
|
||
|
public:
|
||
|
void setUp( ) override;
|
||
|
void tearDown( ) override;
|
||
|
|
||
|
void testEmpty( );
|
||
|
|
||
|
void testList( );
|
||
|
void testListRecursive( );
|
||
|
void testListUnbalanced( );
|
||
|
|
||
|
void testWord( );
|
||
|
void testString( );
|
||
|
void testStringEscapes( );
|
||
|
void testStringUTF8Short( );
|
||
|
void testStringUTF8Long( );
|
||
|
|
||
|
void testTextWord( );
|
||
|
void testTextString( );
|
||
|
|
||
|
void testCommentLine( );
|
||
|
void testCommentMultiLines( );
|
||
|
|
||
|
void testVar( );
|
||
|
|
||
|
void testInt( );
|
||
|
void testInt32( );
|
||
|
void testInt64( );
|
||
|
void testFloat( );
|
||
|
|
||
|
void testBinaryEmpty( );
|
||
|
void testBinaryShort( );
|
||
|
void testBinaryLong( );
|
||
|
|
||
|
void testIndentation( );
|
||
|
};
|
||
|
CPPUNIT_TEST_SUITE_REGISTRATION( SRDTextWriterTest );
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::setUp( )
|
||
|
{
|
||
|
stream = NewOwned< T_MemoryOutputStream >( buffer );
|
||
|
writer = NewOwned< T_SRDTextWriter >( *stream );
|
||
|
writer->start( );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::tearDown( )
|
||
|
{
|
||
|
writer.clear( );
|
||
|
stream.clear( );
|
||
|
buffer.resize( 0 );
|
||
|
}
|
||
|
|
||
|
T_String SRDTextWriterTest::getResult( )
|
||
|
{
|
||
|
if ( writer ) {
|
||
|
writer->end( );
|
||
|
writer.clear( );
|
||
|
stream.clear( );
|
||
|
if ( buffer.size( ) != 0 ) {
|
||
|
return T_String( &buffer[ 0 ] , buffer.size( ) );
|
||
|
}
|
||
|
return T_String( );
|
||
|
}
|
||
|
CPPUNIT_FAIL( "error in test" );
|
||
|
return T_String( );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testEmpty( )
|
||
|
{
|
||
|
CPPUNIT_ASSERT( getResult( ) == "" );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testList( )
|
||
|
{
|
||
|
writer->startList( );
|
||
|
writer->endList( );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "()\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testListRecursive( )
|
||
|
{
|
||
|
writer->startList( );
|
||
|
writer->startList( );
|
||
|
writer->endList( );
|
||
|
writer->endList( );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "(())\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testListUnbalanced( )
|
||
|
{
|
||
|
writer->startList( );
|
||
|
writer->startList( );
|
||
|
writer->endList( );
|
||
|
try {
|
||
|
getResult( );
|
||
|
CPPUNIT_FAIL( "X_SRDWriterError not thrown" );
|
||
|
} catch ( X_SRDWriterError const& ) {
|
||
|
// OK
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testWord( )
|
||
|
{
|
||
|
writer->putWord( T_String( "this-is-a-word" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "this-is-a-word\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testString( )
|
||
|
{
|
||
|
writer->putString( T_String( "just a string" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "\"just a string\"\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testStringEscapes( )
|
||
|
{
|
||
|
writer->putString( T_String( "\n\r\t\x08\x0c\"\\" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "\"\\n\\r\\t\\b\\f\\\"\\\\\"\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testStringUTF8Short( )
|
||
|
{
|
||
|
writer->putString( T_String( "\x01\xc3\x81\xc7\x81\xe2\x82\xac" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "\"\\u0001\\u00C1\\u01C1\\u20AC\"\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testStringUTF8Long( )
|
||
|
{
|
||
|
writer->putString( T_String( "\xf0\x91\x87\x81" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "\"\\U000111C1\"\r\n" );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testTextWord( )
|
||
|
{
|
||
|
writer->putText( T_String( "-whatever-that-is" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "-whatever-that-is\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testTextString( )
|
||
|
{
|
||
|
writer->putText( T_String( "-whatever-that-is\x01" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "\"-whatever-that-is\\u0001\"\r\n" );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testCommentLine( )
|
||
|
{
|
||
|
writer->putComment( T_String( " a comment" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "# a comment\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testCommentMultiLines( )
|
||
|
{
|
||
|
writer->putComment( T_String( " a comment\n with a few\r\n lines" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "# a comment\r\n# with a few\r\n# lines\r\n" );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testVar( )
|
||
|
{
|
||
|
writer->putVariable( T_String( "thingmabob" ) );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "$thingmabob\r\n" );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testInt( )
|
||
|
{
|
||
|
writer->putInteger( 123 );
|
||
|
writer->putInteger( 12345678000 );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "123 12345678000\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testInt32( )
|
||
|
{
|
||
|
writer->putInt32( 123 );
|
||
|
writer->putInt32( -123 );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "123 -123\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testInt64( )
|
||
|
{
|
||
|
writer->putInt64( 12345678000 );
|
||
|
writer->putInt64( -12345678000 );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "12345678000 -12345678000\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testFloat( )
|
||
|
{
|
||
|
writer->putFloat( 1.5 );
|
||
|
writer->putFloat( M_PI );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "1.5 3.141592653589793115997963\r\n" );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testBinaryEmpty( )
|
||
|
{
|
||
|
T_Buffer< uint8_t > buf( 0 );
|
||
|
writer->putBinary( buf );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "[]\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testBinaryShort( )
|
||
|
{
|
||
|
T_Buffer< uint8_t > buf( 8 );
|
||
|
for ( auto i = 0u ; i < 8 ; i ++ ) {
|
||
|
buf[ i ] = i << 3;
|
||
|
}
|
||
|
writer->putBinary( buf );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result == "[ 00 08 10 18 20 28 30 38 ]\r\n" );
|
||
|
}
|
||
|
|
||
|
void SRDTextWriterTest::testBinaryLong( )
|
||
|
{
|
||
|
T_Buffer< uint8_t > buf( 18 );
|
||
|
for ( auto i = 0u ; i < 18 ; i ++ ) {
|
||
|
buf[ i ] = i;
|
||
|
}
|
||
|
writer->putBinary( buf );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result ==
|
||
|
"[\r\n"
|
||
|
" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n"
|
||
|
" 10 11\r\n"
|
||
|
"]\r\n" );
|
||
|
}
|
||
|
|
||
|
/*----------------------------------------------------------------------------*/
|
||
|
|
||
|
void SRDTextWriterTest::testIndentation( )
|
||
|
{
|
||
|
writer->startList( )
|
||
|
.putText( T_String( "this" ) )
|
||
|
.putText( T_String( "is" ) )
|
||
|
.startList( )
|
||
|
.putText( T_String( "just" ) )
|
||
|
.putText( T_String( "a" ) )
|
||
|
.endList( )
|
||
|
.putComment( T_String( " very, very, very...\n BAD!" ) )
|
||
|
.putText( T_String( "test" ) )
|
||
|
.putComment( T_String( " and then some." ) )
|
||
|
.putComment( T_String( " Twice, even." ) )
|
||
|
.endList( );
|
||
|
const T_String result( getResult( ) );
|
||
|
CPPUNIT_ASSERT( result ==
|
||
|
"(this is\r\n"
|
||
|
" (just a) # very, very, very...\r\n"
|
||
|
" # BAD!\r\n"
|
||
|
" test # and then some.\r\n"
|
||
|
" # Twice, even.\r\n"
|
||
|
" )\r\n" );
|
||
|
}
|
||
|
|