1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript;
23
24 import org.millscript.commons.alert.EscapeException;
25 import org.millscript.millscript.expr.ApplyExpr;
26 import org.millscript.millscript.expr.ConstantExpr;
27 import org.millscript.millscript.expr.NameExpr;
28 import org.millscript.millscript.expr.SkipExpr;
29 import org.millscript.millscript.vm.CompilerState;
30 import org.millscript.millscript.vm.Package;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 /**
36 * Unit test for Millscript autoloading system. This actually allows us to test
37 * a large portion of the Millscript sub system. We cannot check the results of
38 * autoloading if large portions of the language are not functioning.
39 *
40 * @author <a href="mailto:moya@users.sourceforge.net">Kevin Rogers</a>
41 */
42 public class NameExprTest extends AbstractTestCase {
43
44 /**
45 * bug53 Package.
46 */
47 private Package bug53Package;
48
49 /**
50 * bug53a Package.
51 */
52 private Package bug53aPackage;
53
54 /**
55 * String used as the argument for millscript test functions
56 */
57 private static final ConstantExpr arg = new ConstantExpr( "argument" );
58
59 /**
60 * Create the test case
61 *
62 * @param testName name of the test case
63 */
64 public NameExprTest( String testName ) {
65 super( testName );
66 }
67
68 /**
69 * @return the suite of tests being tested
70 */
71 public static Test suite() {
72 return new TestSuite( NameExprTest.class );
73 }
74
75 /**
76 * Initialize the test fixture. In this instance we require a machine with a
77 * standard environment, from which we get the "bug53" and "bug53a" package
78 * environments(i.e. we import it). We can then proceed with testing that
79 * NameExpr, or at least NameExpr.shareIdent, works properly.
80 */
81 @Override
82 public void setUp() {
83
84 super.setUp();
85
86
87 engine.addInventory( getResource( "test/millscript" ) );
88
89
90 bug53Package = engine.getPackage( "bug53" );
91 bug53aPackage = engine.getPackage( "bug53a" );
92
93
94 assertNotNull( bug53Package );
95 assertNotNull( bug53aPackage );
96
97 }
98
99 /**
100 * Clean up the test fixture by reseting the millscript machine.
101 */
102 @Override
103 public void tearDown() {
104 engine.getMachine().reset();
105 }
106
107 /**
108 * Check that NameExpr is working properly by executing some statements from
109 * both packages that are expected to return a true result.
110 */
111 public void testTrueStatements() {
112
113
114 try {
115 CompilerState state = bug53Package.getCompilerState();
116
117 Object result = state.compileExprToValue(
118 new ApplyExpr(
119 new NameExpr( "true_first" ),
120 arg
121 )
122 );
123
124 assertTrue( result instanceof Boolean );
125 assertEquals( ((Boolean)result).booleanValue(), true );
126
127 result = state.compileExprToValue(
128 new ApplyExpr(
129 new NameExpr( "true_second" ),
130 arg
131 )
132 );
133
134 assertTrue( result instanceof Boolean );
135 assertEquals( ((Boolean)result).booleanValue(), true );
136
137 } catch ( Exception ex ) {
138 System.out.println( ex.getMessage() );
139 System.out.println( ex );
140 }
141
142
143 try {
144 CompilerState state = bug53aPackage.getCompilerState();
145
146 Object result = state.compileExprToValue(
147 new ApplyExpr(
148 new NameExpr( "true_first" ),
149 arg
150 )
151 );
152
153 assertTrue( result instanceof Boolean );
154 assertEquals( ((Boolean)result).booleanValue(), true );
155
156 result = state.compileExprToValue(
157 new ApplyExpr(
158 new NameExpr( "true_second" ),
159 arg
160 )
161 );
162
163 assertTrue( result instanceof Boolean );
164 assertEquals( ((Boolean)result).booleanValue(), true );
165
166 } catch ( Exception ex ) {
167 System.out.println( ex.getMessage() );
168 System.out.println( ex );
169 }
170
171 }
172
173 /**
174 * Check that NameExpr is working properly by executing some statements from
175 * both packages that are expected to return a false result.
176 */
177 public void testFalseStatements() {
178
179
180 try {
181 CompilerState state = bug53Package.getCompilerState();
182
183 Object result = state.compileExprToValue(
184 new ApplyExpr(
185 new NameExpr( "false_first" ),
186 arg
187 )
188 );
189
190 assertTrue( result instanceof Boolean );
191 assertEquals( ((Boolean)result).booleanValue(), false );
192
193 } catch ( Exception ex ) {
194 System.out.println( ex.getMessage() );
195 System.out.println( ex );
196 }
197
198
199 try {
200 CompilerState state = bug53aPackage.getCompilerState();
201
202 Object result = state.compileExprToValue(
203 new ApplyExpr(
204 new NameExpr( "false_first" ),
205 arg
206 )
207 );
208
209 assertTrue( result instanceof Boolean );
210 assertEquals( ((Boolean)result).booleanValue(), false );
211
212 } catch ( Exception ex ) {
213 System.out.println( ex.getMessage() );
214 System.out.println( ex );
215 }
216
217 }
218
219 /**
220 * Check that NameExpr is working properly by executing some statements from
221 * both packages that are expected to cause the compiler to fail.
222 */
223 public void testCompilerFailures() {
224
225
226 try {
227 CompilerState state = bug53Package.getCompilerState();
228
229 state.compileExprToValue(
230 new ApplyExpr(
231 new NameExpr( "compiler_failure_first" ),
232 arg
233 )
234 );
235
236 } catch ( EscapeException ex ) {
237
238 } catch ( Exception ex ) {
239 System.out.println( ex.getMessage() );
240 System.out.println( ex );
241 }
242
243 try {
244 CompilerState state = bug53Package.getCompilerState();
245
246 state.compileExprToValue(
247 new ApplyExpr(
248 new NameExpr( "compiler_failure_second" ),
249 arg
250 )
251 );
252
253 } catch ( EscapeException ex ) {
254
255 } catch ( Exception ex ) {
256 System.out.println( ex.getMessage() );
257 System.out.println( ex );
258 }
259
260
261 try {
262 CompilerState state = bug53aPackage.getCompilerState();
263
264 state.compileExprToValue(
265 new ApplyExpr(
266 new NameExpr( "compiler_failure_first" ),
267 arg
268 )
269 );
270
271 } catch ( EscapeException ex ) {
272
273 } catch ( Exception ex ) {
274 System.out.println( ex.getMessage() );
275 System.out.println( ex );
276 }
277
278 }
279
280 /**
281 * Check that NameExpr is working properly by executing some statements in
282 * one package that assign to a variable in another package.
283 */
284 public void testAssignment() {
285
286
287 try {
288 CompilerState state = bug53aPackage.getCompilerState();
289
290 state.compileExpr(
291 new ApplyExpr(
292 new NameExpr( "assign_first" ),
293 arg
294 )
295 );
296
297
298 state.changePackage( bug53Package );
299
300
301 Object result = state.compileExprToValue(
302 new NameExpr( "foo" )
303 );
304
305
306 assertTrue( result instanceof String );
307 assertEquals( "ok", (String)result );
308
309
310 state.changePackage( bug53aPackage );
311
312
313 state.compileExpr(
314 new ApplyExpr(
315 new NameExpr( "assign_second" ),
316 new SkipExpr()
317 )
318 );
319
320
321 state.changePackage( bug53Package );
322
323
324 result = state.compileExprToValue(
325 new NameExpr( "foo" )
326 );
327
328
329 assertTrue( result instanceof String );
330 assertEquals( "worked", (String)result );
331
332 } catch ( Exception ex ) {
333 System.out.println( ex.getMessage() );
334 System.out.println( ex );
335 }
336
337 }
338
339 }