|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2025 Gargoyle Software Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package org.htmlunit.corejs; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests for the optional chaining operator (?.). |
| 21 | + * |
| 22 | + * @author Ronald Brill |
| 23 | + */ |
| 24 | +public class OptionalChainingOperatorTest { |
| 25 | + |
| 26 | + /** |
| 27 | + * @throws Exception if the test fails |
| 28 | + */ |
| 29 | + @Test |
| 30 | + public void basic() throws Exception { |
| 31 | + final String script = "var res = ''\n" |
| 32 | + + " var o = { a: 'one', b: { c: 'two' }};\n" |
| 33 | + + " res += o.x?.c;\n" |
| 34 | + + " res += ' ';\n" |
| 35 | + + " res += o.b?.c;\n" |
| 36 | + + "res"; |
| 37 | + |
| 38 | + Utils.assertWithAllModes_ES6("undefined two", script); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @throws Exception if the test fails |
| 43 | + */ |
| 44 | + @Test |
| 45 | + public void parse() throws Exception { |
| 46 | + final String script = "var res = ''\n" |
| 47 | + + " var o = {a: true};\n" |
| 48 | + + " res += o?.['a'];\n" |
| 49 | + + "res"; |
| 50 | + |
| 51 | + Utils.assertWithAllModes_ES6("true", script); |
| 52 | + } |
| 53 | +} |
0 commit comments