Ejemplo n.º 1
0
  @Before
  public void setUp() throws Throwable {
    // Filter disabled tests
    assumeTrue(test.isEnabled());

    String fileContent = test.readFile();
    if (!test.isValidTest(isStrictTest, unmarkedDefault)) {
      return;
    }

    final String preamble;
    if (test.isRaw()) {
      preamble = "";
      preambleLines = 0;
    } else if (isStrictTest) {
      preamble = "\"use strict\";\nvar strict_mode = true;\n";
      preambleLines = 2;
    } else {
      preamble = "//\"use strict\";\nvar strict_mode = false;\n";
      preambleLines = 2;
    }
    sourceCode = Strings.concat(preamble, fileContent);

    global = globals.newGlobal(new Test262Console(), test);
    exceptionHandler.setExecutionContext(global.getRealm().defaultContext());

    if (!test.isNegative()) {
      errorHandler.match(StandardErrorHandler.defaultMatcher());
      exceptionHandler.match(ScriptExceptionHandler.defaultMatcher());
    } else {
      expected.expect(
          Matchers.either(StandardErrorHandler.defaultMatcher())
              .or(ScriptExceptionHandler.defaultMatcher())
              .or(instanceOf(Test262AssertionError.class)));
      String errorType = test.getErrorType();
      if (errorType != null) {
        expected.expect(
            hasErrorMessage(
                global.getRealm().defaultContext(),
                matchesPattern(errorType, Pattern.CASE_INSENSITIVE)));
      }
    }

    // Load test includes
    for (String name : test.getIncludes()) {
      global.include(name);
    }

    if (test.isAsync()) {
      // "doneprintHandle.js" is replaced with AsyncHelper
      async = global.install(new AsyncHelper(), AsyncHelper.class);
    }

    // Install test hooks
    if (!test.getScript().startsWith(selfTestDirectory)) {
      global.install(global, Test262GlobalObject.class);
    }
  }
Ejemplo n.º 2
0
  @Before
  public void setUp() throws Throwable {
    assumeTrue("Test disabled", test.isEnabled());

    String fileContent = test.readFile();
    if (!isValidTestConfiguration()) {
      return;
    }

    final String preamble;
    if (test.isRaw() || test.isModule()) {
      preamble = "";
      preambleLines = 0;
    } else if (isStrictTest) {
      preamble = "\"use strict\";\nvar strict_mode = true;\n";
      preambleLines = 2;
    } else {
      preamble = "//\"use strict\";\nvar strict_mode = false;\n";
      preambleLines = 2;
    }
    sourceCode = Strings.concat(preamble, fileContent);

    global = globals.newGlobal(new Test262Console(), test);
    exceptionHandler.setExecutionContext(global.getRealm().defaultContext());

    if (!test.isNegative()) {
      errorHandler.match(StandardErrorHandler.defaultMatcher());
      exceptionHandler.match(ScriptExceptionHandler.defaultMatcher());
    } else {
      expected.expect(
          Matchers.either(StandardErrorHandler.defaultMatcher())
              .or(ScriptExceptionHandler.defaultMatcher()));
      String errorType = test.getErrorType();
      if (errorType != null) {
        expected.expect(
            hasErrorMessage(
                global.getRealm().defaultContext(),
                matchesPattern(errorType, Pattern.CASE_INSENSITIVE)));
      }
    }

    // Load test includes
    for (String name : test.getIncludes()) {
      global.include(name);
    }

    if (test.isAsync()) {
      async = global.install(new Test262Async(), Test262Async.class);
    }
  }
Ejemplo n.º 3
0
/** The standard test262 test suite (strict) */
@RunWith(Parallelized.class)
@UseParametersRunnerFactory(ParameterizedRunnerFactory.class)
@TestConfiguration(name = "test262.test.strict", file = "resource:/test-configuration.properties")
public final class Test262Strict {
  private static final Configuration configuration = loadConfiguration(Test262Strict.class);
  private static final DefaultMode unmarkedDefault =
      DefaultMode.forName(configuration.getString("unmarked_default"));
  private static final Path selfTestDirectory = Paths.get(configuration.getString("self_test"));

  @Parameters(name = "{0}")
  public static List<Test262Info> suiteValues() throws IOException {
    return Resources.loadTests(
        configuration,
        new BiFunction<Path, Path, Test262Info>() {
          @Override
          public Test262Info apply(Path basedir, Path file) {
            return new Test262Info(basedir, file);
          }
        });
  }

  @ClassRule
  public static TestGlobals<Test262GlobalObject, Test262Info> globals =
      new TestGlobals<Test262GlobalObject, Test262Info>(configuration) {
        @Override
        protected ObjectAllocator<Test262GlobalObject> newAllocator(
            ShellConsole console, Test262Info test, ScriptCache scriptCache) {
          return newGlobalObjectAllocator(console, test, scriptCache);
        }
      };

  @Rule
  public TestWatcher watcher =
      new TestWatcher() {
        @Override
        protected void starting(Description description) {
          isStrictTest = description.getAnnotation(Strict.class) != null;
        }
      };

  private boolean isStrictTest = false;

  @Rule public Timeout maxTime = new Timeout(120, TimeUnit.SECONDS);

  @Rule public StandardErrorHandler errorHandler = StandardErrorHandler.none();

  @Rule public ScriptExceptionHandler exceptionHandler = ScriptExceptionHandler.none();

  @Rule public ExpectedException expected = ExpectedException.none();

  @Parameter(0)
  public Test262Info test;

  private Test262GlobalObject global;
  private AsyncHelper async;
  private String sourceCode;
  private int preambleLines;

  @Before
  public void setUp() throws Throwable {
    // Filter disabled tests
    assumeTrue(test.isEnabled());

    String fileContent = test.readFile();
    if (!test.isValidTest(isStrictTest, unmarkedDefault)) {
      return;
    }

    final String preamble;
    if (test.isRaw()) {
      preamble = "";
      preambleLines = 0;
    } else if (isStrictTest) {
      preamble = "\"use strict\";\nvar strict_mode = true;\n";
      preambleLines = 2;
    } else {
      preamble = "//\"use strict\";\nvar strict_mode = false;\n";
      preambleLines = 2;
    }
    sourceCode = Strings.concat(preamble, fileContent);

    global = globals.newGlobal(new Test262Console(), test);
    exceptionHandler.setExecutionContext(global.getRealm().defaultContext());

    if (!test.isNegative()) {
      errorHandler.match(StandardErrorHandler.defaultMatcher());
      exceptionHandler.match(ScriptExceptionHandler.defaultMatcher());
    } else {
      expected.expect(
          Matchers.either(StandardErrorHandler.defaultMatcher())
              .or(ScriptExceptionHandler.defaultMatcher())
              .or(instanceOf(Test262AssertionError.class)));
      String errorType = test.getErrorType();
      if (errorType != null) {
        expected.expect(
            hasErrorMessage(
                global.getRealm().defaultContext(),
                matchesPattern(errorType, Pattern.CASE_INSENSITIVE)));
      }
    }

    // Load test includes
    for (String name : test.getIncludes()) {
      global.include(name);
    }

    if (test.isAsync()) {
      // "doneprintHandle.js" is replaced with AsyncHelper
      async = global.install(new AsyncHelper(), AsyncHelper.class);
    }

    // Install test hooks
    if (!test.getScript().startsWith(selfTestDirectory)) {
      global.install(global, Test262GlobalObject.class);
    }
  }

  @After
  public void tearDown() {
    if (global != null) {
      global.getScriptLoader().getExecutor().shutdown();
    }
  }

  @Test
  public void runTest() throws Throwable {
    if (!test.isValidTest(isStrictTest, unmarkedDefault)) {
      return;
    }
    // Evaluate actual test-script
    if (test.isModule()) {
      global.evalModule(test.toModuleName(), sourceCode, 1 - preambleLines);
    } else {
      global.eval(test.toFile(), sourceCode, 1 - preambleLines);
    }

    // Wait for pending tasks to finish
    if (test.isAsync()) {
      assertFalse(async.doneCalled);
      global.getRealm().getWorld().runEventLoop();
      assertTrue(async.doneCalled);
    } else {
      global.getRealm().getWorld().runEventLoop();
    }
  }

  @Test
  @Strict
  public void runTestStrict() throws Throwable {
    if (!test.isValidTest(isStrictTest, unmarkedDefault)) {
      return;
    }
    // Evaluate actual test-script
    if (test.isModule()) {
      global.evalModule(test.toModuleName(), sourceCode, 1 - preambleLines);
    } else {
      global.eval(test.toFile(), sourceCode, 1 - preambleLines);
    }

    // Wait for pending tasks to finish
    if (test.isAsync()) {
      assertFalse(async.doneCalled);
      global.getRealm().getWorld().runEventLoop();
      assertTrue(async.doneCalled);
    } else {
      global.getRealm().getWorld().runEventLoop();
    }
  }

  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.METHOD})
  public @interface Strict {}

  public static final class AsyncHelper {
    boolean doneCalled = false;

    @Properties.Function(name = "$DONE", arity = 0)
    public void done(boolean argument) {
      assertFalse(doneCalled);
      doneCalled = true;
      if (argument) {
        throw new Test262AssertionError(argument);
      }
    }
  }
}