PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/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. package org.pentaho.reporting.libraries.formula;
  18. import junit.framework.TestCase;
  19. import org.pentaho.reporting.libraries.formula.parser.ParseException;
  20. import org.pentaho.reporting.libraries.formula.lvalues.Term;
  21. /**
  22. * Todo: Document Me
  23. *
  24. * @author Thomas Morgner
  25. */
  26. public class FormulaParsingTest extends TestCase
  27. {
  28. public FormulaParsingTest()
  29. {
  30. super();
  31. }
  32. public FormulaParsingTest(final String s)
  33. {
  34. super(s);
  35. }
  36. protected void setUp() throws Exception
  37. {
  38. LibFormulaBoot.getInstance().start();
  39. }
  40. public void testTermOperands() throws ParseException, EvaluationException
  41. {
  42. final Formula formula = new Formula("\"a\" & \"b\" & \"c\"");
  43. formula.initialize(new DefaultFormulaContext());
  44. Term term = (Term) formula.getRootReference();
  45. term.getOperands();
  46. }
  47. public void testParseWithLineBreaks() throws ParseException, EvaluationException
  48. {
  49. final Formula formula = new Formula("\"aaa\" \n&\n \"bb\" & \"\n\" & \"\"");
  50. formula.initialize(new DefaultFormulaContext());
  51. final Object o = formula.evaluate();
  52. assertEquals("Formula value", "aaabb\n", o);
  53. }
  54. public void testParse() throws ParseException, EvaluationException
  55. {
  56. final Formula formula = new Formula("MID(UPPER([name] & \n\r" +
  57. " \" \" \n\r" +
  58. " & [firstname]);5;10)");
  59. final DefaultFormulaContext context = new DefaultFormulaContext();
  60. context.defineReference("name", "name");
  61. context.defineReference("firstname", "firstname");
  62. formula.initialize(context);
  63. final Object o = formula.evaluate();
  64. assertEquals("Formula value", " FIRSTNAME", o);
  65. }
  66. public void testEscapes() throws Exception
  67. {
  68. final Formula formula = new Formula("T(\"\\\") = \"A\"");
  69. final DefaultFormulaContext context = new DefaultFormulaContext();
  70. context.defineReference("path", "x");
  71. formula.initialize(context);
  72. final Object o = formula.evaluate();
  73. assertEquals("Formula value", Boolean.FALSE, o);
  74. }
  75. public void testQuotedReference() 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. public void testQuotedReference2() throws Exception
  85. {
  86. final Formula formula = new Formula("T([\"[x]\"]) = \"x\"");
  87. final DefaultFormulaContext context = new DefaultFormulaContext();
  88. context.defineReference("[x]", "x");
  89. formula.initialize(context);
  90. final Object o = formula.evaluate();
  91. assertEquals("Formula value", Boolean.TRUE, o);
  92. }
  93. }