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