]>
git.saurik.com Git - wxWidgets.git/blob - tests/controls/markuptest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/markup.cpp
3 // Purpose: wxMarkupParser and related classes unit tests
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
19 #include "wx/private/markupparser.h"
21 class MarkupTestCase
: public CppUnit::TestCase
27 CPPUNIT_TEST_SUITE( MarkupTestCase
);
28 CPPUNIT_TEST( RoundTrip
);
29 CPPUNIT_TEST( Quote
);
30 CPPUNIT_TEST( Strip
);
31 CPPUNIT_TEST_SUITE_END();
37 wxDECLARE_NO_COPY_CLASS(MarkupTestCase
);
40 // register in the unnamed registry so that these tests are run by default
41 CPPUNIT_TEST_SUITE_REGISTRATION( MarkupTestCase
);
43 // also include in it's own registry so that these tests can be run alone
44 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MarkupTestCase
, "MarkupTestCase" );
46 void MarkupTestCase::RoundTrip()
48 // Define a wxMarkupParserOutput object which produces the same markup
49 // string on output. This is, of course, perfectly useless, but allows us
50 // to test that parsing works as expected.
51 class RoundTripOutput
: public wxMarkupParserOutput
56 void Reset() { m_text
.clear(); }
58 const wxString
& GetText() const { return m_text
; }
61 virtual void OnText(const wxString
& text
) { m_text
+= text
; }
63 virtual void OnBoldStart() { m_text
+= "<b>"; }
64 virtual void OnBoldEnd() { m_text
+= "</b>"; }
66 virtual void OnItalicStart() { m_text
+= "<i>"; }
67 virtual void OnItalicEnd() { m_text
+= "</i>"; }
69 virtual void OnUnderlinedStart() { m_text
+= "<u>"; }
70 virtual void OnUnderlinedEnd() { m_text
+= "</u>"; }
72 virtual void OnStrikethroughStart() { m_text
+= "<s>"; }
73 virtual void OnStrikethroughEnd() { m_text
+= "</s>"; }
75 virtual void OnBigStart() { m_text
+= "<big>"; }
76 virtual void OnBigEnd() { m_text
+= "</big>"; }
78 virtual void OnSmallStart() { m_text
+= "<small>"; }
79 virtual void OnSmallEnd() { m_text
+= "</small>"; }
81 virtual void OnTeletypeStart() { m_text
+= "<tt>"; }
82 virtual void OnTeletypeEnd() { m_text
+= "</tt>"; }
84 virtual void OnSpanStart(const wxMarkupSpanAttributes
& attrs
)
88 if ( !attrs
.m_fgCol
.empty() )
89 m_text
<< " foreground='" << attrs
.m_fgCol
<< "'";
91 if ( !attrs
.m_bgCol
.empty() )
92 m_text
<< " background='" << attrs
.m_bgCol
<< "'";
94 if ( !attrs
.m_fontFace
.empty() )
95 m_text
<< " face='" << attrs
.m_fontFace
<< "'";
98 switch ( attrs
.m_sizeKind
)
100 case wxMarkupSpanAttributes::Size_Unspecified
:
103 case wxMarkupSpanAttributes::Size_Relative
:
104 size
<< (attrs
.m_fontSize
> 0 ? "larger" : "smaller");
107 case wxMarkupSpanAttributes::Size_Symbolic
:
109 CPPUNIT_ASSERT( attrs
.m_fontSize
>= -3 &&
110 attrs
.m_fontSize
<= 3 );
111 static const char *cssSizes
[] =
113 "xx-small", "x-small", "small",
115 "large", "x-large", "xx-large",
118 size
<< cssSizes
[attrs
.m_fontSize
+ 3];
122 case wxMarkupSpanAttributes::Size_PointParts
:
123 size
.Printf("%u", attrs
.m_fontSize
);
128 m_text
<< " size='" << size
<< '\'';
130 // TODO: Handle the rest of attributes.
135 virtual void OnSpanEnd(const wxMarkupSpanAttributes
& WXUNUSED(attrs
))
145 RoundTripOutput output
;
146 wxMarkupParser
parser(output
);
148 #define CHECK_PARSES_OK(text) \
150 CPPUNIT_ASSERT( parser.Parse(text) ); \
151 CPPUNIT_ASSERT_EQUAL( text, output.GetText() )
153 #define CHECK_PARSES_AS(text, result) \
155 CPPUNIT_ASSERT( parser.Parse(text) ); \
156 CPPUNIT_ASSERT_EQUAL( result, output.GetText() )
158 #define CHECK_DOESNT_PARSE(text) \
159 CPPUNIT_ASSERT( !parser.Parse(text) )
161 CHECK_PARSES_OK( "" );
162 CHECK_PARSES_OK( "foo" );
163 CHECK_PARSES_OK( "foo<b>bar</b>" );
164 CHECK_PARSES_OK( "1<big>2<small>3</small>4<big>5</big></big>6" );
165 CHECK_PARSES_OK( "first <span foreground='red'>second</span> last" );
166 CHECK_PARSES_OK( "first <span foreground='red' "
167 "background='#ffffff'>second </span> last" );
168 CHECK_PARSES_OK( "<span size='10240'>10pt</span>" );
169 CHECK_PARSES_OK( "<span size='x-small'>much smaller</span>" );
170 CHECK_PARSES_OK( "<span size='larger'>larger</span>" );
173 "<u>Please</u> notice: <i><b>any</b></i> <span foreground='grey'>"
174 "<s><tt>bugs</tt></s></span> in this code are <span foreground='red' "
175 "size='xx-large'>NOT</span> allowed."
178 CHECK_PARSES_OK( "foo&bar" );
179 CHECK_PARSES_AS( "foo&bar", "foo&&bar" );
180 CHECK_PARSES_AS( "<O'Reilly>", "<O'Reilly>" );
182 CHECK_DOESNT_PARSE( "<" );
183 CHECK_DOESNT_PARSE( "<b" );
184 CHECK_DOESNT_PARSE( "<b>" );
185 CHECK_DOESNT_PARSE( "<b></i>" );
186 CHECK_DOESNT_PARSE( "<b><i></b></i>" );
187 CHECK_DOESNT_PARSE( "<foo></foo>" );
189 #undef CHECK_PARSES_OK
190 #undef CHECK_DOESNT_PARSE
193 void MarkupTestCase::Quote()
195 CPPUNIT_ASSERT_EQUAL( "", wxMarkupParser::Quote("") );
196 CPPUNIT_ASSERT_EQUAL( "foo", wxMarkupParser::Quote("foo") );
197 CPPUNIT_ASSERT_EQUAL( "<foo>", wxMarkupParser::Quote("<foo>") );
198 CPPUNIT_ASSERT_EQUAL( "B&B", wxMarkupParser::Quote("B&B") );
199 CPPUNIT_ASSERT_EQUAL( """", wxMarkupParser::Quote("\"\"") );
202 void MarkupTestCase::Strip()
204 #define CHECK_STRIP( text, stripped ) \
205 CPPUNIT_ASSERT_EQUAL( stripped, wxMarkupParser::Strip(text) )
207 CHECK_STRIP( "", "" );
208 CHECK_STRIP( "foo", "foo" );
209 CHECK_STRIP( "<foo>", "<foo>" );
210 CHECK_STRIP( "<b>Big</b> problem", "Big problem" );
211 CHECK_STRIP( "<span foreground='red'>c</span>"
212 "<span background='green'>o</span>"
213 "<span background='blue'>l</span>"
214 "<span background='green'>o</span>"
215 "<span foreground='yellow'>u</span>"
216 "<span background='green'>r</span>",