android: Update app-specific/MIME type icons
[LibreOffice.git] / starmath / qa / cppunit / test_nodetotextvisitors.cxx
blob11ef5affcc4c923b677e74adcceb81c1c7b32ff2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
11 #include <test/bootstrapfixture.hxx>
13 #include <vcl/virdev.hxx>
14 #include <sfx2/sfxmodelfactory.hxx>
15 #include <smdll.hxx>
17 #include <document.hxx>
18 #include <node.hxx>
19 #include <parse5.hxx>
20 #include <visitors.hxx>
21 #include <cursor.hxx>
23 #include "mock-visitor.hxx"
24 #include <memory>
26 typedef tools::SvRef<SmDocShell> SmDocShellRef;
28 using namespace ::com::sun::star;
30 namespace
32 class Test : public test::BootstrapFixture
34 public:
35 // init
36 virtual void setUp() override;
37 virtual void tearDown() override;
39 // tests
40 void SimpleUnaryOp();
41 void SimpleBinaryOp();
42 void SimpleRelationalOp();
43 void SimpleSetOp();
44 void SimpleFunctions();
45 void SimpleOperators();
46 void SimpleAttributes();
47 void SimpleMisc();
48 void SimpleBrackets();
49 void SimpleFormats();
50 void SimpleGreekChars();
51 void SimpleSpecialChars();
52 void testBinomInBinHor();
53 void testBinVerInUnary();
54 void testBinHorInSubSup();
55 void testUnaryInMixedNumberAsNumerator();
56 void testMiscEquivalent();
57 void testParser();
59 CPPUNIT_TEST_SUITE(Test);
60 CPPUNIT_TEST(SimpleUnaryOp);
61 CPPUNIT_TEST(SimpleBinaryOp);
62 CPPUNIT_TEST(SimpleRelationalOp);
63 CPPUNIT_TEST(SimpleSetOp);
64 CPPUNIT_TEST(SimpleFunctions);
65 CPPUNIT_TEST(SimpleOperators);
66 CPPUNIT_TEST(SimpleAttributes);
67 CPPUNIT_TEST(SimpleMisc);
68 CPPUNIT_TEST(SimpleBrackets);
69 CPPUNIT_TEST(SimpleFormats);
70 CPPUNIT_TEST(SimpleGreekChars);
71 CPPUNIT_TEST(SimpleSpecialChars);
72 CPPUNIT_TEST(testBinomInBinHor);
73 CPPUNIT_TEST(testBinVerInUnary);
74 CPPUNIT_TEST(testBinHorInSubSup);
75 CPPUNIT_TEST(testUnaryInMixedNumberAsNumerator);
76 CPPUNIT_TEST(testMiscEquivalent);
77 CPPUNIT_TEST(testParser);
78 CPPUNIT_TEST_SUITE_END();
80 private:
81 SmDocShellRef xDocShRef;
82 void parseandparseagain(const char* input, const char* test_name);
83 void ParseAndCheck(const char* input, const char* expected, const char* test_name);
84 void ParseAndCompare(const char* formula1, const char* formula2, const char* test_name);
87 void Test::setUp()
89 BootstrapFixture::setUp();
91 SmGlobals::ensure();
93 xDocShRef = new SmDocShell(SfxModelFlags::EMBEDDED_OBJECT);
94 xDocShRef->DoInitNew();
97 void Test::tearDown()
99 xDocShRef->DoClose();
100 xDocShRef.clear();
101 BootstrapFixture::tearDown();
105 * Most of the formula commands in this file came from:
106 * http://wiki.openoffice.org/wiki/Template:Math_commands_reference
107 * which was licensed with a
108 * Creative Common Attribution 3.0 license and written by:
109 * Jeanweber, Weegreenblobbie, Jdpipe, TJFrazier, Ysangkok, B michaelsen, Spellbreaker
112 void Test::SimpleUnaryOp()
114 parseandparseagain("+1", "Positive (plus)");
115 parseandparseagain("-2", "Negative (minus)");
116 parseandparseagain("+-3", "Plus/minus");
117 parseandparseagain("-+4", "Minus/plus");
118 parseandparseagain("neg a", "Boolean 'not'");
119 parseandparseagain("fact a", "Factorial");
120 parseandparseagain("- { 1 over 2 }", "BinVer in Unary 1");
121 ParseAndCheck("- { 1 over 2 }", "- { 1 over 2 }", "BinVer in Unary 1");
122 parseandparseagain("{- { 1 over 2 } }", "BinVer in Unary 2");
123 parseandparseagain("- 1 over 2", "Unary in BinVer as numerator 1");
124 parseandparseagain("{ - 1 } over 2", "Unary in BinVer as numerator 2");
125 parseandparseagain("1 over - 2", "Unary in BinVer as denominator 1");
126 parseandparseagain("1 over { - 2 }", "Unary in BinVer as denominator 2");
127 parseandparseagain("2 { - 1 over 2 }", "Mixed number with Unary in denominator 1");
128 parseandparseagain("2 { - 1 } over 2", "Mixed number with Unary in denominator 2");
129 parseandparseagain("- 1 + 2", "Unary in BinHor");
132 void Test::SimpleBinaryOp()
134 parseandparseagain("a + b", "Addition");
135 parseandparseagain("a cdot b", "Dot product");
136 parseandparseagain("a times b", "Cross product");
137 parseandparseagain("a * b", "Multiplication (asterisk)");
138 parseandparseagain("a and b", "Boolean 'and'");
139 parseandparseagain("a - b", "Subtraction");
140 parseandparseagain("a over b", "Division (as a fraction)");
141 parseandparseagain("a div b", "Division (as an operator)");
142 parseandparseagain("a / b", "Division (with a slash)");
143 parseandparseagain("a or b", "Boolean 'or'");
144 parseandparseagain("a circ b", "Concatenation");
147 void Test::SimpleRelationalOp()
149 parseandparseagain("a = b", "Is equal");
150 parseandparseagain("a <> b", "Is not equal");
151 parseandparseagain("a approx 2", "Approximately");
152 parseandparseagain("a divides b", "Divides");
153 parseandparseagain("a ndivides b", "Does not divide");
154 parseandparseagain("a < 2", "Less than");
155 parseandparseagain("a > 2", "Greater than");
156 parseandparseagain("a simeq b", "Similar to or equal");
157 parseandparseagain("a parallel b", "Parallel");
158 parseandparseagain("a ortho b", "Orthogonal to");
159 parseandparseagain("a leslant b", "Less than or equal to");
160 parseandparseagain("a geslant b", "Greater than or equal to");
161 parseandparseagain("a sim b", "Similar to");
162 parseandparseagain("a equiv b", "Congruent");
163 parseandparseagain("a <= b", "Less than or equal to");
164 parseandparseagain("a >= b", "Greater than or equal to");
165 parseandparseagain("a prop b", "Proportional");
166 parseandparseagain("a toward b", "Toward");
167 parseandparseagain("a dlarrow b", "Arrow left");
168 parseandparseagain("a dlrarrow b", "Double arrow left and right");
169 parseandparseagain("drarrow b", "Arrow right");
172 void Test::SimpleSetOp()
174 parseandparseagain("a in B", "Is in");
175 parseandparseagain("a notin B", "Is not in");
176 parseandparseagain("A owns b", "Owns");
177 parseandparseagain("emptyset", "Empty set");
178 parseandparseagain("A intersection B", "Intersection");
179 parseandparseagain("A union B", "Union");
180 parseandparseagain("A setminus B", "Difference");
181 parseandparseagain("A slash B", "Quotient");
182 parseandparseagain("aleph", "Aleph");
183 parseandparseagain("A subset B", "Subset");
184 parseandparseagain("A subseteq B", "Subset or equal to");
185 parseandparseagain("A supset B", "Superset");
186 parseandparseagain("A supseteq B", "Superset or equal to");
187 parseandparseagain("A nsubset B", "Not subset");
188 parseandparseagain("A nsubseteq B", "Not subset or equal");
189 parseandparseagain("A nsupset B", "Not superset");
190 parseandparseagain("A nsupseteq B", "Not superset or equal");
191 parseandparseagain("setN", "Set of natural numbers");
192 parseandparseagain("setZ", "Set of integers");
193 parseandparseagain("setQ", "Set of rational numbers");
194 parseandparseagain("setR", "Set of real numbers");
195 parseandparseagain("setC", "Set of complex numbers");
198 void Test::SimpleFunctions()
200 parseandparseagain("func e^{a}", "Exponential");
201 parseandparseagain("ln(a)", "Natural logarithm");
202 parseandparseagain("exp(a)", "Exponential function");
203 parseandparseagain("log(a)", "Logarithm");
204 parseandparseagain("a^{b}", "Power");
205 parseandparseagain("sin(a)", "Sine");
206 parseandparseagain("cos(a)", "Cosine");
207 parseandparseagain("tan(a)", "Tangent");
208 parseandparseagain("cot(a)", "Cotangent");
209 parseandparseagain("sqrt{a}", "Square root");
210 parseandparseagain("arcsin(a)", "Arcsine");
211 parseandparseagain("arccos(a)", "Arccosine");
212 parseandparseagain("arctan(a)", "Arctangent");
213 parseandparseagain("arccot(a)", "Arc cotangent");
214 parseandparseagain("nroot{a}{b}", "nth root");
215 parseandparseagain("sinh(a)", "Hyperbolic sine");
216 parseandparseagain("cosh(a)", "Hyperbolic cosine");
217 parseandparseagain("tanh(a)", "Hyperbolic tangent");
218 parseandparseagain("coth(a)", "Hyperbolic cotangent");
219 parseandparseagain("abs{a}", "Absolute value");
220 parseandparseagain("arsinh(a)", "Arc hyperbolic sine");
221 parseandparseagain("arcosh(a)", "Arc hyperbolic cosine");
222 parseandparseagain("artanh(a)", "Arc hyperbolic tangent");
223 parseandparseagain("arcoth(a)", "Arc hyperbolic cotangent");
226 void Test::SimpleOperators()
228 parseandparseagain("lim{a}", "Limit");
229 parseandparseagain("sum{a}", "Sum");
230 parseandparseagain("prod{a}", "Product");
231 parseandparseagain("coprod{a}", "Coproduct");
232 parseandparseagain("int from {r_0} to {r_t} a",
233 "Upper and lower bounds shown with integral (from & to)");
234 ParseAndCheck("int csup {r_0} csub {r_t} a", "int csup { r _ 0 } csub { r _ t } a",
235 "Upper and lower bounds shown with integral (csub & csup)");
236 ParseAndCheck("sum csup { size 8 { x - 1 } } csub { size 8 a } b",
237 "sum csup { size 8 { x - 1 } } csub { size 8 a } b",
238 "Sum with sized upper and lower bounds");
239 parseandparseagain("int{a}", "Integral");
240 parseandparseagain("intd_{1}^{2}{x dx}", "Dynamically-sized integral");
241 parseandparseagain("iint{a}", "Double integral");
242 parseandparseagain("iiint{a}", "Triple integral");
243 parseandparseagain("sum from{3}b", "Lower bound shown with summation symbol");
244 parseandparseagain("lint a", "Contour integral");
245 parseandparseagain("llint a", "Double curved integral");
246 parseandparseagain("lllint a", "Triple curved integral");
247 parseandparseagain("prod from {i=1} to {n} {(i+1)}", "Product with range");
248 ParseAndCheck("%Ux2135", "%Ux2135", "fdo#77831");
251 void Test::SimpleAttributes()
253 parseandparseagain("acute a", "Acute accent");
254 parseandparseagain("grave a", "Grave accent");
255 parseandparseagain("check a", "Reverse circumflex");
256 parseandparseagain("breve a", "Breve");
257 parseandparseagain("circle a", "Circle");
258 parseandparseagain("vec a", "Vector arrow");
259 parseandparseagain("harpoon a", "Harpoon");
260 parseandparseagain("tilde a", "Tilde");
261 parseandparseagain("hat a", "Circumflex");
262 parseandparseagain("bar a", "Line above");
263 parseandparseagain("dot a", "Dot");
264 parseandparseagain("widevec abc", "Wide vector arrow");
265 parseandparseagain("wideharpoon abc", "Wide harpoon");
266 parseandparseagain("widetilde abc", "Wide tilde");
267 parseandparseagain("widehat abc", "Wide circumflex");
268 parseandparseagain("ddot a", "Double dot");
269 parseandparseagain("overline abc", "Line over");
270 parseandparseagain("underline abc", "Line under");
271 parseandparseagain("overstrike abc", "Line through");
272 parseandparseagain("dddot a", "Triple dot");
273 parseandparseagain("phantom a", "Transparent (useful to get a placeholder of a given size)");
274 parseandparseagain("bold a", "Bold font");
275 parseandparseagain("ital a", "Italic font");
276 parseandparseagain("nitalic a", "Roman (non-italic) font 1");
277 parseandparseagain("\"a\"", "Roman (non-italic) font 2");
278 parseandparseagain("size 16 qv", "Resize font");
279 parseandparseagain("font sans qv", "Sans serif font");
280 parseandparseagain("font serif qv", "Serif font");
281 parseandparseagain("font fixed qv", "Fixed font");
282 parseandparseagain("color cyan qv", "Cyan color");
283 parseandparseagain("color yellow qv", "Yellow color");
284 parseandparseagain("color white qv", "White color");
285 parseandparseagain("color green qv", "Green color");
286 parseandparseagain("color blue qv", "Blue color");
287 parseandparseagain("color red qv", "Red color");
288 parseandparseagain("color green X qv", "Green color changes back");
289 parseandparseagain("color green {X qv}", "Green color, more than one item");
292 void Test::SimpleMisc()
294 parseandparseagain("infinity", "Infinity");
295 parseandparseagain("partial", "Partial");
296 parseandparseagain("nabla", "Nabla");
297 parseandparseagain("exists", "There exists");
298 parseandparseagain("notexists", "There not exists");
299 parseandparseagain("forall", "For all");
300 parseandparseagain("hbar", "H bar");
301 parseandparseagain("lambdabar", "Lambda bar");
302 parseandparseagain("re", "Real part");
303 parseandparseagain("im", "Imaginary part");
304 parseandparseagain("wp", "Weierstrass p");
305 parseandparseagain("leftarrow", "Left arrow");
306 parseandparseagain("rightarrow", "Right arrow");
307 parseandparseagain("uparrow", "Up arrow");
308 parseandparseagain("downarrow", "Down arrow");
309 parseandparseagain("dotslow", "Dots at bottom");
310 parseandparseagain("dotsaxis", "Dots at middle");
311 parseandparseagain("dotsvert", "Dots vertical");
312 parseandparseagain("dotsup", "Dots diagonal upward");
313 parseandparseagain("dotsdown", "Dots diagonal downward");
316 void Test::SimpleBrackets()
318 parseandparseagain("(a)", "Round Brackets");
319 parseandparseagain("[b]", "Square Brackets");
320 parseandparseagain("ldbracket c rdbracket", "Double Square Brackets");
321 parseandparseagain("lline a rline", "Single line or absolute");
322 parseandparseagain("abs a", "Single line or absolute 2");
323 parseandparseagain("ldline a rdline", "Double line");
324 parseandparseagain("lbrace w rbrace", "Braces");
325 parseandparseagain("left lbrace stack{0, n <> 0 # 1, n = 1} right none", "Single left brace");
326 parseandparseagain("langle d rangle", "Angle Brackets");
327 parseandparseagain("langle a mline b rangle", "Operator Brackets");
328 parseandparseagain("{a}", "Group brackets (used for program control)");
329 parseandparseagain("left ( stack{a # b # z} right )", "Round brackets scalable");
330 parseandparseagain("left [ stack{x # y} right ]", "Square brackets scalable");
331 parseandparseagain("left ldbracket c right rdbracket", "Double square brackets scalable");
332 parseandparseagain("left lline a right rline", "Line scalable");
333 parseandparseagain("left ldline d right rdline", "Double line scalable");
334 parseandparseagain("left lbrace e right rbrace", "Brace scalable");
335 parseandparseagain("left langle f right rangle", "Angle bracket scalable");
336 parseandparseagain("left langle g mline h right rangle", "Operator brackets scalable");
337 parseandparseagain("{a} overbrace b", "Over brace scalable");
338 parseandparseagain("{b} underbrace a", "Under brace scalable");
341 void Test::SimpleFormats()
343 parseandparseagain("a lsup{b}", "Left superscript");
344 parseandparseagain("a csup{b}", "Center superscript");
345 parseandparseagain("a^{b}", "Right superscript");
346 parseandparseagain("a lsub{b}", "Left subscript");
347 parseandparseagain("a csub{b}", "Center subscript");
348 parseandparseagain("a_{b}", "Right subscript");
349 parseandparseagain("stack { Hello world # alignl (a) }", "Align character to left");
350 parseandparseagain("stack{Hello world # alignc(a)}", "Align character to center");
351 parseandparseagain("stack { Hello world # alignr(a)}", "Align character to right");
352 parseandparseagain("binom{a}{b}", "Vertical stack of 2");
353 parseandparseagain("stack{a # b # z}", "Vertical stack, more than 2");
354 parseandparseagain("matrix{a # b ## c # d}", "Matrix");
355 parseandparseagain("matrix{a # \"=\" # alignl{b} ## {} # \"=\" # alignl{c+1}}",
356 "Equations aligned at '=' (using 'matrix') ");
357 parseandparseagain("stack{alignl{a} = b # alignl{phantom{a} = c+1}}",
358 "Equations aligned at '=' (using 'phantom') ");
359 parseandparseagain("asldkfjo newline sadkfj", "New line");
360 parseandparseagain("stuff `stuff", "Small gap (grave)");
361 parseandparseagain("stuff~stuff", "Large gap (tilde)");
364 void Test::SimpleGreekChars()
366 parseandparseagain("%ALPHA", "Capital alpha");
367 parseandparseagain("%BETA", "Capital beta");
368 parseandparseagain("%CHI", "Capital chi");
369 parseandparseagain("%DELTA", "Capital delta");
370 parseandparseagain("%EPSILON", "Capital epsilon");
371 parseandparseagain("%ETA", "Capital eta");
372 parseandparseagain("%GAMMA", "Capital gamma");
373 parseandparseagain("%IOTA", "Capital iota");
374 parseandparseagain("%LAMBDA", "Capital lambda");
375 parseandparseagain("%MU", "Capital mu");
376 parseandparseagain("%NU", "Capital nu");
377 parseandparseagain("%OMEGA", "Capital omega");
378 parseandparseagain("%OMICRON", "Capital omicron");
379 parseandparseagain("%PHI", "Capital phi");
380 parseandparseagain("%PI", "Capital pi");
381 parseandparseagain("%PSI", "Capital psi");
382 parseandparseagain("%RHO", "Capital rho");
383 parseandparseagain("%SIGMA", "Capital sigma");
384 parseandparseagain("%TAU", "Capital tau");
385 parseandparseagain("%THETA", "Capital theta");
386 parseandparseagain("%UPSILON", "Capital upsilon");
387 parseandparseagain("%XI", "Capital xi");
388 parseandparseagain("%ZETA", "Capital zeta");
389 parseandparseagain("%alpha", "lowercase alpha");
390 parseandparseagain("%beta", "lowercase beta");
391 parseandparseagain("%chi", "lowercase chi");
392 parseandparseagain("%delta", "lowercase delta");
393 parseandparseagain("%epsilon", "lowercase epsilon");
394 parseandparseagain("%eta", "lowercase eta");
395 parseandparseagain("%gamma", "lowercase gamma");
396 parseandparseagain("%iota", "lowercase iota");
397 parseandparseagain("%kappa", "lowercase kappa");
398 parseandparseagain("%lambda", "lowercase lambda");
399 parseandparseagain("%mu", "lowercase mu");
400 parseandparseagain("%nu", "lowercase nu");
401 parseandparseagain("%omega", "lowercase omega");
402 parseandparseagain("%omicron", "lowercase omicron");
403 parseandparseagain("%phi", "lowercase phi");
404 parseandparseagain("%pi", "lowercase pi");
405 parseandparseagain("%psi", "lowercase psi");
406 parseandparseagain("%rho", "lowercase rho");
407 parseandparseagain("%sigma", "lowercase sigma");
408 parseandparseagain("%tau", "lowercase tau");
409 parseandparseagain("%theta", "lowercase theta");
410 parseandparseagain("%upsilon", "lowercase upsilon");
411 parseandparseagain("%varepsilon", "Varepsilon");
412 parseandparseagain("%varphi", "Varphi");
413 parseandparseagain("%varpi", "Varpi");
414 parseandparseagain("%varrho", "Varrho");
415 parseandparseagain("%varsigma", "Varsigma");
416 parseandparseagain("%vartheta", "Vartheta");
417 parseandparseagain("%xi", "lowercase xi");
418 parseandparseagain("%zeta", "lowercase zeta");
421 void Test::SimpleSpecialChars()
423 parseandparseagain("%and", "And");
424 parseandparseagain("%angle", "Angle");
425 parseandparseagain("%element", "Element");
426 parseandparseagain("%identical", "Identical");
427 parseandparseagain("%infinite", "Infinite");
428 parseandparseagain("%noelement", "No element");
429 parseandparseagain("%notequal", "Not equal");
430 parseandparseagain("%or", "Or");
431 parseandparseagain("%perthousand", "Per thousand");
432 parseandparseagain("%strictlygreaterthan", "Strictly greater than");
433 parseandparseagain("%strictlylessthan", "Strictly less than");
434 parseandparseagain("%tendto", "Tend to");
437 /* This test takes a formula command, parses it, converts the node to text,
438 * parses it again, converts it to text again, and compares the values.
439 * Doing this doesn't prove that it is correct, but it should prove that the
440 * meaning of the original command is not being changed.
442 void Test::parseandparseagain(const char* formula, const char* test_name)
444 OUString output1, output2;
446 // parse 1
447 OUString input = OUString::createFromAscii(formula);
448 auto pNode1 = SmParser5().ParseExpression(input);
449 pNode1->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
450 SmNodeToTextVisitor(pNode1.get(), output1);
452 // parse 2
453 auto pNode2 = SmParser5().ParseExpression(output1);
454 pNode2->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
455 SmNodeToTextVisitor(pNode2.get(), output2);
457 // compare
458 CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name, output1, output2);
460 // auxiliary test for Accept()
461 std::unique_ptr<MockVisitor> mv(new MockVisitor);
462 pNode1->Accept(mv.get());
463 pNode2->Accept(mv.get());
466 void Test::ParseAndCheck(const char* formula, const char* expected, const char* test_name)
468 OUString sOutput;
470 // parse
471 OUString sInput = OUString::createFromAscii(formula);
472 auto pNode = SmParser5().ParseExpression(sInput);
473 pNode->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
474 SmNodeToTextVisitor(pNode.get(), sOutput);
476 // compare
477 OUString sExpected = OUString::createFromAscii(expected);
478 CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name, sExpected, sOutput);
480 // auxiliary test for Accept()
481 std::unique_ptr<MockVisitor> mv(new MockVisitor);
482 pNode->Accept(mv.get());
485 // Parse two formula commands and verify that they give the same output
486 void Test::ParseAndCompare(const char* formula1, const char* formula2, const char* test_name)
488 OUString sOutput1, sOutput2;
490 // parse formula1
491 OUString sInput1(formula1, strlen(formula1), RTL_TEXTENCODING_UTF8);
492 auto pNode1 = SmParser5().ParseExpression(sInput1);
493 pNode1->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
494 SmNodeToTextVisitor(pNode1.get(), sOutput1);
496 // parse formula2
497 OUString sInput2(formula2, strlen(formula2), RTL_TEXTENCODING_UTF8);
498 auto pNode2 = SmParser5().ParseExpression(sInput2);
499 pNode2->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
500 SmNodeToTextVisitor(pNode2.get(), sOutput2);
502 CPPUNIT_ASSERT_EQUAL_MESSAGE(test_name, sOutput1, sOutput2);
504 // auxiliary test for Accept()
505 std::unique_ptr<MockVisitor> mv(new MockVisitor);
506 pNode1->Accept(mv.get());
507 pNode2->Accept(mv.get());
510 void Test::testBinomInBinHor()
512 OUString sInput, sExpected;
514 // set up a binom (table) node
515 sInput += "binom a b + c";
516 auto pTree = SmParser5().Parse(sInput);
517 pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
519 SmCursor aCursor(pTree.get(), xDocShRef.get());
520 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
522 // move forward (more than) enough places to be at the end
523 int i;
524 for (i = 0; i < 8; ++i)
525 aCursor.Move(pOutputDevice, MoveRight);
527 // tack +d on the end, which will put the binom into an SmBinHorNode
528 aCursor.InsertElement(PlusElement);
529 aCursor.InsertText("d");
531 sExpected += "{ { binom a b + c } + d }";
532 CPPUNIT_ASSERT_EQUAL_MESSAGE("Binom Node in BinHor Node", sExpected, xDocShRef->GetText());
535 void Test::testBinVerInUnary()
537 OUString sInput, sExpected;
539 // set up a unary operator with operand
540 sInput += "- 1";
541 auto pTree = SmParser5().Parse(sInput);
542 pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
544 SmCursor aCursor(pTree.get(), xDocShRef.get());
545 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
547 // move forward (more than) enough places to be at the end
548 int i;
549 for (i = 0; i < 3; ++i)
550 aCursor.Move(pOutputDevice, MoveRight);
552 // select the operand
553 aCursor.Move(pOutputDevice, MoveLeft, false);
554 // set up a fraction
555 aCursor.InsertFraction();
556 aCursor.Move(pOutputDevice, MoveDown);
557 aCursor.InsertText("2");
559 sExpected += "- { 1 over 2 }";
560 CPPUNIT_ASSERT_EQUAL_MESSAGE("Binary Vertical in Unary Operator", sExpected,
561 xDocShRef->GetText());
564 void Test::testBinHorInSubSup()
566 // set up a blank formula
567 auto pTree = SmParser5().Parse(OUString());
568 pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
570 SmCursor aCursor(pTree.get(), xDocShRef.get());
571 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
573 // Insert an RSup expression with a BinHor for the exponent
574 aCursor.InsertText("a");
575 aCursor.InsertSubSup(RSUP);
576 aCursor.InsertText("b");
577 aCursor.InsertElement(PlusElement);
578 aCursor.InsertText("c");
580 // Move to the end and add d to the expression
581 aCursor.Move(pOutputDevice, MoveRight);
582 aCursor.InsertElement(PlusElement);
583 aCursor.InsertText("d");
585 CPPUNIT_ASSERT_EQUAL_MESSAGE("BinHor in SubSup", OUString("{ a ^ { b + c } + d }"),
586 xDocShRef->GetText());
589 void Test::testUnaryInMixedNumberAsNumerator()
591 // set up a unary operator
592 auto pTree = SmParser5().Parse("- 1");
593 pTree->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
595 SmCursor aCursor(pTree.get(), xDocShRef.get());
596 ScopedVclPtrInstance<VirtualDevice> pOutputDevice;
598 // move forward (more than) enough places to be at the end
599 for (size_t i = 0; i < 3; ++i)
600 aCursor.Move(pOutputDevice, MoveRight);
602 // Select the whole Unary Horizontal Node
603 aCursor.Move(pOutputDevice, MoveLeft, false);
604 aCursor.Move(pOutputDevice, MoveLeft, false);
606 // Set up a fraction
607 aCursor.InsertFraction();
608 aCursor.Move(pOutputDevice, MoveDown);
609 aCursor.InsertText("2");
611 // Move left and turn this into a mixed number
612 // (bad form, but this could happen right?)
613 aCursor.Move(pOutputDevice, MoveLeft);
614 aCursor.Move(pOutputDevice, MoveLeft);
615 aCursor.InsertText("2");
617 // move forward (more than) enough places to be at the end
618 for (size_t i = 0; i < 8; ++i)
619 aCursor.Move(pOutputDevice, MoveRight);
621 // add 4 to the end
622 aCursor.InsertElement(PlusElement);
623 aCursor.InsertText("4");
625 CPPUNIT_ASSERT_EQUAL_MESSAGE("Unary in mixed number as Numerator",
626 OUString("{ 2 { - 1 over 2 } + 4 }"), xDocShRef->GetText());
629 void Test::testMiscEquivalent()
631 // fdo#55853
632 ParseAndCompare("2x", "2 x", "Number times variable");
633 ParseAndCompare("3x^2", "3 x^2", "Number times power");
635 // i#11752 and fdo#55853
636 ParseAndCompare("x_2n", "x_{2 n}", "Number times variable in subscript");
637 ParseAndCompare("x^2n", "x^{2 n}", "Number times variable in supscript");
639 // fdo#66081
640 ParseAndCompare("{x}", "x", "Variable in brace");
641 ParseAndCompare("{{x+{{y}}}}", "x+y", "Nested braces");
643 // check non-BMP Unicode char
644 ParseAndCompare("{\xf0\x9d\x91\x8e}", "\xf0\x9d\x91\x8e", "non-BMP variable in brace");
645 ParseAndCompare("{ \xf0\x9d\x91\x8e }", "\xf0\x9d\x91\x8e", "non-BMP variable in brace");
647 // tdf#88320
648 ParseAndCompare("A_1,B_2", "A_{1},B_2",
649 "Comma between a digit and non-digit delimits subscript");
651 //tdf#97164
652 ParseAndCompare("100 %", "100\"%\"", "Percent symbol at the end");
655 void Test::testParser()
657 OUString sOutput;
658 auto pNode = SmParser5().ParseExpression(u"{ \U0001D44E }"); // non-BMP Unicode
659 pNode->Prepare(xDocShRef->GetFormat(), *xDocShRef, 0);
660 SmNodeToTextVisitor(pNode.get(), sOutput);
661 CPPUNIT_ASSERT_EQUAL(OUString(u"\U0001D44E"), sOutput);
664 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
667 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */