/libformula-1.1.3/test/org/pentaho/reporting/libraries/formula/FormulaParsingTest.java
Java | 104 lines | 72 code | 11 blank | 21 comment | 0 complexity | 759dbfe805140d91ce779e75264b4b36 MD5 | raw file
Possible License(s): LGPL-2.1
1/*
2 * This program is free software; you can redistribute it and/or modify it under the
3 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
4 * Foundation.
5 *
6 * You should have received a copy of the GNU Lesser General Public License along with this
7 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
8 * or from the Free Software Foundation, Inc.,
9 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU Lesser General Public License for more details.
14 *
15 * Copyright (c) 2009 Pentaho Corporation. All rights reserved.
16 */
17
18package org.pentaho.reporting.libraries.formula;
19
20import junit.framework.TestCase;
21import org.pentaho.reporting.libraries.formula.parser.ParseException;
22import org.pentaho.reporting.libraries.formula.lvalues.Term;
23
24/**
25 * Todo: Document Me
26 *
27 * @author Thomas Morgner
28 */
29public class FormulaParsingTest extends TestCase
30{
31 public FormulaParsingTest()
32 {
33 super();
34 }
35
36 public FormulaParsingTest(final String s)
37 {
38 super(s);
39 }
40
41 protected void setUp() throws Exception
42 {
43 LibFormulaBoot.getInstance().start();
44 }
45
46 public void testTermOperands() throws ParseException, EvaluationException
47 {
48 final Formula formula = new Formula("\"a\" & \"b\" & \"c\"");
49 formula.initialize(new DefaultFormulaContext());
50 Term term = (Term) formula.getRootReference();
51 term.getOperands();
52 }
53
54 public void testParseWithLineBreaks() throws ParseException, EvaluationException
55 {
56 final Formula formula = new Formula("\"aaa\" \n&\n \"bb\" & \"\n\" & \"\"");
57 formula.initialize(new DefaultFormulaContext());
58 final Object o = formula.evaluate();
59 assertEquals("Formula value", "aaabb\n", o);
60 }
61
62 public void testParse() throws ParseException, EvaluationException
63 {
64 final Formula formula = new Formula("MID(UPPER([name] & \n\r" +
65 " \" \" \n\r" +
66 " & [firstname]);5;10)");
67 final DefaultFormulaContext context = new DefaultFormulaContext();
68 context.defineReference("name", "name");
69 context.defineReference("firstname", "firstname");
70 formula.initialize(context);
71 final Object o = formula.evaluate();
72 assertEquals("Formula value", " FIRSTNAME", o);
73 }
74
75 public void testEscapes() throws Exception
76 {
77 final Formula formula = new Formula("T(\"\\\") = \"A\"");
78 final DefaultFormulaContext context = new DefaultFormulaContext();
79 context.defineReference("path", "x");
80 formula.initialize(context);
81 final Object o = formula.evaluate();
82 assertEquals("Formula value", Boolean.FALSE, o);
83 }
84
85 public void testQuotedReference() throws Exception
86 {
87 final Formula formula = new Formula("T([\"\\\\\"]) = \"A\"");
88 final DefaultFormulaContext context = new DefaultFormulaContext();
89 context.defineReference("path", "x");
90 formula.initialize(context);
91 final Object o = formula.evaluate();
92 assertEquals("Formula value", Boolean.FALSE, o);
93 }
94
95 public void testQuotedReference2() throws Exception
96 {
97 final Formula formula = new Formula("T([\"[x]\"]) = \"x\"");
98 final DefaultFormulaContext context = new DefaultFormulaContext();
99 context.defineReference("[x]", "x");
100 formula.initialize(context);
101 final Object o = formula.evaluate();
102 assertEquals("Formula value", Boolean.TRUE, o);
103 }
104}