corelib/tests/console-edit.cc

469 lines
12 KiB
C++
Raw Normal View History

#include <lw/lib/Console.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
class ConsoleEditTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( ConsoleEditTest );
CPPUNIT_TEST( testEmpty );
CPPUNIT_TEST( testSetContents );
CPPUNIT_TEST( testReset );
CPPUNIT_TEST( testCursorStart );
CPPUNIT_TEST( testCursorStartEmpty );
CPPUNIT_TEST( testCursorEnd );
CPPUNIT_TEST( testCursorEndEmpty );
CPPUNIT_TEST( testCursorLeft );
CPPUNIT_TEST( testCursorLeftStart );
CPPUNIT_TEST( testCursorRight );
CPPUNIT_TEST( testCursorRightEnd );
CPPUNIT_TEST( testAppendCharacter );
CPPUNIT_TEST( testInsertCharacter );
CPPUNIT_TEST( testAppendBuffer );
CPPUNIT_TEST( testInsertBuffer );
CPPUNIT_TEST( testPrevWordStart );
CPPUNIT_TEST( testPrevWordInWord );
CPPUNIT_TEST( testPrevWordSpaces );
CPPUNIT_TEST( testNextWordEnd );
CPPUNIT_TEST( testNextWordInWord );
CPPUNIT_TEST( testNextWordSpaces );
CPPUNIT_TEST( testRmCharAtEnd );
CPPUNIT_TEST( testRmCharLast );
CPPUNIT_TEST( testRmCharMiddle );
CPPUNIT_TEST( testRmWordAtEnd );
CPPUNIT_TEST( testRmWordMiddle );
CPPUNIT_TEST( testRmWordAtStart );
CPPUNIT_TEST( testRmRestAtStart );
CPPUNIT_TEST( testRmRestMiddle );
CPPUNIT_TEST( testRmRestAtEnd );
CPPUNIT_TEST_SUITE_END( );
public:
void testEmpty( );
void testSetContents( );
void testReset( );
void testCursorStart( );
void testCursorStartEmpty( );
void testCursorEnd( );
void testCursorEndEmpty( );
void testCursorLeft( );
void testCursorLeftStart( );
void testCursorRight( );
void testCursorRightEnd( );
void testAppendCharacter( );
void testInsertCharacter( );
void testAppendBuffer( );
void testInsertBuffer( );
void testPrevWordStart( );
void testPrevWordInWord( );
void testPrevWordSpaces( );
void testNextWordEnd( );
void testNextWordInWord( );
void testNextWordSpaces( );
void testRmCharAtEnd( );
void testRmCharLast( );
void testRmCharMiddle( );
void testRmWordAtEnd( );
void testRmWordMiddle( );
void testRmWordAtStart( );
void testRmRestAtStart( );
void testRmRestMiddle( );
void testRmRestAtEnd( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( ConsoleEditTest );
/*----------------------------------------------------------------------------*/
void ConsoleEditTest::testEmpty( )
{
T_ConsoleLineState s;
CPPUNIT_ASSERT( s.getContents( ) == "" );
CPPUNIT_ASSERT_EQUAL( 0u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.historyIndex( ) );
}
void ConsoleEditTest::testSetContents( )
{
const T_String str( "t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.setContents( str , 1 );
CPPUNIT_ASSERT( s.getContents( ) == str );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 4u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 1u , s.historyIndex( ) );
}
void ConsoleEditTest::testReset( )
{
const T_String str( "t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.setContents( str , 1 );
s.reset( );
CPPUNIT_ASSERT( s.getContents( ) == "" );
CPPUNIT_ASSERT_EQUAL( 0u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.historyIndex( ) );
}
/*----------------------------------------------------------------------------*/
void ConsoleEditTest::testCursorStart( )
{
const T_String str( "t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
}
void ConsoleEditTest::testCursorStartEmpty( )
{
T_ConsoleLineState s;
s.toStart( );
CPPUNIT_ASSERT_EQUAL( 0u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
}
void ConsoleEditTest::testCursorEnd( )
{
const T_String str( "t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
s.toEnd( );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 4u , s.pos( ) );
}
void ConsoleEditTest::testCursorEndEmpty( )
{
T_ConsoleLineState s;
s.toEnd( );
CPPUNIT_ASSERT_EQUAL( 0u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
}
void ConsoleEditTest::testCursorLeft( )
{
const T_String str( "t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.left( );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 3u , s.pos( ) );
}
void ConsoleEditTest::testCursorLeftStart( )
{
T_ConsoleLineState s;
s.left( );
CPPUNIT_ASSERT_EQUAL( 0u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
}
void ConsoleEditTest::testCursorRight( )
{
const T_String str( "t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
s.right( );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 1u , s.pos( ) );
}
void ConsoleEditTest::testCursorRightEnd( )
{
T_ConsoleLineState s;
s.right( );
CPPUNIT_ASSERT_EQUAL( 0u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
}
/*----------------------------------------------------------------------------*/
void ConsoleEditTest::testAppendCharacter( )
{
const T_String str( "t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.insert( 't' );
s.insert( 0x20ac );
s.insert( 's' );
s.insert( 't' );
CPPUNIT_ASSERT( s.getContents( ) == str );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 4u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.historyIndex( ) );
}
void ConsoleEditTest::testInsertCharacter( )
{
const T_String str( "t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.insert( 0x20ac );
s.insert( 's' );
s.insert( 't' );
s.toStart( );
s.insert( 't' );
CPPUNIT_ASSERT( s.getContents( ) == str );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 1u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.historyIndex( ) );
}
void ConsoleEditTest::testAppendBuffer( )
{
const T_String str( "t\xe2\x82\xacst" );
T_Buffer< uint32_t > b( 4 );
b[ 0 ] = b[ 3 ] = 't';
b[ 1 ] = 0x20ac;
b[ 2 ] = 's';
T_ConsoleLineState s;
s.insert( b , 4 );
CPPUNIT_ASSERT( s.getContents( ) == str );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 4u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.historyIndex( ) );
}
void ConsoleEditTest::testInsertBuffer( )
{
const T_String str( "t\xe2\x82\xacst" );
T_Buffer< uint32_t > b( 3 );
b[ 0 ] = 't';
b[ 1 ] = 0x20ac;
b[ 2 ] = 's';
T_ConsoleLineState s;
s.insert( 't' );
s.toStart( );
s.insert( b , 3 );
CPPUNIT_ASSERT( s.getContents( ) == str );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 3u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.historyIndex( ) );
}
/*----------------------------------------------------------------------------*/
void ConsoleEditTest::testPrevWordStart( )
{
const T_String str( "t\xe2\x82\xacst " );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
s.prevWord( );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
}
void ConsoleEditTest::testPrevWordInWord( )
{
const T_String str( " t\xe2\x82\xacst" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.prevWord( );
CPPUNIT_ASSERT_EQUAL( 1u , s.pos( ) );
}
void ConsoleEditTest::testPrevWordSpaces( )
{
const T_String str( " t\xe2\x82\xacst " );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.prevWord( );
CPPUNIT_ASSERT_EQUAL( 1u , s.pos( ) );
}
void ConsoleEditTest::testNextWordEnd( )
{
const T_String str( "t\xe2\x82\xacst " );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.nextWord( );
CPPUNIT_ASSERT_EQUAL( s.size( ) , s.pos( ) );
}
void ConsoleEditTest::testNextWordInWord( )
{
const T_String str( "t\xe2\x82\xacst " );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
s.nextWord( );
CPPUNIT_ASSERT_EQUAL( 4u , s.pos( ) );
}
void ConsoleEditTest::testNextWordSpaces( )
{
const T_String str( " t\xe2\x82\xacst " );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
s.nextWord( );
CPPUNIT_ASSERT_EQUAL( 6u , s.pos( ) );
}
/*----------------------------------------------------------------------------*/
void ConsoleEditTest::testRmCharAtEnd( )
{
const T_String str( "test" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.removeCharacter( );
CPPUNIT_ASSERT( s.getContents( ) == "test" );
CPPUNIT_ASSERT_EQUAL( 4u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
}
void ConsoleEditTest::testRmCharLast( )
{
const T_String str( "test" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.left( );
s.removeCharacter( );
CPPUNIT_ASSERT( s.getContents( ) == "tes" );
CPPUNIT_ASSERT_EQUAL( 3u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 3u , s.size( ) );
}
void ConsoleEditTest::testRmCharMiddle( )
{
const T_String str( "test" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
s.removeCharacter( );
CPPUNIT_ASSERT( s.getContents( ) == "est" );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 3u , s.size( ) );
}
/*----------------------------------------------------------------------------*/
void ConsoleEditTest::testRmWordAtEnd( )
{
const T_String str( "test blah" );
T_ConsoleLineState s;
s.setContents( str , 0 );
T_Buffer< uint32_t > cpb;
uint32_t cpl( 572 );
s.removeWord( cpb , cpl );
CPPUNIT_ASSERT( s.getContents( ) == "test " );
CPPUNIT_ASSERT_EQUAL( 5u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 5u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 4u , cpl );
CPPUNIT_ASSERT_EQUAL( uint32_t( 'b' ) , cpb[ 0 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 'l' ) , cpb[ 1 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 'a' ) , cpb[ 2 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 'h' ) , cpb[ 3 ] );
}
void ConsoleEditTest::testRmWordMiddle( )
{
const T_String str( "test blah" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.left( );
s.left( );
s.left( );
s.left( );
T_Buffer< uint32_t > cpb;
uint32_t cpl( 572 );
s.removeWord( cpb , cpl );
CPPUNIT_ASSERT( s.getContents( ) == "blah" );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 5u , cpl );
CPPUNIT_ASSERT_EQUAL( uint32_t( 't' ) , cpb[ 0 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 'e' ) , cpb[ 1 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 's' ) , cpb[ 2 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 't' ) , cpb[ 3 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( ' ' ) , cpb[ 4 ] );
}
void ConsoleEditTest::testRmWordAtStart( )
{
const T_String str( "test blah" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
T_Buffer< uint32_t > cpb;
uint32_t cpl( 572 );
s.removeWord( cpb , cpl );
CPPUNIT_ASSERT( s.getContents( ) == "test blah" );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 9u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 572u , cpl );
}
/*----------------------------------------------------------------------------*/
void ConsoleEditTest::testRmRestAtStart( )
{
const T_String str( "test" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.toStart( );
T_Buffer< uint32_t > cpb;
uint32_t cpl( 572 );
s.removeRestOfLine( cpb , cpl );
CPPUNIT_ASSERT( s.getContents( ) == "" );
CPPUNIT_ASSERT_EQUAL( 0u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 0u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 4u , cpl );
CPPUNIT_ASSERT_EQUAL( uint32_t( 't' ) , cpb[ 0 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 'e' ) , cpb[ 1 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 's' ) , cpb[ 2 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 't' ) , cpb[ 3 ] );
}
void ConsoleEditTest::testRmRestMiddle( )
{
const T_String str( "test" );
T_ConsoleLineState s;
s.setContents( str , 0 );
s.left( );
s.left( );
T_Buffer< uint32_t > cpb;
uint32_t cpl( 572 );
s.removeRestOfLine( cpb , cpl );
CPPUNIT_ASSERT( s.getContents( ) == "te" );
CPPUNIT_ASSERT_EQUAL( 2u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 2u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 2u , cpl );
CPPUNIT_ASSERT_EQUAL( uint32_t( 's' ) , cpb[ 0 ] );
CPPUNIT_ASSERT_EQUAL( uint32_t( 't' ) , cpb[ 1 ] );
}
void ConsoleEditTest::testRmRestAtEnd( )
{
const T_String str( "test" );
T_ConsoleLineState s;
s.setContents( str , 0 );
T_Buffer< uint32_t > cpb;
uint32_t cpl( 572 );
s.removeRestOfLine( cpb , cpl );
CPPUNIT_ASSERT( s.getContents( ) == "test" );
CPPUNIT_ASSERT_EQUAL( 4u , s.pos( ) );
CPPUNIT_ASSERT_EQUAL( 4u , s.size( ) );
CPPUNIT_ASSERT_EQUAL( 572u , cpl );
}