]>
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
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
18 #include "wx/private/markupparser.h"
20 class MarkupTestCase
: public CppUnit::TestCase
26 CPPUNIT_TEST_SUITE( MarkupTestCase
);
27 CPPUNIT_TEST( RoundTrip
);
28 CPPUNIT_TEST( Quote
);
29 CPPUNIT_TEST( Strip
);
30 CPPUNIT_TEST_SUITE_END();
36 wxDECLARE_NO_COPY_CLASS(MarkupTestCase
);
39 // register in the unnamed registry so that these tests are run by default
40 CPPUNIT_TEST_SUITE_REGISTRATION( MarkupTestCase
);
42 // also include in its own registry so that these tests can be run alone
43 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MarkupTestCase
, "MarkupTestCase" );
45 void MarkupTestCase::RoundTrip()
47 // Define a wxMarkupParserOutput object which produces the same markup
48 // string on output. This is, of course, perfectly useless, but allows us
49 // to test that parsing works as expected.
50 class RoundTripOutput
: public wxMarkupParserOutput
55 void Reset() { m_text
.clear(); }
57 const wxString
& GetText() const { return m_text
; }
60 virtual void OnText(const wxString
& text
) { m_text
+= text
; }
62 virtual void OnBoldStart() { m_text
+= "<b>"; }
63 virtual void OnBoldEnd() { m_text
+= "</b>"; }
65 virtual void OnItalicStart() { m_text
+= "<i>"; }
66 virtual void OnItalicEnd() { m_text
+= "</i>"; }
68 virtual void OnUnderlinedStart() { m_text
+= "<u>"; }
69 virtual void OnUnderlinedEnd() { m_text
+= "</u>"; }
71 virtual void OnStrikethroughStart() { m_text
+= "<s>"; }
72 virtual void OnStrikethroughEnd() { m_text
+= "</s>"; }
74 virtual void OnBigStart() { m_text
+= "<big>"; }
75 virtual void OnBigEnd() { m_text
+= "</big>"; }
77 virtual void OnSmallStart() { m_text
+= "<small>"; }
78 virtual void OnSmallEnd() { m_text
+= "</small>"; }
80 virtual void OnTeletypeStart() { m_text
+= "<tt>"; }
81 virtual void OnTeletypeEnd() { m_text
+= "</tt>"; }
83 virtual void OnSpanStart(const wxMarkupSpanAttributes
& attrs
)
87 if ( !attrs
.m_fgCol
.empty() )
88 m_text
<< " foreground='" << attrs
.m_fgCol
<< "'";
90 if ( !attrs
.m_bgCol
.empty() )
91 m_text
<< " background='" << attrs
.m_bgCol
<< "'";
93 if ( !attrs
.m_fontFace
.empty() )
94 m_text
<< " face='" << attrs
.m_fontFace
<< "'";
97 switch ( attrs
.m_sizeKind
)
99 case wxMarkupSpanAttributes::Size_Unspecified
:
102 case wxMarkupSpanAttributes::Size_Relative
:
103 size
<< (attrs
.m_fontSize
> 0 ? "larger" : "smaller");
106 case wxMarkupSpanAttributes::Size_Symbolic
:
108 CPPUNIT_ASSERT( attrs
.m_fontSize
>= -3 &&
109 attrs
.m_fontSize
<= 3 );
110 static const char *cssSizes
[] =
112 "xx-small", "x-small", "small",
114 "large", "x-large", "xx-large",
117 size
<< cssSizes
[attrs
.m_fontSize
+ 3];
121 case wxMarkupSpanAttributes::Size_PointParts
:
122 size
.Printf("%u", attrs
.m_fontSize
);
127 m_text
<< " size='" << size
<< '\'';
129 // TODO: Handle the rest of attributes.
134 virtual void OnSpanEnd(const wxMarkupSpanAttributes
& WXUNUSED(attrs
))
144 RoundTripOutput output
;
145 wxMarkupParser
parser(output
);
147 #define CHECK_PARSES_OK(text) \
149 CPPUNIT_ASSERT( parser.Parse(text) ); \
150 CPPUNIT_ASSERT_EQUAL( text, output.GetText() )
152 #define CHECK_PARSES_AS(text, result) \
154 CPPUNIT_ASSERT( parser.Parse(text) ); \
155 CPPUNIT_ASSERT_EQUAL( result, output.GetText() )
157 #define CHECK_DOESNT_PARSE(text) \
158 CPPUNIT_ASSERT( !parser.Parse(text) )
160 CHECK_PARSES_OK( "" );
161 CHECK_PARSES_OK( "foo" );
162 CHECK_PARSES_OK( "foo<b>bar</b>" );
163 CHECK_PARSES_OK( "1<big>2<small>3</small>4<big>5</big></big>6" );
164 CHECK_PARSES_OK( "first <span foreground='red'>second</span> last" );
165 CHECK_PARSES_OK( "first <span foreground='red' "
166 "background='#ffffff'>second </span> last" );
167 CHECK_PARSES_OK( "<span size='10240'>10pt</span>" );
168 CHECK_PARSES_OK( "<span size='x-small'>much smaller</span>" );
169 CHECK_PARSES_OK( "<span size='larger'>larger</span>" );
172 "<u>Please</u> notice: <i><b>any</b></i> <span foreground='grey'>"
173 "<s><tt>bugs</tt></s></span> in this code are <span foreground='red' "
174 "size='xx-large'>NOT</span> allowed."
177 CHECK_PARSES_OK( "foo&bar" );
178 CHECK_PARSES_AS( "foo&bar", "foo&&bar" );
179 CHECK_PARSES_AS( "<O'Reilly>", "<O'Reilly>" );
181 CHECK_DOESNT_PARSE( "<" );
182 CHECK_DOESNT_PARSE( "<b" );
183 CHECK_DOESNT_PARSE( "<b>" );
184 CHECK_DOESNT_PARSE( "<b></i>" );
185 CHECK_DOESNT_PARSE( "<b><i></b></i>" );
186 CHECK_DOESNT_PARSE( "<foo></foo>" );
188 #undef CHECK_PARSES_OK
189 #undef CHECK_DOESNT_PARSE
192 void MarkupTestCase::Quote()
194 CPPUNIT_ASSERT_EQUAL( "", wxMarkupParser::Quote("") );
195 CPPUNIT_ASSERT_EQUAL( "foo", wxMarkupParser::Quote("foo") );
196 CPPUNIT_ASSERT_EQUAL( "<foo>", wxMarkupParser::Quote("<foo>") );
197 CPPUNIT_ASSERT_EQUAL( "B&B", wxMarkupParser::Quote("B&B") );
198 CPPUNIT_ASSERT_EQUAL( """", wxMarkupParser::Quote("\"\"") );
201 void MarkupTestCase::Strip()
203 #define CHECK_STRIP( text, stripped ) \
204 CPPUNIT_ASSERT_EQUAL( stripped, wxMarkupParser::Strip(text) )
206 CHECK_STRIP( "", "" );
207 CHECK_STRIP( "foo", "foo" );
208 CHECK_STRIP( "<foo>", "<foo>" );
209 CHECK_STRIP( "<b>Big</b> problem", "Big problem" );
210 CHECK_STRIP( "<span foreground='red'>c</span>"
211 "<span background='green'>o</span>"
212 "<span background='blue'>l</span>"
213 "<span background='green'>o</span>"
214 "<span foreground='yellow'>u</span>"
215 "<span background='green'>r</span>",