Skip to content

Commit

Permalink
simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Dec 15, 2024
1 parent eab85bd commit f54930a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
/** */
package org.mozilla.javascript.tests;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mozilla.javascript.Scriptable;

/**
* See https://bugzilla.mozilla.org/show_bug.cgi?id=409702
Expand All @@ -33,25 +30,13 @@ public final void a() {}

@Test
public void adapter() {
final int value = 12;
String source =
"var instance = "
+ " new JavaAdapter("
String script =
"var instance = new JavaAdapter("
+ Foo.Subclass.class.getName()
+ ","
+ "{ b: function () { return "
+ value
+ "; } });"
+ "{ b: function () { return 12; } });"
+ "instance.b();";

Utils.runWithAllOptimizationLevels(
cx -> {
final Scriptable scope = cx.initStandardObjects();

Object result = cx.evaluateString(scope, source, "source", 1, null);
assertEquals(Integer.valueOf(value), result);

return null;
});
Utils.assertWithAllOptimizationLevels(12, script);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.mozilla.javascript.tests;

import org.junit.Test;
import org.mozilla.javascript.ScriptableObject;

/**
* See https://bugzilla.mozilla.org/show_bug.cgi?id=412433
Expand All @@ -17,12 +16,6 @@ public class Bug412433Test {

@Test
public void malformedJavascript2() {
Utils.runWithAllOptimizationLevels(
cx -> {
ScriptableObject scope = cx.initStandardObjects();
cx.evaluateString(scope, "\"\".split(/[/?,/&]/)", "", 0, null);

return null;
});
Utils.assertWithAllOptimizationLevels("", "'' + \"\".split(/[/?,/&]/)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,29 @@

package org.mozilla.javascript.tests;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mozilla.javascript.Scriptable;

/**
* See https://bugzilla.mozilla.org/show_bug.cgi?id=419940
*
* @author Norris Boyd
*/
public class Bug419940Test {
static final int value = 12;

public abstract static class BaseFoo {
public abstract int doSomething();
}

public static class Foo extends BaseFoo {
@Override
public int doSomething() {
return value;
return 12;
}
}

@Test
public void adapter() {
String source = "(new JavaAdapter(" + Foo.class.getName() + ", {})).doSomething();";

Utils.runWithAllOptimizationLevels(
cx -> {
Scriptable scope = cx.initStandardObjects();
Object result = cx.evaluateString(scope, source, "source", 1, null);
assertEquals(Integer.valueOf(value), result);
String script = "(new JavaAdapter(" + Foo.class.getName() + ", {})).doSomething();";

return null;
});
Utils.assertWithAllOptimizationLevels(12, script);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.junit.Test;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;

/**
* Test for delete that should apply for properties defined in prototype chain. See
Expand All @@ -27,11 +28,6 @@ public void deletePropInPrototype() throws Exception {
+ "[].foo();\n"
+ "[][1]();\n";

Utils.runWithAllOptimizationLevels(
_cx -> {
final ScriptableObject scope = _cx.initStandardObjects();
final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
return null;
});
Utils.assertWithAllOptimizationLevels(Undefined.instance, script);
}
}
22 changes: 6 additions & 16 deletions rhino/src/test/java/org/mozilla/javascript/tests/FunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class FunctionTest {
*/
@Test
public void functionWithSlashSlash() {
assertEvaluates(true, "new Function('return true//;').call()");
Utils.assertWithAllOptimizationLevels(true, "new Function('return true//;').call()");
}

@Test
Expand All @@ -39,7 +39,7 @@ public void functionHasNameOfVarStrictMode() throws Exception {
+ "foo();\n"
+ "result;";

assertEvaluates("-outer abc = 1-inner abc = function", script);
Utils.assertWithAllOptimizationLevels("-outer abc = 1-inner abc = function", script);
}

@Test
Expand All @@ -59,7 +59,7 @@ public void innerFunctionWithSameName() throws Exception {
+ "b();\n"
+ "result;";

assertEvaluates("ab", script);
Utils.assertWithAllOptimizationLevels("ab", script);
}

@Test
Expand All @@ -76,7 +76,7 @@ public void innerFunctionWithSameNameAsOutsideStrict() throws Exception {
+ "a();\n"
+ "result;";

assertEvaluates("a", script);
Utils.assertWithAllOptimizationLevels("a", script);
}

@Test
Expand All @@ -92,7 +92,7 @@ public void secondFunctionWithSameNameStrict() throws Exception {
+ "func();\n"
+ "result;";

assertEvaluates("functionfunc(){result+=norm(func);}outer", script);
Utils.assertWithAllOptimizationLevels("functionfunc(){result+=norm(func);}outer", script);
}

@Test
Expand Down Expand Up @@ -137,16 +137,6 @@ public void functioNamesExceptionsStrict() throws Exception {
+ "result;";

// assertEvaluates("f1f2f3!f4f5!f6!f7!f8f10f11f12!f10f11f12f13", script);
assertEvaluates("f1f2f3!f4f5!f6!f7!f8f10f11f12f11f12f13", script);
}

private static void assertEvaluates(final Object expected, final String source) {
Utils.runWithAllOptimizationLevels(
cx -> {
final Scriptable scope = cx.initStandardObjects();
final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
assertEquals(expected, rep);
return null;
});
Utils.assertWithAllOptimizationLevels("f1f2f3!f4f5!f6!f7!f8f10f11f12f11f12f13", script);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void parseFloatAndIntWhiteSpaces() {
}

private void testParseFloatWhiteSpaces(final String prefix) {
assertEvaluates("789", "String(parseInt('" + prefix + "789 '))");
assertEvaluates("7.89", "String(parseFloat('" + prefix + "7.89 '))");
Utils.assertWithAllOptimizationLevels("789", "String(parseInt('" + prefix + "789 '))");
Utils.assertWithAllOptimizationLevels("7.89", "String(parseFloat('" + prefix + "7.89 '))");
}

/**
Expand Down Expand Up @@ -70,16 +70,6 @@ public void parseFloatTrailingNoise() {
}

private static void testParseFloat(final String expected, final String value) {
assertEvaluates(expected, "String(parseFloat('" + value + "'))");
}

private static void assertEvaluates(final Object expected, final String source) {
Utils.runWithAllOptimizationLevels(
cx -> {
final Scriptable scope = cx.initStandardObjects();
final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
assertEquals(expected, rep);
return null;
});
Utils.assertWithAllOptimizationLevels(expected, "String(parseFloat('" + value + "'))");
}
}

0 comments on commit f54930a

Please sign in to comment.