Esempio n. 1
0
 private static boolean doEcho(PolyglotEngine vm) {
   PolyglotEngine.Value echoValue = vm.eval(GET_ECHO);
   Object echo = echoValue.get();
   if (echo instanceof TruffleObject) {
     RLogicalVector echoVec = echoValue.as(RLogicalVector.class);
     return RRuntime.fromLogical(echoVec.getDataAt(0));
   } else if (echo instanceof Byte) {
     return RRuntime.fromLogical((Byte) echo);
   } else {
     throw RInternalError.shouldNotReachHere();
   }
 }
  /**
   * Executes the passed in test case. Instrumentation is added according to the name of the file as
   * explained in {@link #createTests(Class)}. Note that this code is not generalizable.
   */
  @Override
  protected void runChild(InstrumentTestCase testCase, RunNotifier notifier) {
    // TODO Current tests are hard-coded, automate this eventually
    notifier.fireTestStarted(testCase.name);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(out);

    PolyglotEngine vm = null;
    try {
      // We use the name of the file to determine what visitor to attach to it.
      if (testCase.baseName.endsWith(ASSIGNMENT_VALUE_SUFFIX)) {
        // Set up the execution context for Simple and register our two listeners
        vm =
            PolyglotEngine.newBuilder()
                .setIn(new ByteArrayInputStream(testCase.testInput.getBytes("UTF-8")))
                .setOut(out)
                .build();

        final String src = readAllLines(testCase.path);
        vm.eval(Source.fromText(src, testCase.path.toString()).withMimeType("application/x-sl"));

        PolyglotEngine.Value main = vm.findGlobalSymbol("main");
        main.execute();
      } else {
        notifier.fireTestFailure(
            new Failure(
                testCase.name, new UnsupportedOperationException("No instrumentation found.")));
      }
      ps.flush();
      String actualOutput = new String(out.toByteArray());
      Assert.assertEquals(testCase.expectedOutput, actualOutput);
    } catch (Throwable ex) {
      notifier.fireTestFailure(new Failure(testCase.name, ex));
    } finally {
      if (vm != null) {
        vm.dispose();
      }
      notifier.fireTestFinished(testCase.name);
    }
  }