1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Excel: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2006 Open World Ltd, Kevin Rogers
4   //
5   // This file is part of MillScript-Excel.
6   //
7   // MillScript-Excel is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript-Excel is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript-Excel; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.office.spreadsheet.formula;
22  
23  import org.millscript.millscript.conf.ApiConf;
24  import org.millscript.millscript.expr.Expr;
25  import org.millscript.millscript.syntax.Parser;
26  import org.millscript.millscript.vm.Engine;
27  
28  import java.io.StringReader;
29  
30  import junit.framework.TestCase;
31  
32  
33  public class FormulaParserImplTest extends TestCase {
34  
35      /**
36       * MillScript engine used for testing.
37       * This is required when we try to perform any compiled
38       * actions.
39       */
40      Engine engine;
41  
42      /**
43       * Act on the supplied expression, returning a single object from
44       * the machine stack. It is assumed that any supplied expression
45       * will generate one result.
46       *
47       * @param   e   The expression to act upon.
48       * @return  The last object put on the machine stack.
49       */
50      public Object actExpr( final Expr e ) {
51  
52          e.compileIt().act( engine.getMachine() );
53  
54          return engine.getMachine().popObject();
55  
56      }
57  
58      /**
59       * Parse the supplied string for and then act on it, returning
60       * a single object from the machine stack. It is assumed that
61       * any supplied expression will generate one result.
62       *
63       * @param   expression  The expression to parse and act upon
64       * @return  The last object put on the machine stack.
65       */
66      public Object actParseExpr( final String expression ) {
67  
68          Parser p = new FormulaParserImpl(
69              "test",
70              new StringReader( expression ),
71              engine.getConfig()
72          );
73  
74          return actExpr( p.readExpr() );
75  
76      }
77  
78      /**
79       * Initialize the test fixture by creating a new Machine for the test. This
80       * should reduce interference from previous tests.
81       */
82      @Override
83      public void setUp() {
84          engine = new ApiConf().getStandardEngine();
85      }
86  
87      public void testDiv() {
88          // And now a simple binary division test
89          assertEquals(
90              actParseExpr(
91                  "9 / 2"
92              ),
93              new Integer( 4 )
94          );
95          engine.getMachine().reset();
96      }
97  
98      public void testMinus() {
99          // Do a simple unary minus test
100         assertEquals(
101             actParseExpr(
102                 "-7"
103             ),
104             new Integer( -7 )
105         );
106         engine.getMachine().reset();
107         // And now a simple binary minus test
108         assertEquals(
109             actParseExpr(
110                 "2 - 5"
111             ),
112             new Integer( -3 )
113         );
114         engine.getMachine().reset();
115     }
116 
117     public void testMultiplication() {
118         // And now a simple binary multiplication test
119         assertEquals(
120             actParseExpr(
121                 "4 * 6"
122             ),
123             new Integer( 24 )
124         );
125         engine.getMachine().reset();
126     }
127 
128     public void testPercent() {
129         // Do a simple percent test
130         assertEquals(
131             actParseExpr(
132                 "100%"
133             ),
134             new Integer( 1 )
135         );
136         engine.getMachine().reset();
137     }
138 
139     public void testPlus() {
140         // Do a simple unary plus test
141         assertEquals(
142             actParseExpr(
143                 "+7"
144             ),
145             new Integer( 7 )
146         );
147         engine.getMachine().reset();
148         // And now a simple binary addition test
149         assertEquals(
150             actParseExpr(
151                 "2 + 5"
152             ),
153             new Integer( 7 )
154         );
155         engine.getMachine().reset();
156     }
157 
158 }