1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
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          // We want to load packages from here..
87          engine.addInventory( getResource( "test/millscript" ) );
88  
89          // Try to import the test packages
90          bug53Package = engine.getPackage( "bug53" );
91          bug53aPackage = engine.getPackage( "bug53a" );
92  
93          // Check the test packages were imported
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         // Checks for package bug53
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         // Checks for package bug53a
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         // Checks for package bug53
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         // Checks for package bug53a
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         // Checks for package bug53
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             // Yippee! This actually means it worked...
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             // Yippee! This actually means it worked...
255         } catch ( Exception ex ) {
256             System.out.println( ex.getMessage() );
257             System.out.println( ex );
258         }
259 
260         // Checks for package bug53a
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             // Yippee! This actually means it worked...
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         // Checks for package bug53a
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             // Change to the bug53 package, so that we can test bug53::foo.
298             state.changePackage( bug53Package );
299 
300             // Get the value of bug53::foo
301             Object result = state.compileExprToValue(
302                 new NameExpr( "foo" )
303             );
304 
305             // Check we got a String, which is equal to "ok"
306             assertTrue( result instanceof String );
307             assertEquals( "ok", (String)result );
308 
309             // Change back to the bug53a package, so we can continue testing.
310             state.changePackage( bug53aPackage );
311 
312             // Now the for loop test
313             state.compileExpr(
314                 new ApplyExpr(
315                     new NameExpr( "assign_second" ),
316                     new SkipExpr()
317                 )
318             );
319 
320             // Change to the bug53 package, so that we can test bug53::foo.
321             state.changePackage( bug53Package );
322 
323             // Get the value of bug53::foo
324             result = state.compileExprToValue(
325                 new NameExpr( "foo" )
326             );
327 
328             // Check we got a String, which is equal to "ok"
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 }