|
| 1 | +package org.htmlunit.corejs; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.htmlunit.corejs.javascript.Context; |
| 6 | +import org.htmlunit.corejs.javascript.ContextAction; |
| 7 | +import org.htmlunit.corejs.javascript.Scriptable; |
| 8 | +import org.htmlunit.corejs.javascript.ScriptableObject; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +/** |
| 12 | + * Unit tests for NativeReflect |
| 13 | + * |
| 14 | + * @author Ronald Brill |
| 15 | + */ |
| 16 | +public class ReflectTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + public void constructor() { |
| 20 | + final String script = |
| 21 | + " var res = '';\n" |
| 22 | + + "function foo(a, b) {\n" |
| 23 | + + " res += 'foo - ';\n" |
| 24 | + + " for (let i = 0; i < arguments.length; i++) {\n" |
| 25 | + + " res += arguments[i] + ' ';\n" |
| 26 | + + " }\n" |
| 27 | + + "}\n" |
| 28 | + |
| 29 | + + "Reflect.construct(foo, [1, 2]);\n" |
| 30 | + + "res;"; |
| 31 | + |
| 32 | + test(script, "foo - 1 2 "); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void constructorWithTarger() { |
| 37 | + final String script = |
| 38 | + " var res = '';\n" |
| 39 | + + "function foo(a, b) {\n" |
| 40 | + + " res += 'foo - ';\n" |
| 41 | + + " for (let i = 0; i < arguments.length; i++) {\n" |
| 42 | + + " res += arguments[i] + ' ';\n" |
| 43 | + + " }\n" |
| 44 | + + "}\n" |
| 45 | + + "function bar(a, b) {\n" |
| 46 | + + " res += 'bar - ';\n" |
| 47 | + + " for (let i = 0; i < arguments.length; i++) {\n" |
| 48 | + + " res += arguments[i] + ' ';\n" |
| 49 | + + " }\n" |
| 50 | + + "}\n" |
| 51 | + |
| 52 | + + "Reflect.construct(foo, [6, 7, 8], bar);\n" |
| 53 | + + "res;"; |
| 54 | + |
| 55 | + test(script, "foo - 6 7 8 "); |
| 56 | + } |
| 57 | + |
| 58 | + private static void test(final String script, final Object expected) { |
| 59 | + final ContextAction<Object> action = new ContextAction<Object>() { |
| 60 | + @Override |
| 61 | + public Object run(final Context cx) { |
| 62 | + try { |
| 63 | + Scriptable scope = cx.initSafeStandardObjects(); |
| 64 | + final Object o = cx.evaluateString(scope, script, "test_script", 1, null); |
| 65 | + assertEquals(expected, o); |
| 66 | + return o; |
| 67 | + } catch (final Exception e) { |
| 68 | + throw new RuntimeException(e); |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + Utils.runWithAllOptimizationLevels(action); |
| 74 | + } |
| 75 | +} |
0 commit comments