/////////////////////////////////////////////////////////////////////////////// // Name: tests/controls/markup.cpp // Purpose: wxMarkupParser and related classes unit tests // Author: Vadim Zeitlin // Created: 2011-02-17 // Copyright: (c) 2011 Vadim Zeitlin /////////////////////////////////////////////////////////////////////////////// #include "testprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #endif // WX_PRECOMP #include "wx/private/markupparser.h" class MarkupTestCase : public CppUnit::TestCase { public: MarkupTestCase() { } private: CPPUNIT_TEST_SUITE( MarkupTestCase ); CPPUNIT_TEST( RoundTrip ); CPPUNIT_TEST( Quote ); CPPUNIT_TEST( Strip ); CPPUNIT_TEST_SUITE_END(); void RoundTrip(); void Quote(); void Strip(); wxDECLARE_NO_COPY_CLASS(MarkupTestCase); }; // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( MarkupTestCase ); // also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MarkupTestCase, "MarkupTestCase" ); void MarkupTestCase::RoundTrip() { // Define a wxMarkupParserOutput object which produces the same markup // string on output. This is, of course, perfectly useless, but allows us // to test that parsing works as expected. class RoundTripOutput : public wxMarkupParserOutput { public: RoundTripOutput() { } void Reset() { m_text.clear(); } const wxString& GetText() const { return m_text; } virtual void OnText(const wxString& text) { m_text += text; } virtual void OnBoldStart() { m_text += ""; } virtual void OnBoldEnd() { m_text += ""; } virtual void OnItalicStart() { m_text += ""; } virtual void OnItalicEnd() { m_text += ""; } virtual void OnUnderlinedStart() { m_text += ""; } virtual void OnUnderlinedEnd() { m_text += ""; } virtual void OnStrikethroughStart() { m_text += ""; } virtual void OnStrikethroughEnd() { m_text += ""; } virtual void OnBigStart() { m_text += ""; } virtual void OnBigEnd() { m_text += ""; } virtual void OnSmallStart() { m_text += ""; } virtual void OnSmallEnd() { m_text += ""; } virtual void OnTeletypeStart() { m_text += ""; } virtual void OnTeletypeEnd() { m_text += ""; } virtual void OnSpanStart(const wxMarkupSpanAttributes& attrs) { m_text << " 0 ? "larger" : "smaller"); break; case wxMarkupSpanAttributes::Size_Symbolic: { CPPUNIT_ASSERT( attrs.m_fontSize >= -3 ); CPPUNIT_ASSERT( attrs.m_fontSize <= 3 ); static const char *cssSizes[] = { "xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", }; size << cssSizes[attrs.m_fontSize + 3]; } break; case wxMarkupSpanAttributes::Size_PointParts: size.Printf("%u", attrs.m_fontSize); break; } if ( !size.empty() ) m_text << " size='" << size << '\''; // TODO: Handle the rest of attributes. m_text << ">"; } virtual void OnSpanEnd(const wxMarkupSpanAttributes& WXUNUSED(attrs)) { m_text += ""; } private: wxString m_text; }; RoundTripOutput output; wxMarkupParser parser(output); #define CHECK_PARSES_OK(text) \ output.Reset(); \ CPPUNIT_ASSERT( parser.Parse(text) ); \ CPPUNIT_ASSERT_EQUAL( text, output.GetText() ) #define CHECK_PARSES_AS(text, result) \ output.Reset(); \ CPPUNIT_ASSERT( parser.Parse(text) ); \ CPPUNIT_ASSERT_EQUAL( result, output.GetText() ) #define CHECK_DOESNT_PARSE(text) \ CPPUNIT_ASSERT( !parser.Parse(text) ) CHECK_PARSES_OK( "" ); CHECK_PARSES_OK( "foo" ); CHECK_PARSES_OK( "foobar" ); CHECK_PARSES_OK( "123456" ); CHECK_PARSES_OK( "first second last" ); CHECK_PARSES_OK( "first second last" ); CHECK_PARSES_OK( "10pt" ); CHECK_PARSES_OK( "much smaller" ); CHECK_PARSES_OK( "larger" ); CHECK_PARSES_OK ( "Please notice: any " "bugs in this code are NOT allowed." ); CHECK_PARSES_OK( "foo&bar" ); CHECK_PARSES_AS( "foo&bar", "foo&bar" ); CHECK_PARSES_AS( "<O'Reilly>", "" ); CHECK_DOESNT_PARSE( "<" ); CHECK_DOESNT_PARSE( "" ); CHECK_DOESNT_PARSE( "" ); CHECK_DOESNT_PARSE( "" ); CHECK_DOESNT_PARSE( "" ); #undef CHECK_PARSES_OK #undef CHECK_DOESNT_PARSE } void MarkupTestCase::Quote() { CPPUNIT_ASSERT_EQUAL( "", wxMarkupParser::Quote("") ); CPPUNIT_ASSERT_EQUAL( "foo", wxMarkupParser::Quote("foo") ); CPPUNIT_ASSERT_EQUAL( "<foo>", wxMarkupParser::Quote("") ); CPPUNIT_ASSERT_EQUAL( "B&B", wxMarkupParser::Quote("B&B") ); CPPUNIT_ASSERT_EQUAL( """", wxMarkupParser::Quote("\"\"") ); } void MarkupTestCase::Strip() { #define CHECK_STRIP( text, stripped ) \ CPPUNIT_ASSERT_EQUAL( stripped, wxMarkupParser::Strip(text) ) CHECK_STRIP( "", "" ); CHECK_STRIP( "foo", "foo" ); CHECK_STRIP( "<foo>", "" ); CHECK_STRIP( "Big problem", "Big problem" ); CHECK_STRIP( "c" "o" "l" "o" "u" "r", "colour" ); #undef CHECK_STRIP }