markuptest.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/markup.cpp
  3. // Purpose: wxMarkupParser and related classes unit tests
  4. // Author: Vadim Zeitlin
  5. // Created: 2011-02-17
  6. // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #ifdef __BORLANDC__
  10. #pragma hdrstop
  11. #endif
  12. #ifndef WX_PRECOMP
  13. #endif // WX_PRECOMP
  14. #include "wx/private/markupparser.h"
  15. class MarkupTestCase : public CppUnit::TestCase
  16. {
  17. public:
  18. MarkupTestCase() { }
  19. private:
  20. CPPUNIT_TEST_SUITE( MarkupTestCase );
  21. CPPUNIT_TEST( RoundTrip );
  22. CPPUNIT_TEST( Quote );
  23. CPPUNIT_TEST( Strip );
  24. CPPUNIT_TEST_SUITE_END();
  25. void RoundTrip();
  26. void Quote();
  27. void Strip();
  28. wxDECLARE_NO_COPY_CLASS(MarkupTestCase);
  29. };
  30. // register in the unnamed registry so that these tests are run by default
  31. CPPUNIT_TEST_SUITE_REGISTRATION( MarkupTestCase );
  32. // also include in its own registry so that these tests can be run alone
  33. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MarkupTestCase, "MarkupTestCase" );
  34. void MarkupTestCase::RoundTrip()
  35. {
  36. // Define a wxMarkupParserOutput object which produces the same markup
  37. // string on output. This is, of course, perfectly useless, but allows us
  38. // to test that parsing works as expected.
  39. class RoundTripOutput : public wxMarkupParserOutput
  40. {
  41. public:
  42. RoundTripOutput() { }
  43. void Reset() { m_text.clear(); }
  44. const wxString& GetText() const { return m_text; }
  45. virtual void OnText(const wxString& text) { m_text += text; }
  46. virtual void OnBoldStart() { m_text += "<b>"; }
  47. virtual void OnBoldEnd() { m_text += "</b>"; }
  48. virtual void OnItalicStart() { m_text += "<i>"; }
  49. virtual void OnItalicEnd() { m_text += "</i>"; }
  50. virtual void OnUnderlinedStart() { m_text += "<u>"; }
  51. virtual void OnUnderlinedEnd() { m_text += "</u>"; }
  52. virtual void OnStrikethroughStart() { m_text += "<s>"; }
  53. virtual void OnStrikethroughEnd() { m_text += "</s>"; }
  54. virtual void OnBigStart() { m_text += "<big>"; }
  55. virtual void OnBigEnd() { m_text += "</big>"; }
  56. virtual void OnSmallStart() { m_text += "<small>"; }
  57. virtual void OnSmallEnd() { m_text += "</small>"; }
  58. virtual void OnTeletypeStart() { m_text += "<tt>"; }
  59. virtual void OnTeletypeEnd() { m_text += "</tt>"; }
  60. virtual void OnSpanStart(const wxMarkupSpanAttributes& attrs)
  61. {
  62. m_text << "<span";
  63. if ( !attrs.m_fgCol.empty() )
  64. m_text << " foreground='" << attrs.m_fgCol << "'";
  65. if ( !attrs.m_bgCol.empty() )
  66. m_text << " background='" << attrs.m_bgCol << "'";
  67. if ( !attrs.m_fontFace.empty() )
  68. m_text << " face='" << attrs.m_fontFace << "'";
  69. wxString size;
  70. switch ( attrs.m_sizeKind )
  71. {
  72. case wxMarkupSpanAttributes::Size_Unspecified:
  73. break;
  74. case wxMarkupSpanAttributes::Size_Relative:
  75. size << (attrs.m_fontSize > 0 ? "larger" : "smaller");
  76. break;
  77. case wxMarkupSpanAttributes::Size_Symbolic:
  78. {
  79. CPPUNIT_ASSERT( attrs.m_fontSize >= -3 &&
  80. attrs.m_fontSize <= 3 );
  81. static const char *cssSizes[] =
  82. {
  83. "xx-small", "x-small", "small",
  84. "medium",
  85. "large", "x-large", "xx-large",
  86. };
  87. size << cssSizes[attrs.m_fontSize + 3];
  88. }
  89. break;
  90. case wxMarkupSpanAttributes::Size_PointParts:
  91. size.Printf("%u", attrs.m_fontSize);
  92. break;
  93. }
  94. if ( !size.empty() )
  95. m_text << " size='" << size << '\'';
  96. // TODO: Handle the rest of attributes.
  97. m_text << ">";
  98. }
  99. virtual void OnSpanEnd(const wxMarkupSpanAttributes& WXUNUSED(attrs))
  100. {
  101. m_text += "</span>";
  102. }
  103. private:
  104. wxString m_text;
  105. };
  106. RoundTripOutput output;
  107. wxMarkupParser parser(output);
  108. #define CHECK_PARSES_OK(text) \
  109. output.Reset(); \
  110. CPPUNIT_ASSERT( parser.Parse(text) ); \
  111. CPPUNIT_ASSERT_EQUAL( text, output.GetText() )
  112. #define CHECK_PARSES_AS(text, result) \
  113. output.Reset(); \
  114. CPPUNIT_ASSERT( parser.Parse(text) ); \
  115. CPPUNIT_ASSERT_EQUAL( result, output.GetText() )
  116. #define CHECK_DOESNT_PARSE(text) \
  117. CPPUNIT_ASSERT( !parser.Parse(text) )
  118. CHECK_PARSES_OK( "" );
  119. CHECK_PARSES_OK( "foo" );
  120. CHECK_PARSES_OK( "foo<b>bar</b>" );
  121. CHECK_PARSES_OK( "1<big>2<small>3</small>4<big>5</big></big>6" );
  122. CHECK_PARSES_OK( "first <span foreground='red'>second</span> last" );
  123. CHECK_PARSES_OK( "first <span foreground='red' "
  124. "background='#ffffff'>second </span> last" );
  125. CHECK_PARSES_OK( "<span size='10240'>10pt</span>" );
  126. CHECK_PARSES_OK( "<span size='x-small'>much smaller</span>" );
  127. CHECK_PARSES_OK( "<span size='larger'>larger</span>" );
  128. CHECK_PARSES_OK
  129. (
  130. "<u>Please</u> notice: <i><b>any</b></i> <span foreground='grey'>"
  131. "<s><tt>bugs</tt></s></span> in this code are <span foreground='red' "
  132. "size='xx-large'>NOT</span> allowed."
  133. );
  134. CHECK_PARSES_OK( "foo&bar" );
  135. CHECK_PARSES_AS( "foo&amp;bar", "foo&&bar" );
  136. CHECK_PARSES_AS( "&lt;O&apos;Reilly&gt;", "<O'Reilly>" );
  137. CHECK_DOESNT_PARSE( "<" );
  138. CHECK_DOESNT_PARSE( "<b" );
  139. CHECK_DOESNT_PARSE( "<b>" );
  140. CHECK_DOESNT_PARSE( "<b></i>" );
  141. CHECK_DOESNT_PARSE( "<b><i></b></i>" );
  142. CHECK_DOESNT_PARSE( "<foo></foo>" );
  143. #undef CHECK_PARSES_OK
  144. #undef CHECK_DOESNT_PARSE
  145. }
  146. void MarkupTestCase::Quote()
  147. {
  148. CPPUNIT_ASSERT_EQUAL( "", wxMarkupParser::Quote("") );
  149. CPPUNIT_ASSERT_EQUAL( "foo", wxMarkupParser::Quote("foo") );
  150. CPPUNIT_ASSERT_EQUAL( "&lt;foo&gt;", wxMarkupParser::Quote("<foo>") );
  151. CPPUNIT_ASSERT_EQUAL( "B&amp;B", wxMarkupParser::Quote("B&B") );
  152. CPPUNIT_ASSERT_EQUAL( "&quot;&quot;", wxMarkupParser::Quote("\"\"") );
  153. }
  154. void MarkupTestCase::Strip()
  155. {
  156. #define CHECK_STRIP( text, stripped ) \
  157. CPPUNIT_ASSERT_EQUAL( stripped, wxMarkupParser::Strip(text) )
  158. CHECK_STRIP( "", "" );
  159. CHECK_STRIP( "foo", "foo" );
  160. CHECK_STRIP( "&lt;foo&gt;", "<foo>" );
  161. CHECK_STRIP( "<b>Big</b> problem", "Big problem" );
  162. CHECK_STRIP( "<span foreground='red'>c</span>"
  163. "<span background='green'>o</span>"
  164. "<span background='blue'>l</span>"
  165. "<span background='green'>o</span>"
  166. "<span foreground='yellow'>u</span>"
  167. "<span background='green'>r</span>",
  168. "colour" );
  169. #undef CHECK_STRIP
  170. }