/interpreter/tags/at2dist091109/test/edu/vub/at/AmbientTalkTest.java

http://ambienttalk.googlecode.com/ · Java · 183 lines · 141 code · 20 blank · 22 comment · 9 complexity · dbcc0792bfb425484979fec7b2a0f999 MD5 · raw file

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