コード例 #1
0
 public ATObject evalAndReturn(String input) {
   try {
     ATAbstractGrammar ptree = NATParser._INSTANCE_.base_parse(NATText.atValue(input));
     return ptree.meta_eval(ctx_);
   } catch (XParseError e) {
     fail("Parse error: " + e.getMessage());
   } catch (InterpreterException e) {
     e.printStackTrace();
     fail("Eval error: " + e.getMessage());
   }
   return null;
 }
コード例 #2
0
 public void evalAndTestException(String input, Class interpreterExceptionClass) {
   try {
     ATAbstractGrammar ptree = NATParser._INSTANCE_.base_parse(NATText.atValue(input));
     ptree.meta_eval(ctx_);
     fail(
         "Expected an exception of type "
             + Evaluator.getSimpleName(
                 interpreterExceptionClass)); // test should throw an exception
   } catch (InterpreterException ex) {
     if (!interpreterExceptionClass.isInstance(ex)) {
       ex.printStackTrace();
       fail("Unexpected exception: " + ex.getMessage());
     }
   }
 }
コード例 #3
0
  /**
   * Loads and evaluates the content of a code snippet file and returns the resulting AmbientTalk
   * ATObject. Given a class Foo and the name "snippet", the code file consulted is the file named
   * "Foo-snippet" which should be located in the same directory as the Foo.class file.
   */
  public static final ATObject evalSnippet(Class forTestClass, String name, ATContext inContext)
      throws InterpreterException {
    // Backport from JDK 1.4 to 1.3 -> no more URI, construct file directly from URL instead
    // Open a stream to the file using the URL
    try {
      String fileName = Evaluator.getSimpleName(forTestClass) + "-" + name;
      InputStream in = forTestClass.getResource(fileName).openStream();
      BufferedReader dis = new BufferedReader(new InputStreamReader(in));
      StringBuffer fBuf = new StringBuffer();
      String line;

      while ((line = dis.readLine()) != null) {
        fBuf.append(line + "\n");
      }
      in.close();

      // parse and evaluate the code in the proper context and return its result
      ATAbstractGrammar source = NATParser.parse(fileName, fBuf.toString());
      return source.meta_eval(inContext);
    } catch (IOException e) {
      fail(e.getMessage());
      return Evaluator.getNil();
    }

    /*try {
    	File inFile = new File(forTestClass.getResource(Evaluator.getSimpleName(forTestClass) + "-" + name));
              // load the code from the file
    	String code = Evaluator.loadContentOfFile(inFile);

        // parse and evaluate the code in the proper context and return its result
    	ATAbstractGrammar source = NATParser.parse(inFile.getName(), code);
    	return source.meta_eval(inContext);
    } catch (IOException e) {
    	throw new XIOProblem(e);
    } catch (URISyntaxException e) {
    	fail(e.getMessage());
    	return NATNil._INSTANCE_;
    }*/
  }