Esempio n. 1
1
  @Parameters(name = "js={0}, opt={3}, strict={4}")
  public static Collection<Object[]> test262SuiteValues() throws IOException {
    List<Object[]> result = new ArrayList<Object[]>();
    List<File> tests = getTestFiles();
    for (File jsTest : tests) {
      String jsFileStr = (String) SourceReader.readFileOrUrl(jsTest.getPath(), true, "UTF-8");
      List<String> harnessFiles = new ArrayList<String>();
      harnessFiles.add("sta.js");
      harnessFiles.add("assert.js");

      Map header =
          (Map)
              YAML.load(
                  jsFileStr.substring(
                      jsFileStr.indexOf("/*---") + 5, jsFileStr.lastIndexOf("---*/")));
      if (header.containsKey("includes")) {
        harnessFiles.addAll((Collection) header.get("includes"));
      }

      EcmaErrorType errorType = EcmaErrorType._valueOf((String) header.get("negative"));
      List<String> flags =
          header.containsKey("flags") ? (List<String>) header.get("flags") : Collections.EMPTY_LIST;
      for (int optLevel : OPT_LEVELS) {
        if (!flags.contains("onlyStrict")) {
          result.add(
              new Object[] {jsTest.getPath(), jsFileStr, harnessFiles, optLevel, false, errorType});
        }
        if (!flags.contains("noStrict")) {
          result.add(
              new Object[] {jsTest.getPath(), jsFileStr, harnessFiles, optLevel, true, errorType});
        }
      }
    }
    return result;
  }
Esempio n. 2
0
  private Object executeRhinoScript() {
    Context cx = Context.enter();

    try {
      cx.setOptimizationLevel(optLevel);

      Scriptable scope = cx.initStandardObjects();
      for (String harnessFile : harnessFiles) {
        if (!HARNESS_SCRIPT_CACHE.get(optLevel).containsKey(harnessFile)) {
          HARNESS_SCRIPT_CACHE
              .get(optLevel)
              .put(
                  harnessFile,
                  cx.compileReader(
                      new FileReader("test262/harness/" + harnessFile),
                      "test262/harness/" + harnessFile,
                      1,
                      null));
        }
        HARNESS_SCRIPT_CACHE.get(optLevel).get(harnessFile).exec(cx, scope);
      }

      String str = jsFileStr;
      if (useStrict) { // taken from test262.py
        str = "\"use strict\";\nvar strict_mode = true;\n" + jsFileStr;
      }

      Object result = cx.evaluateString(scope, str, jsFilePath.replaceAll("\\\\", "/"), 1, null);

      if (errorType != EcmaErrorType.NONE) {
        fail(String.format("failed negative test. expected error: %s", errorType));
        return null;
      }

      return result;
    } catch (RhinoException ex) {
      if (errorType == EcmaErrorType.NONE || !(ex instanceof EcmaError)) {
        fail(String.format("%s%n%s", ex.getMessage(), ex.getScriptStackTrace()));
      } else {
        if (errorType == EcmaErrorType.ANY) {
          // passed
        } else {
          EcmaError ecmaError = (EcmaError) ex;
          assertEquals(errorType.name(), ecmaError.getName());
        }
      }
      return null;
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      Context.exit();
    }
  }