1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
89 assertEquals(
90 actParseExpr(
91 "9 / 2"
92 ),
93 new Integer( 4 )
94 );
95 engine.getMachine().reset();
96 }
97
98 public void testMinus() {
99
100 assertEquals(
101 actParseExpr(
102 "-7"
103 ),
104 new Integer( -7 )
105 );
106 engine.getMachine().reset();
107
108 assertEquals(
109 actParseExpr(
110 "2 - 5"
111 ),
112 new Integer( -3 )
113 );
114 engine.getMachine().reset();
115 }
116
117 public void testMultiplication() {
118
119 assertEquals(
120 actParseExpr(
121 "4 * 6"
122 ),
123 new Integer( 24 )
124 );
125 engine.getMachine().reset();
126 }
127
128 public void testPercent() {
129
130 assertEquals(
131 actParseExpr(
132 "100%"
133 ),
134 new Integer( 1 )
135 );
136 engine.getMachine().reset();
137 }
138
139 public void testPlus() {
140
141 assertEquals(
142 actParseExpr(
143 "+7"
144 ),
145 new Integer( 7 )
146 );
147 engine.getMachine().reset();
148
149 assertEquals(
150 actParseExpr(
151 "2 + 5"
152 ),
153 new Integer( 7 )
154 );
155 engine.getMachine().reset();
156 }
157
158 }