PageRenderTime 24ms CodeModel.GetById 9ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2-build270707/test/edu/vub/at/AmbientTalkTest.java

http://ambienttalk.googlecode.com/
Java | 184 lines | 142 code | 20 blank | 22 comment | 9 complexity | ed2c38e23c93c1f22a56aed62fc3c6d3 MD5 | raw file
  1package edu.vub.at;
  2
  3import edu.vub.at.actors.eventloops.Callable;
  4import edu.vub.at.actors.natives.ELActor;
  5import edu.vub.at.eval.Evaluator;
  6import edu.vub.at.exceptions.InterpreterException;
  7import edu.vub.at.exceptions.XParseError;
  8import edu.vub.at.objects.ATAbstractGrammar;
  9import edu.vub.at.objects.ATContext;
 10import edu.vub.at.objects.ATObject;
 11import edu.vub.at.objects.natives.NATContext;
 12import edu.vub.at.objects.natives.OBJNil;
 13import edu.vub.at.objects.natives.NATObject;
 14import edu.vub.at.objects.natives.NATText;
 15import edu.vub.at.parser.NATParser;
 16
 17import java.io.BufferedReader;
 18import java.io.IOException;
 19import java.io.InputStream;
 20import java.io.InputStreamReader;
 21
 22import junit.framework.TestCase;
 23
 24public abstract class AmbientTalkTest extends TestCase {
 25
 26	protected final ATContext ctx_;
 27	
 28	public AmbientTalkTest() {	
 29		ATObject supr = new NATObject();
 30		ATObject self =
 31			new NATObject(supr, NATObject._SHARES_A_); // self has root as lex parent and supr as dyn parent
 32		ATObject scope = new NATObject(self); // scope has no dyn parent and is nested within self
 33		ctx_ = new NATContext(scope, self);
 34	}
 35	
 36	/**
 37	 * Loads and evaluates the content of a code snippet file and returns the resulting AmbientTalk ATObject.
 38	 * Given a class Foo and the name "snippet", the code file consulted is the file named "Foo-snippet" which
 39	 * should be located in the same directory as the Foo.class file.
 40	 */
 41	public static final ATObject evalSnippet(Class forTestClass, String name, ATContext inContext) throws InterpreterException {
 42		// Backport from JDK 1.4 to 1.3 -> no more URI, construct file directly from URL instead
 43	    // Open a stream to the file using the URL
 44	    try {
 45	    	String fileName = Evaluator.getSimpleName(forTestClass) + "-" + name;
 46	        InputStream in = forTestClass.getResource(fileName).openStream();
 47	        BufferedReader dis = new BufferedReader(new InputStreamReader(in));
 48	        StringBuffer fBuf = new StringBuffer();
 49	        String line;
 50
 51	        while ( (line = dis.readLine()) != null) {
 52	          fBuf.append (line + "\n");
 53	        }
 54	        in.close();
 55	      
 56		    // parse and evaluate the code in the proper context and return its result
 57			ATAbstractGrammar source = NATParser.parse(fileName, fBuf.toString());
 58			return source.meta_eval(inContext);
 59	    }
 60	    catch (IOException e) {
 61	       fail(e.getMessage());
 62	       return OBJNil._INSTANCE_;
 63	    }
 64		
 65		/*try {
 66			File inFile = new File(forTestClass.getResource(Evaluator.getSimpleName(forTestClass) + "-" + name));
 67            // load the code from the file
 68			String code = Evaluator.loadContentOfFile(inFile);
 69		    
 70		    // parse and evaluate the code in the proper context and return its result
 71			ATAbstractGrammar source = NATParser.parse(inFile.getName(), code);
 72			return source.meta_eval(inContext);
 73		} catch (IOException e) {
 74			throw new XIOProblem(e);
 75		} catch (URISyntaxException e) {
 76			fail(e.getMessage());
 77			return NATNil._INSTANCE_;
 78		}*/
 79	}
 80	
 81	public ATObject evalAndReturn(String input) {
 82        try {
 83			ATAbstractGrammar ptree = 
 84				NATParser._INSTANCE_.base_parse(NATText.atValue(input));
 85			return ptree.meta_eval(ctx_);
 86		} catch (XParseError e) {
 87			fail("Parse error: "+e.getMessage());
 88		} catch (InterpreterException e) {
 89			e.printStackTrace();
 90			fail("Eval error: "+e.getMessage());
 91		}
 92		return null;
 93	}
 94	
 95	public void evalAndTestException(String input, Class interpreterExceptionClass) {
 96        try {
 97			ATAbstractGrammar ptree = 
 98				NATParser._INSTANCE_.base_parse(NATText.atValue(input));
 99			ptree.meta_eval(ctx_);
100			fail("Expected an exception of type " + Evaluator.getSimpleName(interpreterExceptionClass)); // test should throw an exception
101		} catch (InterpreterException ex) {
102			if (!interpreterExceptionClass.isInstance(ex)) {
103				ex.printStackTrace();
104				fail("Unexpected exception: "+ex.getMessage());
105			}
106		}
107	}
108	
109	public void evalAndThrowException(String input, Class interpreterExceptionClass) throws InterpreterException {
110        try {
111			ATAbstractGrammar ptree = 
112				NATParser._INSTANCE_.base_parse(NATText.atValue(input));
113			ptree.meta_eval(ctx_);
114			fail("Expected an exception of type " + Evaluator.getSimpleName(interpreterExceptionClass)); // test should throw an exception
115		} catch (InterpreterException ex) {
116			if (!interpreterExceptionClass.isInstance(ex)) {
117				ex.printStackTrace();
118				fail("Unexpected exception: "+ex.getMessage());
119			} else {
120				throw ex;
121			}
122		}
123	}
124	
125	public ATObject evalInActor(String input) {
126        try {
127			ATAbstractGrammar ptree = 
128				NATParser._INSTANCE_.base_parse(NATText.atValue(input));
129			return ELActor.currentActor().sync_event_eval(ptree);
130		} catch (XParseError e) {
131			fail("Parse error: "+e.getMessage());
132		} catch (InterpreterException e) {
133			e.printStackTrace();
134			fail("Eval error: "+e.getMessage());
135		}
136		return null;
137	}
138	
139	public static abstract class Actorscript implements Callable {
140		public Object call(Object arg) throws Exception {
141			test();
142			return null;
143		}
144		public abstract void test() throws Exception;
145	}
146	
147	public void actorTest(Actorscript test) throws Exception {
148        try {
149        	ELActor.currentActor().sync_event_performTest(test);
150		} catch (XParseError e) {
151			fail("Parse error: "+e.getMessage());
152		} catch (InterpreterException e) {
153			e.printStackTrace();
154			fail("Eval error: "+e.getMessage());
155		}
156	}
157	
158	public void evalAndCompareTo(String input, ATObject output) {
159		ATObject result = evalAndReturn(input);
160		if (result != null) {
161			assertEquals(output, result);
162		}
163	}
164	
165	public void evalAndCompareTo(String input, String output) {
166		try {
167			ATObject result = evalAndReturn(input);
168			if (result != null) {
169				assertEquals(output, result.meta_print().javaValue);
170			}
171		} catch (InterpreterException e) {
172			fail(e.getMessage());
173		}
174	}
175	
176	public void printedEquals(ATObject input, String expected) {
177		try {
178			assertEquals(expected, input.meta_print().javaValue);
179		} catch (InterpreterException e) {
180			fail(e.getMessage());
181		}
182	}
183	
184}