From 997f2f08e075cc90bfd810356fa6cbfc76d5ebf8 Mon Sep 17 00:00:00 2001 From: michaelfrost Date: Fri, 5 Sep 2025 12:30:04 +0300 Subject: [PATCH] Update tests for 7.12.3 Description: Update tests for 7.12.3 Record Indexing Expression. Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICWMYE Test: All pre-merge tests passed. Results are available in the ggwatcher. Signed-off-by: michaelfrost Change-Id: Ic533d678b24cb23bc7bd3a7540084ad2bccc7cbc --- ...{record17.ets => dynamic_record_index.ets} | 10 +-- .../dynamic_record_index.params.yaml | 18 +++++ .../ind.params.yaml | 67 ++----------------- .../ind2.params.yaml | 8 +-- .../03.record_indexing_expression/indx1c.ets | 30 --------- .../03.record_indexing_expression/indx1e.ets | 30 --------- .../03.record_indexing_expression/indx3b.ets | 29 -------- .../03.record_indexing_expression/indx3d.ets | 29 -------- ...{indx1d.ets => nullable_record_access.ets} | 11 +-- .../nullable_record_access.params.yaml | 19 ++++++ ...ndx1b.ets => nullable_record_access_n.ets} | 10 +-- .../nullable_record_access_n.params.yaml | 19 ++++++ .../record04.ets | 33 --------- .../record07.ets | 34 ---------- .../record08.ets | 36 ---------- .../record09.ets | 32 --------- .../record10.ets | 32 --------- .../record11.ets | 28 -------- .../record12.ets | 27 -------- .../record14.ets | 31 --------- .../record15.ets | 30 --------- .../record16.ets | 28 -------- .../record18.ets | 30 --------- .../record19.ets | 31 --------- .../record21.ets | 29 -------- .../record22.ets | 32 --------- .../record23.ets | 31 --------- .../record24.ets | 31 --------- .../record25.ets | 29 -------- .../record26.ets | 29 -------- .../record27.ets | 34 ---------- ...dx3c.ets => record_access_wrong_key_n.ets} | 25 ++++--- .../record_access_wrong_key_n.params.yaml | 19 ++++++ .../{record28.ets => record_cast_index.ets} | 18 +++-- .../record_cast_index.params.yaml | 22 ++++++ .../{record06.ets => record_enum_index.ets} | 20 +++--- .../record_enum_index.params.yaml | 20 ++++++ .../{record02.ets => record_field_access.ets} | 20 +++--- .../record_field_access.params.yaml | 19 ++++++ .../{record01.ets => record_field_set.ets} | 20 +++--- .../record_field_set.params.yaml | 18 +++++ .../{record03.ets => record_number_index.ets} | 24 ++++--- .../record_number_index.params.yaml | 28 ++++++++ ...{indx3a.ets => record_set_wrong_key_n.ets} | 25 ++++--- .../record_set_wrong_key_n.params.yaml | 19 ++++++ .../{record13.ets => record_string_index.ets} | 17 +++-- .../record_string_index.params.yaml | 24 +++++++ ...cord05.ets => record_union_type_index.ets} | 12 ++-- .../record_union_type_index.params.yaml | 20 ++++++ .../test-lists/ets-cts/ets-cts-ignored.txt | 18 ++++- 50 files changed, 401 insertions(+), 854 deletions(-) rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{record17.ets => dynamic_record_index.ets} (82%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/dynamic_record_index.params.yaml delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1c.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1e.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3b.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3d.ets rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{indx1d.ets => nullable_record_access.ets} (78%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{indx1b.ets => nullable_record_access_n.ets} (79%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access_n.params.yaml delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record04.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record07.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record08.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record09.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record10.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record11.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record12.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record14.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record15.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record16.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record18.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record19.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record21.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record22.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record23.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record24.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record25.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record26.ets delete mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record27.ets rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{indx3c.ets => record_access_wrong_key_n.ets} (48%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_access_wrong_key_n.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{record28.ets => record_cast_index.ets} (75%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_cast_index.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{record06.ets => record_enum_index.ets} (73%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{record02.ets => record_field_access.ets} (68%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{record01.ets => record_field_set.ets} (69%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{record03.ets => record_number_index.ets} (65%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_number_index.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{indx3a.ets => record_set_wrong_key_n.ets} (47%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_set_wrong_key_n.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{record13.ets => record_string_index.ets} (71%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_string_index.params.yaml rename static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/{record05.ets => record_union_type_index.ets} (82%) create mode 100644 static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_union_type_index.params.yaml diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record17.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/dynamic_record_index.ets similarity index 82% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record17.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/dynamic_record_index.ets index cde42b2a13..57a334076e 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record17.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/dynamic_record_index.ets @@ -13,18 +13,20 @@ * limitations under the License. */ +{% for c in cases %} /*--- desc: Record index expression - the keys of the record are dynamically generated. ---*/ -let dynamicKeyRecord: Record = {}; +let dynamicRecord: Record = {}; function setValue(key: string, value: number) { - dynamicKeyRecord[key] = value; + dynamicRecord[key] = value; } function main(): void { setValue("key1", 10); setValue("key2", 20); - arktest.assertEQ(dynamicKeyRecord["key1"], 10); -} \ No newline at end of file + arktest.assertEQ(dynamicRecord["{{c.index}}"], {{c.value}}); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/dynamic_record_index.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/dynamic_record_index.params.yaml new file mode 100644 index 0000000000..82fdb028cb --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/dynamic_record_index.params.yaml @@ -0,0 +1,18 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { index: 'key1', value: 10 } + - { index: 'key2', value: 20 } + - { index: 'key3', value: undefined } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/ind.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/ind.params.yaml index f02abe58e6..490fb80842 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/ind.params.yaml +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/ind.params.yaml @@ -29,8 +29,7 @@ cases: r["3"] = new Short(30) arktest.assertTrue(r["1"] == 10 && r["2"] == 20 && r["3"] == 30) - - tags: 'compile-only, negative' - use: |- + - use: |- // key is a union of literal types let idx: Short = 0 let r: Record<"11"|"12"|"13", Short> = { @@ -39,46 +38,9 @@ cases: "13": idx, } let i = "11" - arktest.assertTrue(r[i] == 0) // CTE expected, expression is not permitted in index - - - tags: 'compile-only, negative' - decl: |- - function foo(p: string): string { - return p + "2" - } - use: |- - // key is a union of literal types - let idx: Short = 0 - let r: Record<"11"|"12"|"13", Short> = { - "11": idx, - "12": idx, - "13": idx, - } - r[foo("1")] = 99 // CTE expected, expression is not permitted in index - arktest.assertTrue(r["12"] == 99) - - - tags: 'compile-only, negative' - use: |- - // key is a union of literal types - let idx: Byte = 0 - let r: Record<" "|" "|" ", Byte> = { - " " : idx, - " " : idx, - " " : idx, - } - arktest.assertTrue(r["?"] == undefined) // CTE expected, unknown key - - - tags: 'compile-only, negative' - use: |- - // key is a union of literal types - let idx: Byte = 0 - let r: Record<" "|" "|" ", Byte> = { - " " : idx, - " " : idx, - " " : idx, - } - r[""] = ++idx; // CTE expected, unknown key - arktest.assertTrue(r[" "] == idx) + arktest.assertEQ(r[i], 0) + arktest.assertEQ(r["12"], 0) + arktest.assertEQ(r["13"], 0) # Record key is a union of string literal types and string - use: |- @@ -119,27 +81,6 @@ cases: r[foo(1)] = 99 arktest.assertTrue(r["1"] == 99 && r["2"] == 2 && r["3"] == undefined) - - tags: 'compile-only, negative' - use: |- - // key is a union of literal types and string - let idx: int = 0 - let r: Record<"1"|"2"|"3"|string, Int> = { - "1": idx + 1, - "2": idx + 2, - } - arktest.assertTrue(r[1] == undefined) // CTE: wrong key type - - - tags: 'compile-only, negative' - use: |- - // key is a union of literal types and string - let idx: int = 0 - let r: Record<"1"|"2"|"3"|string, Int> = { - "1": idx + 1, - "2": idx + 2, - } - r[2] = 99 // CTE: wrong key type - arktest.assertTrue(r["2"] == 2) - # Record key is of numeric type - use: |- // key is of numeric type diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/ind2.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/ind2.params.yaml index 8ad3f8a3fe..1e74b547d6 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/ind2.params.yaml +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/ind2.params.yaml @@ -13,8 +13,8 @@ --- cases: - - { ref_fail: 'false', idx_fail: 'false', idx: '"1"', res: '" ref idx passed 42"' } + - { ref_fail: 'false', idx_fail: 'false', idx: '"1"', res: '" ref idx passed 42"' } - { ref_fail: 'false', idx_fail: 'false', idx: '"2"', res: '" ref idx passed undefined"' } - - { ref_fail: 'false', idx_fail: 'true', idx: '"1"', res: '" ref idx failed 99"' } - - { ref_fail: 'true', idx_fail: 'false', idx: '"1"', res: '" ref failed 99"' } - - { ref_fail: 'true', idx_fail: 'true', idx: '"1"', res: '" ref failed 99"' } + - { ref_fail: 'false', idx_fail: 'true', idx: '"1"', res: '" ref idx failed 99"' } + - { ref_fail: 'true', idx_fail: 'false', idx: '"1"', res: '" ref failed 99"' } + - { ref_fail: 'true', idx_fail: 'true', idx: '"1"', res: '" ref failed 99"' } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1c.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1c.ets deleted file mode 100644 index 2b3677251a..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1c.ets +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: >- - If no ‘?.’ is present in an indexing expression, then the type of object reference - expression must be an array type, or the Record type; a compile-time error occurs otherwise. -tags: [compile-only, negative] ----*/ - -function refNullish(): Record | undefined { - let res: Record | undefined = { "magic": 42 } - return res -} - -function main() { - arktest.assertTrue(refNullish()["magic"] == 42) // CTE expected -} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1e.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1e.ets deleted file mode 100644 index 577049d045..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1e.ets +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: >- - If no ‘?.’ is present in an indexing expression, then the type of object reference - expression must be an array type, or the Record type; a compile-time error occurs otherwise. -tags: [compile-only, negative] ----*/ - -function refNullish(): Record | undefined { - let res: Record | undefined = undefined - return res -} - -function main() { - arktest.assertTrue(refNullish()["magic"] == 42) // CTE expected -} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3b.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3b.ets deleted file mode 100644 index 54a6c374a5..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3b.ets +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: >- - The object reference expression must be checked to evaluate to nullish value; - if it does, then the entire indexing-Expression equals undefined. ----*/ - -function refNullish(): Record | undefined { - let res: Record | undefined = { "magic": 42 } - return res -} - -function main() { - arktest.assertTrue(refNullish()?.["magic"] == 42) -} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3d.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3d.ets deleted file mode 100644 index 02f5fefb63..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3d.ets +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: >- - The object reference expression must be checked to evaluate to nullish value; - if it does, then the entire indexing-Expression equals undefined. ----*/ - -function refNullish(): Record | undefined { - let res: Record | undefined = undefined - return res -} - -function main() { - arktest.assertTrue(refNullish()?.["magic"] === undefined) -} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1d.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access.ets similarity index 78% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1d.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access.ets index 78f8f67de9..51e0a4238e 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1d.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access.ets @@ -13,18 +13,19 @@ * limitations under the License. */ +{% for c in cases %} /*--- desc: >- If no ‘?.’ is present in an indexing expression, then the type of object reference expression must be an array type, or the Record type; a compile-time error occurs otherwise. -tags: [compile-only, negative] ---*/ -function refNullish(): Record | null { - let res: Record | null = null - return res +function refNullish(): Record | {{c.type}} { + let res: Record | {{c.type}} = {{c.value}}; + return res; } function main() { - arktest.assertTrue(refNullish()["magic"] == 42) // CTE expected + arktest.assertEQ(refNullish()?.["magic"], {{c.result}}); } +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access.params.yaml new file mode 100644 index 0000000000..595cd6af73 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access.params.yaml @@ -0,0 +1,19 @@ +# Copyright (c) 2021-2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { type: 'null', value: '{ "magic": 42 }', result: 42 } + - { type: 'undefined', value: '{ "magic": 42 }', result: 42 } + - { type: 'null', value: 'null', result: 'undefined' } + - { type: 'undefined', value: 'undefined', result: 'undefined' } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1b.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access_n.ets similarity index 79% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1b.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access_n.ets index 9d64f0fe76..2fcad76aee 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx1b.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access_n.ets @@ -13,6 +13,7 @@ * limitations under the License. */ +{% for c in cases %} /*--- desc: >- If no ‘?.’ is present in an indexing expression, then the type of object reference @@ -20,11 +21,12 @@ desc: >- tags: [compile-only, negative] ---*/ -function refNullish(): Record | null { - let res: Record | null = { "magic": 42 } - return res +function refNullish(): Record | {{c.type}} { + let res: Record | {{c.type}} = {{c.value}}; + return res; } function main() { - arktest.assertTrue(refNullish()["magic"] == 42) // CTE expected + let a = refNullish()["magic"]; // CTE expected } +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access_n.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access_n.params.yaml new file mode 100644 index 0000000000..7e2c81e953 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/nullable_record_access_n.params.yaml @@ -0,0 +1,19 @@ +# Copyright (c) 2021-2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { type: 'null', value: '{ "magic": 42 }' } + - { type: 'undefined', value: '{ "magic": 42 }' } + - { type: 'null', value: 'null' } + - { type: 'undefined', value: 'undefined' } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record04.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record04.ets deleted file mode 100644 index a8fcaf73ea..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record04.ets +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - there are no restrictions on the index expression no. ----*/ - -let x: Record = { - 1: "hello", - 2: "buy", -}; - -function bar(n: number): string { - let s = x[n]; - if (s == undefined) { return "no"; } - return s!; -} - -function main(): void { - arktest.assertEQ(bar(3), "no"); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record07.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record07.ets deleted file mode 100644 index 4b6a40e84d..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record07.ets +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - key is of other types. ----*/ - -type Keys = 'key1' | 'key2' | 'key3'; - -let x2: Record = { - 1: "hello", - 2: "buy", -}; - -function foo(n: number): string | undefined { - return x2[n]; -} - -function main(): void { - let x = foo(3); - arktest.assertEQ(x, undefined); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record08.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record08.ets deleted file mode 100644 index b0c5bcc5a2..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record08.ets +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - key is of other types. ----*/ - -type Keys = 'key1' | 'key2' | 'key3'; - -let x2: Record = { - 1: "hello", - 2: "buy", -}; - -function bar(n: number): string { - let s = x2[n]; - if (s == undefined) { return "no"; } - return s!; -} - -function main(): void { - let x = bar(3); - arktest.assertEQ(x, "no"); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record09.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record09.ets deleted file mode 100644 index c6fdd0b577..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record09.ets +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - record with dynamic keys. ----*/ - -let dynamicRecord: Record = { - "a": 1, - "b": 2, - "c": 3, -}; - -function getValue(key: string): number | undefined { - return dynamicRecord[key]; -} - -function main(): void { - arktest.assertEQ(getValue("a"), 1); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record10.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record10.ets deleted file mode 100644 index 06c17f5a68..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record10.ets +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - record with dynamic keys -undefined. ----*/ - -let dynamicRecord: Record = { - "a": 1, - "b": 2, - "c": 3, -}; - -function getValue(key: string): number | undefined { - return dynamicRecord[key]; -} - -function main(): void { - arktest.assertEQ(getValue("d"), undefined); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record11.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record11.ets deleted file mode 100644 index 2bccde9e62..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record11.ets +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - record with optional keys. ----*/ - -let optionalRecord: Record = { - "name": "Alice", - "age": "30", -}; - -function main(): void { - arktest.assertEQ(optionalRecord["name"], "Alice"); - arktest.assertEQ(optionalRecord["age"], "30"); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record12.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record12.ets deleted file mode 100644 index bb00cdf3ec..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record12.ets +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - record with optional keys - undefined. ----*/ - -let optionalRecord: Record = { - "name": "Alice", - "age": "30", -}; - -function main(): void { - arktest.assertEQ(optionalRecord["gender"], undefined); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record14.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record14.ets deleted file mode 100644 index 095a258bb4..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record14.ets +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - record and default values. ----*/ - -let recordWithDefaults: Record = { - "a": 1, - "b": 2, -}; - -function getValueOrDefault(key: string, defaultValue: number): number { - return recordWithDefaults[key] ?? defaultValue; -} - -function main(): void { - arktest.assertEQ(getValueOrDefault("c", 0), 0); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record15.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record15.ets deleted file mode 100644 index 07790377d9..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record15.ets +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - the keys of the record are number. ----*/ - -let numberRecord: Record = { - 1: "one", - 2: "two", - 3: "three", -}; - -function main(): void { - arktest.assertEQ(numberRecord[1], "one"); - arktest.assertEQ(numberRecord[2], "two"); - arktest.assertEQ(numberRecord[3], "three"); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record16.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record16.ets deleted file mode 100644 index 8f3decddf8..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record16.ets +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - the keys of the record are number. ----*/ - -let numberRecord: Record = { - 1: "one", - 2: "two", - 3: "three", -}; - -function main(): void { - arktest.assertEQ(numberRecord[4], undefined); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record18.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record18.ets deleted file mode 100644 index 648bff215c..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record18.ets +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - the keys of the record are dynamically generated. ----*/ - -let dynamicKeyRecord: Record = {}; - -function setValue(key: string, value: number) { - dynamicKeyRecord[key] = value; -} - -function main(): void { - setValue("key1", 10); - setValue("key2", 20); - arktest.assertEQ(dynamicKeyRecord["key3"], undefined); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record19.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record19.ets deleted file mode 100644 index 403a925a69..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record19.ets +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - record and default value filling. ----*/ - -let config: Record = { - "theme": "dark", - "language": "en", -}; - -function getConfigValue(key: string, defaultValue: string): string { - return config[key] ?? defaultValue; -} - -function main(): void { - arktest.assertEQ(getConfigValue("theme", "light"), "dark"); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record21.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record21.ets deleted file mode 100644 index b2c956f3dc..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record21.ets +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - type conversion of dynamic keys in a recordype conversion of dynamic keys in a record. -tags: [compile-only, positive] ----*/ - -let stringRecord: Record = { - "1": 10, - "2": 20, - "3": 30, -}; - -function main(): void { - let numberRecord: Record = {}; -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record22.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record22.ets deleted file mode 100644 index ae640c4824..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record22.ets +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - default values for optional keys in a Record. ----*/ - -let optionalRecord: Record = { - "a": 1, - "b": 2, -}; - -function getValueWithDefault(key: string, defaultValue: number): number { - return optionalRecord[key] ?? defaultValue; -} - -function main(): void { - arktest.assertEQ(getValueWithDefault("a", 0), 1); - arktest.assertEQ(getValueWithDefault("b", 0), 2); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record23.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record23.ets deleted file mode 100644 index 87a116bd8d..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record23.ets +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - default values for optional keys in a record. ----*/ - -let optionalRecord: Record = { - "a": 1, - "b": 2, -}; - -function getValueWithDefault(key: string, defaultValue: number): number { - return optionalRecord[key] ?? defaultValue; -} - -function main(): void { - arktest.assertEQ(getValueWithDefault("c", 0), 0); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record24.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record24.ets deleted file mode 100644 index ddfacc25e6..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record24.ets +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - record and default values for dynamic keys. ----*/ - -let dynamicRecord: Record = {}; - -function setValue(key: string, value: number) { - dynamicRecord[key] = value; -} - -function main(): void { - setValue("a", 1); - setValue("b", 2); - arktest.assertEQ(dynamicRecord["a"], 1); - arktest.assertEQ(dynamicRecord["b"], 2); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record25.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record25.ets deleted file mode 100644 index 373015ad89..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record25.ets +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - runtime valid indexing. ----*/ - -let scores: Record = { - 90: "A", - 80: "B", - 70: "C", -}; - -function main(): void { - let gradeA = scores[90]; - arktest.assertEQ(gradeA, "A"); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record26.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record26.ets deleted file mode 100644 index f4fb531dc2..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record26.ets +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - invalid index (key does not exist). ----*/ - -let scores: Record = { - 90: "A", - 80: "B", - 70: "C", -}; - -function main(): void { - let gradeD = scores[60]; - arktest.assertEQ(gradeD, undefined); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record27.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record27.ets deleted file mode 100644 index 6db97a7e4b..0000000000 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record27.ets +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/*--- -desc: Record index expression - runtime check. ----*/ - -let scores: Record = { - 90: "A", - 80: "B", - 70: "C", -}; - -function getGrade(score: number): string { - let grade = scores[score]; - return grade ?? "F"; -} - -function main(): void { - arktest.assertEQ(getGrade(90), "A"); - console.log(getGrade(60)); -} \ No newline at end of file diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3c.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_access_wrong_key_n.ets similarity index 48% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3c.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_access_wrong_key_n.ets index 26ed9af719..b39df3c52e 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3c.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_access_wrong_key_n.ets @@ -1,29 +1,36 @@ /* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ +{% for c in cases %} /*--- desc: >- - The object reference expression must be checked to evaluate to nullish value; - if it does, then the entire indexing-Expression equals undefined. + For a Record indexing, the index expression must be of type Key. +tags: [compile-only, negative] ---*/ -function refNullish(): Record | null { - let res: Record | null = null - return res +let r: Record<'11' | '12' | '13', {{c.type}}> = { + '11': 1, + '12': 2, + '13': 3 +} + +function foo(s: string): string { + return s + '2'; } function main() { - arktest.assertTrue(refNullish()?.["magic"] === undefined) + let a = r[{{c.call}}]; // CTE expected } +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_access_wrong_key_n.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_access_wrong_key_n.params.yaml new file mode 100644 index 0000000000..46861bafaa --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_access_wrong_key_n.params.yaml @@ -0,0 +1,19 @@ +# Copyright (c) 2024-2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { type: Short, call: 'foo("1")' } + - { type: Byte, call: '""' } + - { type: Int, call: '12' } + - { type: Int, call: '"14"' } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record28.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_cast_index.ets similarity index 75% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record28.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_cast_index.ets index ac655c72c9..ee621f6860 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record28.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_cast_index.ets @@ -13,20 +13,28 @@ * limitations under the License. */ +{% for c in cases %} /*--- desc: Record index expression - spread from a record cast from Object. ---*/ +let y: Record = { + "name": "Alice", + "age": "30" +} + function spreadFromObject(y: Record): Record { let x: Record = { ...y }; return x; } -function main(): void { - let y: Record = { "a": "Ali", "e": "Elif" }; +function getValue(key: string, defaultValue: string): string { let x = spreadFromObject(y); - - arktest.assertEQ(x.get("a"), "Ali"); - arktest.assertEQ(x.get("e"), "Elif"); + return x[key] ?? defaultValue; } +function main(): void { + let x = spreadFromObject(y); + arktest.assertEQ({{c.call}}, {{c.value}}); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_cast_index.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_cast_index.params.yaml new file mode 100644 index 0000000000..21d786ef6f --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_cast_index.params.yaml @@ -0,0 +1,22 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { call: 'x.get("name")', value: '"Alice"' } + - { call: 'x.get("age")', value: '"30"' } + - { call: 'x["name"]', value: '"Alice"' } + - { call: 'x["age"]', value: '"30"' } + - { call: 'x["gender"]', value: 'undefined' } + - { call: 'getValue("name", "none")', value: '"Alice"' } + - { call: 'getValue("id", "none")', value: '"none"' } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record06.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index.ets similarity index 73% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record06.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index.ets index 893ed5eee0..e15730ca06 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record06.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index.ets @@ -13,19 +13,21 @@ * limitations under the License. */ +{% for c in cases %} /*--- -desc: Record index expression - key is literal union type. -tags: [compile-only, negative] +desc: Record index expression - record union type index. ---*/ -type Keys = 'key1' | 'key2' | 'key3'; +enum Keys { k1, k2, k3 }; let x: Record = { - 'key1': 1, - 'key2': 2, - 'key3': 4, -}; + Keys.k1: 1; + Keys.k2: 2; + Keys.k3: 4; +} function main(): void { - let y = x["key4"]; -} \ No newline at end of file + let s = {{c.call}}; + arktest.assertEQ({{c.value}}, {{c.result}}); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index.params.yaml new file mode 100644 index 0000000000..fff5e21ebc --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index.params.yaml @@ -0,0 +1,20 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the License); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an AS IS BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { call: 0, value: 'x[Keys.k1]', result: 1 } + - { call: 0, value: 'x[Keys.k2]', result: 2 } + - { call: 0, value: 'x[Keys.k3]', result: 4 } + - { call: 'x[Keys.k1]', value: 's', result: 1 } + - { call: 'x[Keys.k3]', value: 's', result: 4 } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record02.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access.ets similarity index 68% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record02.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access.ets index 720c2cb299..dbfcdcaa1f 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record02.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access.ets @@ -13,18 +13,22 @@ * limitations under the License. */ +{% for c in cases %} /*--- -desc: Record index expression - if the index expression is not a valid literal, a compile-time error will occur. -tags: [compile-only, negative] +desc: >- + The record indexing expression can be written by using a field access notation + if its index expression is formed as an identifier of type string. ---*/ -type Keys = 'key1' | 'key2' | 'key3'; +type Keys = 'key1' | 'key2' | 'key3' -function main(): void { - let x: Record = { +let x: Record = { 'key1': 1, 'key2': 2, 'key3': 4, - }; - console.log(x['key4']); -} \ No newline at end of file +} + +function main(): void { + arktest.assertEQ(x.{{c.index}}, {{c.value}}); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access.params.yaml new file mode 100644 index 0000000000..f0f6c0e9dc --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access.params.yaml @@ -0,0 +1,19 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { index: 'key1', value: 1 } + - { index: 'key2', value: 2 } + - { index: 'key3', value: 4 } + diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record01.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set.ets similarity index 69% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record01.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set.ets index be54fa461a..40d96bbb9a 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record01.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set.ets @@ -13,19 +13,23 @@ * limitations under the License. */ +{% for c in cases %} /*--- -desc: Record index expression - the type Key is a union type that only contains literal types. +desc: >- + The record indexing expression can be written by using a field access notation + if its index expression is formed as an identifier of type string. ---*/ type Keys = 'key1' | 'key2' | 'key3'; -function main(): void { - let x: Record = { +let x: Record = { 'key1': 1, 'key2': 2, - 'key3': 4, - }; + 'key3': 4 +} - let y = x['key2']; - arktest.assertEQ(y, 2); -} \ No newline at end of file +function main(): void { + x.{{c.index}} = 42; + arktest.assertEQ(x[{{c.index}}], 42); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set.params.yaml new file mode 100644 index 0000000000..77ecceefbe --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set.params.yaml @@ -0,0 +1,18 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - index: 'key1' + - index: 'key2' + - index: 'key3' diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record03.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_number_index.ets similarity index 65% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record03.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_number_index.ets index 9bb723dd35..b918745c98 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record03.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_number_index.ets @@ -13,20 +13,28 @@ * limitations under the License. */ +{% for c in cases %} /*--- -desc: Record index expression - there are no restrictions on the index expression undefined. +desc: An index expression has no restriction. The result of an indexing expression is of type Value | undefined. ---*/ let x: Record = { - 1: "hello", - 2: "buy", -}; + 1: "hello", + 2: "buy" +} function foo(n: number): string | undefined { - return x[n]; + return x[n]; +} + +function bar(n: number): string { + let s = x[n]; + if (s == undefined) { return "no" }; + return s!; } function main(): void { - let x = foo(3); - arktest.assertEQ(x, undefined); -} \ No newline at end of file + let s = {{c.call}}; + arktest.assertEQ({{c.value}}, {{c.result}}); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_number_index.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_number_index.params.yaml new file mode 100644 index 0000000000..39019dcf03 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_number_index.params.yaml @@ -0,0 +1,28 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { call: 0, value: 'x[1]', result: '"hello"' } + - { call: 0, value: 'x[2]', result: '"buy"' } + - { call: 0, value: 'x[0]', result: 'undefined' } + - { call: 0, value: 'bar(1)', result: '"hello"' } + - { call: 0, value: 'bar(10)', result: '"no"' } + - { call: 0, value: 'foo(2)', result: '"buy"' } + - { call: 0, value: 'foo(5)', result: 'undefined' } + - { call: 'x[2]', value: 's', result: '"buy"' } + - { call: 'x[3]', value: 's', result: 'undefined' } + - { call: 'bar(1)', value: 's', result: '"hello"' } + - { call: 'bar(8)', value: 's', result: '"no"' } + - { call: 'foo(2)', value: 's', result: '"buy"' } + - { call: 'foo(6)', value: 's', result: 'undefined' } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3a.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_set_wrong_key_n.ets similarity index 47% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3a.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_set_wrong_key_n.ets index 581cd4e376..be44a175da 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/indx3a.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_set_wrong_key_n.ets @@ -1,29 +1,36 @@ /* - * Copyright (c) 2021-2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ +{% for c in cases %} /*--- desc: >- - The object reference expression must be checked to evaluate to nullish value; - if it does, then the entire indexing-Expression equals undefined. + For a Record indexing, the index expression must be of type Key. +tags: [compile-only, negative] ---*/ -function refNullish(): Record | null { - let res: Record | null = { "magic": 42 } - return res +let r: Record<'11' | '12' | '13', {{c.type}}> = { + '11': 1, + '12': 2, + '13': 3 +} + +function foo(s: string): string { + return s + '2'; } function main() { - arktest.assertTrue(refNullish()?.["magic"] == 42) + r[{{c.call}}] = 10; // CTE expected } +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_set_wrong_key_n.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_set_wrong_key_n.params.yaml new file mode 100644 index 0000000000..46861bafaa --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_set_wrong_key_n.params.yaml @@ -0,0 +1,19 @@ +# Copyright (c) 2024-2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { type: Short, call: 'foo("1")' } + - { type: Byte, call: '""' } + - { type: Int, call: '12' } + - { type: Int, call: '"14"' } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record13.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_string_index.ets similarity index 71% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record13.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_string_index.ets index c63c0297a8..aed2cd31e7 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record13.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_string_index.ets @@ -13,19 +13,26 @@ * limitations under the License. */ +{% for c in cases %} /*--- -desc: Record index expression - record and default values. +desc: Record index expression - record string-number. ---*/ -let recordWithDefaults: Record = { +let rec: Record = { "a": 1, "b": 2, + "c": 3, }; +function getValue(key: string): number | undefined { + return rec[key]; +} + function getValueOrDefault(key: string, defaultValue: number): number { - return recordWithDefaults[key] ?? defaultValue; + return rec[key] ?? defaultValue; } function main(): void { - arktest.assertEQ(getValueOrDefault("a", 0), 1); -} \ No newline at end of file + arktest.assertEQ({{c.call}}, {{c.value}}); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_string_index.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_string_index.params.yaml new file mode 100644 index 0000000000..1270dc6ff9 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_string_index.params.yaml @@ -0,0 +1,24 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { call: 'rec["a"]', value: 1 } + - { call: 'rec["b"]', value: 2 } + - { call: 'rec["c"]', value: 3 } + - { call: 'rec["d"]', value: undefined } + - { call: 'getValue("c")', value: 3 } + - { call: 'getValue("d")', value: undefined } + - { call: 'getValueOrDefault("a", 0)', value: 1 } + - { call: 'getValueOrDefault("b", 0)', value: 2 } + - { call: 'getValueOrDefault("e", 0)', value: 0 } diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record05.ets b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_union_type_index.ets similarity index 82% rename from static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record05.ets rename to static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_union_type_index.ets index 7a99385971..556b30388e 100644 --- a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record05.ets +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_union_type_index.ets @@ -13,8 +13,9 @@ * limitations under the License. */ +{% for c in cases %} /*--- -desc: Record index expression - literal union type. +desc: Record index expression - record union type index. ---*/ type Keys = 'key1' | 'key2' | 'key3'; @@ -23,9 +24,10 @@ let x: Record = { 'key1': 1, 'key2': 2, 'key3': 4, -}; +} function main(): void { - let y = x['key2']; - arktest.assertEQ(y, 2); -} \ No newline at end of file + let s = {{c.call}}; + arktest.assertEQ({{c.value}}, {{c.result}}); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_union_type_index.params.yaml b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_union_type_index.params.yaml new file mode 100644 index 0000000000..8ff9d0ed3a --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/07.expressions/12.indexing_expression/03.record_indexing_expression/record_union_type_index.params.yaml @@ -0,0 +1,20 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +cases: + - { call: 0, value: 'x["key1"]', result: 1 } + - { call: 0, value: 'x["key2"]', result: 2 } + - { call: 0, value: 'x["key3"]', result: 4 } + - { call: 'x["key1"]', value: 's', result: 1 } + - { call: 'x["key3"]', value: 's', result: 4 } diff --git a/static_core/plugins/ets/tests/test-lists/ets-cts/ets-cts-ignored.txt b/static_core/plugins/ets/tests/test-lists/ets-cts/ets-cts-ignored.txt index beca84746e..249b9a9197 100644 --- a/static_core/plugins/ets/tests/test-lists/ets-cts/ets-cts-ignored.txt +++ b/static_core/plugins/ets/tests/test-lists/ets-cts/ets-cts-ignored.txt @@ -1068,6 +1068,21 @@ #28526 07.expressions/11.function_call_expression/fcall_20.ets +#28679 +07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index_0.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index_1.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index_2.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index_3.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_enum_index_4.ets + +#29719 +07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access_0.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access_1.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_access_2.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set_0.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set_1.ets +07.expressions/12.indexing_expression/03.record_indexing_expression/record_field_set_2.ets + #29202 CTE expected in `instanceof T` 07.expressions/15.instanceof_expression/instanceof-cte_0.ets @@ -2679,7 +2694,6 @@ 06.contexts_and_conversions/05.implicit_conversions/03.constant_narrowing_integer_conversion/call_func/call-func-n3_7.ets 06.contexts_and_conversions/05.implicit_conversions/03.constant_narrowing_integer_conversion/call_lmbd/call-lmbd-n3_7.ets 06.contexts_and_conversions/05.implicit_conversions/03.constant_narrowing_integer_conversion/call_meth/call-meth-n3_7.ets -07.expressions/12.indexing_expression/03.record_indexing_expression/ind_1.ets 07.expressions/25.equality_expressions/eq_neg_101.ets 07.expressions/25.equality_expressions/eq_neg_146.ets 07.expressions/25.equality_expressions/eq_neg_148.ets @@ -2759,8 +2773,6 @@ 09.classes/07.method_declarations/05.overriding_methods/override_method_with_default_params_12.ets # 21255 no type Exception -07.expressions/12.indexing_expression/01.array_indexing_expression/array_index3_n_0.ets -07.expressions/12.indexing_expression/01.array_indexing_expression/array_index3_n_1.ets 09.classes/07.method_declarations/05.overriding_methods/override_method_declaration_with_keywords_4.ets # 29337 no CTE in ambiguous narrowing -- Gitee