|
| 1 | +package org.mozilla.javascript.interpreterv2; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.mozilla.javascript.testutils.Utils; |
| 5 | + |
| 6 | +/** |
| 7 | + * Test suite for conditional catch blocks functionality in InterpreterV2. Tests the Mozilla/Rhino |
| 8 | + * extension: catch (e if condition) { ... } |
| 9 | + */ |
| 10 | +class ConditionalCatchTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + void testBasicConditionalCatch() { |
| 14 | + String script = |
| 15 | + "var result = 'none';\n" |
| 16 | + + "try {\n" |
| 17 | + + " throw new TypeError('test error');\n" |
| 18 | + + "} catch (e if e instanceof TypeError) {\n" |
| 19 | + + " result = 'caught TypeError';\n" |
| 20 | + + "} catch (e) {\n" |
| 21 | + + " result = 'caught other';\n" |
| 22 | + + "}\n" |
| 23 | + + "result;"; |
| 24 | + |
| 25 | + Utils.assertWithAllModes_ES6("caught TypeError", script); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void testMultipleConditionalCatches() { |
| 30 | + String script = |
| 31 | + "var result = 'none';\n" |
| 32 | + + "try {\n" |
| 33 | + + " throw new ReferenceError('reference error');\n" |
| 34 | + + "} catch (e if e instanceof TypeError) {\n" |
| 35 | + + " result = 'caught TypeError';\n" |
| 36 | + + "} catch (e if e instanceof ReferenceError) {\n" |
| 37 | + + " result = 'caught ReferenceError';\n" |
| 38 | + + "} catch (e) {\n" |
| 39 | + + " result = 'caught other';\n" |
| 40 | + + "}\n" |
| 41 | + + "result;"; |
| 42 | + |
| 43 | + Utils.assertWithAllModes_ES6("caught ReferenceError", script); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testComplexCondition() { |
| 48 | + String script = |
| 49 | + "var result = 'none';\n" |
| 50 | + + "try {\n" |
| 51 | + + " throw new TypeError('special error');\n" |
| 52 | + + "} catch (e if e instanceof TypeError && e.message.indexOf('special') >= 0) {\n" |
| 53 | + + " result = 'caught special TypeError';\n" |
| 54 | + + "} catch (e if e instanceof TypeError) {\n" |
| 55 | + + " result = 'caught regular TypeError';\n" |
| 56 | + + "} catch (e) {\n" |
| 57 | + + " result = 'caught other';\n" |
| 58 | + + "}\n" |
| 59 | + + "result;"; |
| 60 | + |
| 61 | + Utils.assertWithAllModes_ES6("caught special TypeError", script); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testFallthroughToDefault() { |
| 66 | + String script = |
| 67 | + "var result = 'none';\n" |
| 68 | + + "try {\n" |
| 69 | + + " throw new Error('generic error');\n" |
| 70 | + + "} catch (e if e instanceof TypeError) {\n" |
| 71 | + + " result = 'caught TypeError';\n" |
| 72 | + + "} catch (e if e instanceof ReferenceError) {\n" |
| 73 | + + " result = 'caught ReferenceError';\n" |
| 74 | + + "} catch (e) {\n" |
| 75 | + + " result = 'caught default';\n" |
| 76 | + + "}\n" |
| 77 | + + "result;"; |
| 78 | + |
| 79 | + Utils.assertWithAllModes_ES6("caught default", script); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testVariableBinding() { |
| 84 | + String script = |
| 85 | + "var result = 'none';\n" |
| 86 | + + "var outerVar = 'outer';\n" |
| 87 | + + "try {\n" |
| 88 | + + " throw new TypeError('test');\n" |
| 89 | + + "} catch (e if outerVar === 'outer' && e instanceof TypeError) {\n" |
| 90 | + + " result = 'caught with outer variable: ' + outerVar;\n" |
| 91 | + + "} catch (e) {\n" |
| 92 | + + " result = 'caught other';\n" |
| 93 | + + "}\n" |
| 94 | + + "result;"; |
| 95 | + |
| 96 | + Utils.assertWithAllModes_ES6("caught with outer variable: outer", script); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testScopeIsolation() { |
| 101 | + String script = |
| 102 | + "var e = 'outer';\n" |
| 103 | + + "var result = 'none';\n" |
| 104 | + + "try {\n" |
| 105 | + + " throw new TypeError('test');\n" |
| 106 | + + "} catch (e if e instanceof TypeError) {\n" |
| 107 | + + " result = 'inner e is TypeError: ' + (e instanceof TypeError);\n" |
| 108 | + + "}\n" |
| 109 | + + "result + ', outer e: ' + e;"; |
| 110 | + |
| 111 | + Utils.assertWithAllModes_ES6("inner e is TypeError: true, outer e: outer", script); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void testNestedConditionalCatch() { |
| 116 | + String script = |
| 117 | + "var result = 'none';\n" |
| 118 | + + "try {\n" |
| 119 | + + " try {\n" |
| 120 | + + " throw new TypeError('inner error');\n" |
| 121 | + + " } catch (e if e instanceof ReferenceError) {\n" |
| 122 | + + " result = 'caught inner ReferenceError';\n" |
| 123 | + + " } catch (e if e instanceof TypeError) {\n" |
| 124 | + + " result = 'caught inner TypeError';\n" |
| 125 | + + " throw new ReferenceError('outer error');\n" |
| 126 | + + " }\n" |
| 127 | + + "} catch (e if e instanceof ReferenceError) {\n" |
| 128 | + + " result = 'caught outer ReferenceError';\n" |
| 129 | + + "} catch (e) {\n" |
| 130 | + + " result = 'caught outer other';\n" |
| 131 | + + "}\n" |
| 132 | + + "result;"; |
| 133 | + |
| 134 | + Utils.assertWithAllModes_ES6("caught outer ReferenceError", script); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void testConditionalCatchWithFinally() { |
| 139 | + String script = |
| 140 | + "var result = 'none';\n" |
| 141 | + + "var finallyExecuted = false;\n" |
| 142 | + + "try {\n" |
| 143 | + + " throw new TypeError('test');\n" |
| 144 | + + "} catch (e if e instanceof TypeError) {\n" |
| 145 | + + " result = 'caught TypeError';\n" |
| 146 | + + "} catch (e) {\n" |
| 147 | + + " result = 'caught other';\n" |
| 148 | + + "} finally {\n" |
| 149 | + + " finallyExecuted = true;\n" |
| 150 | + + "}\n" |
| 151 | + + "result + ',' + finallyExecuted;"; |
| 152 | + |
| 153 | + Utils.assertWithAllModes_ES6("caught TypeError,true", script); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void testStringException() { |
| 158 | + String script = |
| 159 | + "var result = 'none';\n" |
| 160 | + + "try {\n" |
| 161 | + + " throw 'string exception';\n" |
| 162 | + + "} catch (e if typeof e === 'string') {\n" |
| 163 | + + " result = 'caught string: ' + e;\n" |
| 164 | + + "} catch (e) {\n" |
| 165 | + + " result = 'caught other';\n" |
| 166 | + + "}\n" |
| 167 | + + "result;"; |
| 168 | + |
| 169 | + Utils.assertWithAllModes_ES6("caught string: string exception", script); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + void testNumberException() { |
| 174 | + String script = |
| 175 | + "var result = 'none';\n" |
| 176 | + + "try {\n" |
| 177 | + + " throw 42;\n" |
| 178 | + + "} catch (e if typeof e === 'number') {\n" |
| 179 | + + " result = 'caught number: ' + e;\n" |
| 180 | + + "} catch (e) {\n" |
| 181 | + + " result = 'caught other';\n" |
| 182 | + + "}\n" |
| 183 | + + "result;"; |
| 184 | + |
| 185 | + Utils.assertWithAllModes_ES6("caught number: 42", script); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void testFunctionCallInCondition() { |
| 190 | + String script = |
| 191 | + "function isSpecialError(e) {\n" |
| 192 | + + " return e instanceof TypeError && e.message === 'special';\n" |
| 193 | + + "}\n" |
| 194 | + + "\n" |
| 195 | + + "var result = 'none';\n" |
| 196 | + + "try {\n" |
| 197 | + + " throw new TypeError('special');\n" |
| 198 | + + "} catch (e if isSpecialError(e)) {\n" |
| 199 | + + " result = 'caught special error';\n" |
| 200 | + + "} catch (e) {\n" |
| 201 | + + " result = 'caught other';\n" |
| 202 | + + "}\n" |
| 203 | + + "result;"; |
| 204 | + |
| 205 | + Utils.assertWithAllModes_ES6("caught special error", script); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + void testConditionEvaluationOrder() { |
| 210 | + String script = |
| 211 | + "var evaluationOrder = [];\n" |
| 212 | + + "\n" |
| 213 | + + "function condition1(e) {\n" |
| 214 | + + " evaluationOrder.push('condition1');\n" |
| 215 | + + " return false;\n" |
| 216 | + + "}\n" |
| 217 | + + "\n" |
| 218 | + + "function condition2(e) {\n" |
| 219 | + + " evaluationOrder.push('condition2');\n" |
| 220 | + + " return true;\n" |
| 221 | + + "}\n" |
| 222 | + + "\n" |
| 223 | + + "try {\n" |
| 224 | + + " throw new Error('test');\n" |
| 225 | + + "} catch (e if condition1(e)) {\n" |
| 226 | + + " // Should not reach here\n" |
| 227 | + + "} catch (e if condition2(e)) {\n" |
| 228 | + + " // Should reach here\n" |
| 229 | + + "} catch (e) {\n" |
| 230 | + + " // Should not reach here\n" |
| 231 | + + "}\n" |
| 232 | + + "\n" |
| 233 | + + "evaluationOrder.join(',');"; |
| 234 | + |
| 235 | + Utils.assertWithAllModes_ES6("condition1,condition2", script); |
| 236 | + } |
| 237 | +} |
0 commit comments