|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
4 | 4 |
|
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
5 | 8 | import org.junit.Test; |
6 | 9 | import org.mozilla.javascript.Context; |
7 | 10 | import org.mozilla.javascript.ScriptableObject; |
@@ -37,6 +40,88 @@ public void safeStandard() { |
37 | 40 | } |
38 | 41 | } |
39 | 42 |
|
| 43 | + @Test |
| 44 | + public void safeStandardSupportsStringFromContext() { |
| 45 | + String code = |
| 46 | + "let res = '';\n" |
| 47 | + + "res += str[0];\n" |
| 48 | + + "res += str.length;\n" |
| 49 | + + "res += ' ';\n" |
| 50 | + + "res += str;\n" |
| 51 | + + "res;"; |
| 52 | + |
| 53 | + try (Context cx = Context.enter()) { |
| 54 | + ScriptableObject root = cx.initSafeStandardObjects(); |
| 55 | + root.put("str", root, "Rhino"); |
| 56 | + Object result = cx.evaluateString(root, code, "test", 1, null); |
| 57 | + assertEquals("R5 Rhino", result); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void safeStandardSupportsArrayFromContext() { |
| 63 | + String code = |
| 64 | + "let res = '';\n" |
| 65 | + + "res += arr[0];\n" |
| 66 | + + "res += arr.length;\n" |
| 67 | + + "res += ' ';\n" |
| 68 | + + "for (let elem in arr) { res += arr[elem]; }\n" |
| 69 | + + "res += ' ';\n" |
| 70 | + + "for (let elem of arr) { res += elem; }\n" |
| 71 | + + "res;"; |
| 72 | + |
| 73 | + try (Context cx = Context.enter()) { |
| 74 | + ScriptableObject root = cx.initSafeStandardObjects(); |
| 75 | + root.put("arr", root, new String[] {"a", "b", "d"}); |
| 76 | + Object result = cx.evaluateString(root, code, "test", 1, null); |
| 77 | + assertEquals("a3 abd abd", result); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void safeStandardSupportsListFromContext() { |
| 83 | + String code = |
| 84 | + "let res = '';\n" |
| 85 | + + "res += lst[0];\n" |
| 86 | + + "res += lst.length;\n" |
| 87 | + + "res += ' ';\n" |
| 88 | + + "for (let elem in lst) { res += lst[elem]; }\n" |
| 89 | + + "res += ' ';\n" |
| 90 | + + "for (let elem of lst) { res += elem; }\n" |
| 91 | + + "res;"; |
| 92 | + |
| 93 | + try (Context cx = Context.enter()) { |
| 94 | + ScriptableObject root = cx.initSafeStandardObjects(); |
| 95 | + root.put("lst", root, List.of("a", "b", "c")); |
| 96 | + Object result = cx.evaluateString(root, code, "test", 1, null); |
| 97 | + assertEquals("a3 abc abc", result); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void safeStandardSupportsMapFromContext() { |
| 103 | + String code = "let res = '';\n" + "for (let elem of mp) { res += elem; }\n" + "res;"; |
| 104 | + |
| 105 | + try (Context cx = Context.enter()) { |
| 106 | + ScriptableObject root = cx.initSafeStandardObjects(); |
| 107 | + root.put("mp", root, Map.of("k0", "v0")); |
| 108 | + Object result = cx.evaluateString(root, code, "test", 1, null); |
| 109 | + assertEquals("k0,v0", result); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void safeStandardSupportsSetFromContext() { |
| 115 | + String code = "let res = '';\n" + "for (let elem of mp) { res += elem; }\n" + "res;"; |
| 116 | + |
| 117 | + try (Context cx = Context.enter()) { |
| 118 | + ScriptableObject root = cx.initSafeStandardObjects(); |
| 119 | + root.put("mp", root, Set.of("v0")); |
| 120 | + Object result = cx.evaluateString(root, code, "test", 1, null); |
| 121 | + assertEquals("v0", result); |
| 122 | + } |
| 123 | + } |
| 124 | + |
40 | 125 | @Test |
41 | 126 | public void standardSealed() { |
42 | 127 | try (Context cx = Context.enter()) { |
|
0 commit comments