1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.millscript;
22
23 import org.millscript.commons.vfs.VFS;
24 import org.millscript.commons.vfs.VFolder;
25 import org.millscript.millscript.conf.ApiConf;
26 import org.millscript.millscript.expr.Expr;
27 import org.millscript.millscript.syntax.Parser;
28 import org.millscript.millscript.syntax.ParserImpl;
29 import org.millscript.millscript.vm.Engine;
30
31 import java.io.File;
32 import java.io.StringReader;
33
34 import junit.framework.TestCase;
35
36 /**
37 * Abstract base class for test cases.
38 *
39 * @author <a href="moya@users.sourceforge.net">Kevin Rogers</a>
40 */
41 public abstract class AbstractTestCase extends TestCase {
42
43 /**
44 * MillScript engine used for testing.
45 * This is required when we try to perform any compiled
46 * actions.
47 */
48 Engine engine;
49
50 /**
51 * The root of the source code
52 */
53 VFolder sourceDir = new VFS().resolveAsFolder(
54 new File( System.getProperty( "basedir" ) ).toURI()
55 ).getVFolder( "src" );
56
57 /**
58 * Constructor.
59 */
60 public AbstractTestCase( String testName ) {
61 super( testName );
62 System.setProperty( "millscript.dir", sourceDir.getURI().getPath() );
63 }
64
65 /**
66 * Returns a VFolder object for the specified resource
67 *
68 * @param path the string containing the relative path of a resource
69 * @return a virtual folder representing the resource
70 */
71 public VFolder getResource( String path ) {
72 return sourceDir.getVFolder( path );
73 }
74
75 /**
76 * Initialize the test fixture by creating a new Machine for the test. This
77 * should reduce interference from previous tests.
78 */
79 @Override
80 public void setUp() {
81 engine = new ApiConf().getStandardEngine();
82 }
83
84 /**
85 * Act on the supplied expression, returning a single object from
86 * the machine stack. It is assumed that any supplied expression
87 * will generate one result.
88 *
89 * @param e The expression to act upon.
90 * @return The last object put on the machine stack.
91 */
92 public Object actExpr( Expr e ) {
93
94 e.compileIt().act( engine.getMachine() );
95
96 return engine.getMachine().popObject();
97
98 }
99
100 /**
101 * Parse the supplied string for and then act on it, returning
102 * a single object from the machine stack. It is assumed that
103 * any supplied expression will generate one result.
104 *
105 * @param expression The expression to parse and act upon
106 * @return The last object put on the machine stack.
107 */
108 public Object actParseExpr( String expression ) {
109
110 Parser p = new ParserImpl(
111 "test",
112 new StringReader( expression ),
113 false,
114 engine.getConfig()
115 );
116
117 return actExpr( p.readExpr() );
118
119 }
120
121 }