]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/markuptest.cpp
reusing existing API
[wxWidgets.git] / tests / controls / markuptest.cpp
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 // RCS-ID: $Id$
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "testprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #ifndef WX_PRECOMP
17 #endif // WX_PRECOMP
18
19 #include "wx/private/markupparser.h"
20
21 class MarkupTestCase : public CppUnit::TestCase
22 {
23 public:
24 MarkupTestCase() { }
25
26 private:
27 CPPUNIT_TEST_SUITE( MarkupTestCase );
28 CPPUNIT_TEST( RoundTrip );
29 CPPUNIT_TEST( Quote );
30 CPPUNIT_TEST( Strip );
31 CPPUNIT_TEST_SUITE_END();
32
33 void RoundTrip();
34 void Quote();
35 void Strip();
36
37 wxDECLARE_NO_COPY_CLASS(MarkupTestCase);
38 };
39
40 // register in the unnamed registry so that these tests are run by default
41 CPPUNIT_TEST_SUITE_REGISTRATION( MarkupTestCase );
42
43 // also include in its own registry so that these tests can be run alone
44 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MarkupTestCase, "MarkupTestCase" );
45
46 void MarkupTestCase::RoundTrip()
47 {
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
52 {
53 public:
54 RoundTripOutput() { }
55
56 void Reset() { m_text.clear(); }
57
58 const wxString& GetText() const { return m_text; }
59
60
61 virtual void OnText(const wxString& text) { m_text += text; }
62
63 virtual void OnBoldStart() { m_text += "<b>"; }
64 virtual void OnBoldEnd() { m_text += "</b>"; }
65
66 virtual void OnItalicStart() { m_text += "<i>"; }
67 virtual void OnItalicEnd() { m_text += "</i>"; }
68
69 virtual void OnUnderlinedStart() { m_text += "<u>"; }
70 virtual void OnUnderlinedEnd() { m_text += "</u>"; }
71
72 virtual void OnStrikethroughStart() { m_text += "<s>"; }
73 virtual void OnStrikethroughEnd() { m_text += "</s>"; }
74
75 virtual void OnBigStart() { m_text += "<big>"; }
76 virtual void OnBigEnd() { m_text += "</big>"; }
77
78 virtual void OnSmallStart() { m_text += "<small>"; }
79 virtual void OnSmallEnd() { m_text += "</small>"; }
80
81 virtual void OnTeletypeStart() { m_text += "<tt>"; }
82 virtual void OnTeletypeEnd() { m_text += "</tt>"; }
83
84 virtual void OnSpanStart(const wxMarkupSpanAttributes& attrs)
85 {
86 m_text << "<span";
87
88 if ( !attrs.m_fgCol.empty() )
89 m_text << " foreground='" << attrs.m_fgCol << "'";
90
91 if ( !attrs.m_bgCol.empty() )
92 m_text << " background='" << attrs.m_bgCol << "'";
93
94 if ( !attrs.m_fontFace.empty() )
95 m_text << " face='" << attrs.m_fontFace << "'";
96
97 wxString size;
98 switch ( attrs.m_sizeKind )
99 {
100 case wxMarkupSpanAttributes::Size_Unspecified:
101 break;
102
103 case wxMarkupSpanAttributes::Size_Relative:
104 size << (attrs.m_fontSize > 0 ? "larger" : "smaller");
105 break;
106
107 case wxMarkupSpanAttributes::Size_Symbolic:
108 {
109 CPPUNIT_ASSERT( attrs.m_fontSize >= -3 &&
110 attrs.m_fontSize <= 3 );
111 static const char *cssSizes[] =
112 {
113 "xx-small", "x-small", "small",
114 "medium",
115 "large", "x-large", "xx-large",
116 };
117
118 size << cssSizes[attrs.m_fontSize + 3];
119 }
120 break;
121
122 case wxMarkupSpanAttributes::Size_PointParts:
123 size.Printf("%u", attrs.m_fontSize);
124 break;
125 }
126
127 if ( !size.empty() )
128 m_text << " size='" << size << '\'';
129
130 // TODO: Handle the rest of attributes.
131
132 m_text << ">";
133 }
134
135 virtual void OnSpanEnd(const wxMarkupSpanAttributes& WXUNUSED(attrs))
136 {
137 m_text += "</span>";
138 }
139
140 private:
141 wxString m_text;
142 };
143
144
145 RoundTripOutput output;
146 wxMarkupParser parser(output);
147
148 #define CHECK_PARSES_OK(text) \
149 output.Reset(); \
150 CPPUNIT_ASSERT( parser.Parse(text) ); \
151 CPPUNIT_ASSERT_EQUAL( text, output.GetText() )
152
153 #define CHECK_PARSES_AS(text, result) \
154 output.Reset(); \
155 CPPUNIT_ASSERT( parser.Parse(text) ); \
156 CPPUNIT_ASSERT_EQUAL( result, output.GetText() )
157
158 #define CHECK_DOESNT_PARSE(text) \
159 CPPUNIT_ASSERT( !parser.Parse(text) )
160
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>" );
171 CHECK_PARSES_OK
172 (
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."
176 );
177
178 CHECK_PARSES_OK( "foo&bar" );
179 CHECK_PARSES_AS( "foo&amp;bar", "foo&&bar" );
180 CHECK_PARSES_AS( "&lt;O&apos;Reilly&gt;", "<O'Reilly>" );
181
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>" );
188
189 #undef CHECK_PARSES_OK
190 #undef CHECK_DOESNT_PARSE
191 }
192
193 void MarkupTestCase::Quote()
194 {
195 CPPUNIT_ASSERT_EQUAL( "", wxMarkupParser::Quote("") );
196 CPPUNIT_ASSERT_EQUAL( "foo", wxMarkupParser::Quote("foo") );
197 CPPUNIT_ASSERT_EQUAL( "&lt;foo&gt;", wxMarkupParser::Quote("<foo>") );
198 CPPUNIT_ASSERT_EQUAL( "B&amp;B", wxMarkupParser::Quote("B&B") );
199 CPPUNIT_ASSERT_EQUAL( "&quot;&quot;", wxMarkupParser::Quote("\"\"") );
200 }
201
202 void MarkupTestCase::Strip()
203 {
204 #define CHECK_STRIP( text, stripped ) \
205 CPPUNIT_ASSERT_EQUAL( stripped, wxMarkupParser::Strip(text) )
206
207 CHECK_STRIP( "", "" );
208 CHECK_STRIP( "foo", "foo" );
209 CHECK_STRIP( "&lt;foo&gt;", "<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>",
217 "colour" );
218
219 #undef CHECK_STRIP
220 }