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