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

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