diff --git a/buildSrc/src/main/groovy/rhino.java-conventions.gradle b/buildSrc/src/main/groovy/rhino.java-conventions.gradle index a626fd3f1a..e41277bc0a 100644 --- a/buildSrc/src/main/groovy/rhino.java-conventions.gradle +++ b/buildSrc/src/main/groovy/rhino.java-conventions.gradle @@ -1,3 +1,7 @@ +// This file contains Gradle "conventions" that we use in all of our projects. +// It is the appropriate set of conventions to use for modules that will not be +// published, such as test modules. + plugins { id 'java-library' id 'com.diffplug.spotless' diff --git a/buildSrc/src/main/groovy/rhino.library-conventions.gradle b/buildSrc/src/main/groovy/rhino.library-conventions.gradle index f56e150f50..5973ce60dd 100644 --- a/buildSrc/src/main/groovy/rhino.library-conventions.gradle +++ b/buildSrc/src/main/groovy/rhino.library-conventions.gradle @@ -1,3 +1,8 @@ +// This file inherits from "java-conventions" and adds additional error +// checks and publishing data for modules that will be published to +// Maven Central. It should only be used for those modules that will +// be published and used by other projects. + plugins { id 'rhino.java-conventions' id 'maven-publish' diff --git a/rhino/build.gradle b/rhino/build.gradle index 9cab2d4527..160df15559 100644 --- a/rhino/build.gradle +++ b/rhino/build.gradle @@ -2,6 +2,9 @@ plugins { id 'rhino.library-conventions' } +dependencies { + testImplementation project(':testutils') +} publishing { publications { diff --git a/rhino/src/test/java/org/mozilla/javascript/tests/Utils.java b/rhino/src/test/java/org/mozilla/javascript/tests/Utils.java deleted file mode 100644 index 87f131441b..0000000000 --- a/rhino/src/test/java/org/mozilla/javascript/tests/Utils.java +++ /dev/null @@ -1,87 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package org.mozilla.javascript.tests; - -import static org.junit.Assert.assertEquals; - -import org.mozilla.javascript.Context; -import org.mozilla.javascript.ContextAction; -import org.mozilla.javascript.ContextFactory; -import org.mozilla.javascript.Scriptable; - -/** - * Misc utilities to make test code easier. - * - * @author Marc Guillemot - * @author Ronald Brill - */ -public class Utils { - /** Runs the action successively with all available optimization levels */ - public static void runWithAllOptimizationLevels(final ContextAction action) { - runWithMode(action, false); - runWithMode(action, true); - } - - /** Runs the action successively with all available optimization levels */ - public static void runWithAllOptimizationLevels( - final ContextFactory contextFactory, final ContextAction action) { - runWithMode(contextFactory, action, false); - runWithMode(contextFactory, action, true); - } - - /** Runs the provided action at the given interpretation mode */ - public static void runWithMode(final ContextAction action, final boolean interpretedMode) { - runWithMode(new ContextFactory(), action, interpretedMode); - } - - /** Runs the provided action at the given optimization level */ - public static void runWithMode( - final ContextFactory contextFactory, - final ContextAction action, - final boolean interpretedMode) { - try (final Context cx = contextFactory.enterContext()) { - cx.setInterpretedMode(interpretedMode); - action.run(cx); - } - } - - /** - * Execute the provided script in a fresh context as "myScript.js". - * - * @param script the script code - */ - static void executeScript(String script, boolean interpreted) { - Utils.runWithMode( - cx -> { - final Scriptable scope = cx.initStandardObjects(); - return cx.evaluateString(scope, script, "myScript.js", 1, null); - }, - interpreted); - } - - public static void assertWithAllOptimizationLevels(final Object expected, final String script) { - runWithAllOptimizationLevels( - cx -> { - final Scriptable scope = cx.initStandardObjects(); - final Object res = cx.evaluateString(scope, script, "test.js", 0, null); - - assertEquals(expected, res); - return null; - }); - } - - public static void assertWithAllOptimizationLevelsES6( - final Object expected, final String script) { - runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - final Object res = cx.evaluateString(scope, script, "test.js", 0, null); - - assertEquals(expected, res); - return null; - }); - } -} diff --git a/settings.gradle b/settings.gradle index b4bacbc0be..31d0175025 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,2 @@ rootProject.name = 'rhino-root' -include 'rhino', 'rhino-engine', 'rhino-tools', 'rhino-xml', 'rhino-all', 'examples', 'tests', 'benchmarks' +include 'rhino', 'rhino-engine', 'rhino-tools', 'rhino-xml', 'rhino-all', 'examples', 'testutils', 'tests', 'benchmarks' diff --git a/tests/build.gradle b/tests/build.gradle index fe40562d7b..e35e47d218 100644 --- a/tests/build.gradle +++ b/tests/build.gradle @@ -11,6 +11,7 @@ dependencies { implementation project(':rhino-tools') implementation project(':rhino-xml') + testImplementation project(':testutils') testImplementation("com.tngtech.archunit:archunit-junit4:1.3.0") { exclude group: 'junit' } diff --git a/testutils/build.gradle b/testutils/build.gradle new file mode 100644 index 0000000000..c8690e93e1 --- /dev/null +++ b/testutils/build.gradle @@ -0,0 +1,12 @@ +// This module contains library code used by the tests in multiple other modules. + +plugins { + id 'rhino.java-conventions' +} + +dependencies { + implementation project(':rhino') + // We need this to be an "implementation" dependency because, in order to be shared + // with tests in other projects, code in here must be in "main". + implementation "junit:junit:4.13.2" +} diff --git a/tests/src/test/java/org/mozilla/javascript/tests/Utils.java b/testutils/src/main/java/org/mozilla/javascript/tests/Utils.java similarity index 95% rename from tests/src/test/java/org/mozilla/javascript/tests/Utils.java rename to testutils/src/main/java/org/mozilla/javascript/tests/Utils.java index 78eee8d1a4..e3be1c6f8f 100644 --- a/tests/src/test/java/org/mozilla/javascript/tests/Utils.java +++ b/testutils/src/main/java/org/mozilla/javascript/tests/Utils.java @@ -25,6 +25,20 @@ public class Utils { /** The default set of levels to run tests at. */ public static final int[] DEFAULT_OPT_LEVELS = new int[] {-1, 9}; + /** + * Execute the provided script in a fresh context as "myScript.js". + * + * @param script the script code + */ + static void executeScript(String script, boolean interpreted) { + Utils.runWithMode( + cx -> { + final Scriptable scope = cx.initStandardObjects(); + return cx.evaluateString(scope, script, "myScript.js", 1, null); + }, + interpreted); + } + /** Runs the action successively with all available optimization levels */ public static void runWithAllOptimizationLevels(final ContextAction action) { runWithMode(action, false); @@ -50,6 +64,7 @@ public static void runWithMode(final ContextAction action, final boolean inte } /** Runs the provided action at the given optimization level */ + @SuppressWarnings("deprecation") public static void runWithOptimizationLevel( final ContextFactory contextFactory, final ContextAction action,