diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_1.ets b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..209cc3424e221c6669b449cf1d1da9a6a09975e1 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_1.ets @@ -0,0 +1,26 @@ +/*--- +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. +---*/ + +{% for c in cases %} +/*--- +desc: Any else corresponds to the nearest preceding if of an if statement. +---*/ + +function main(): void { + if ({{c.cond1}}) + if ({{c.cond2}}) arktest.assertTrue({{c.bool1}}) + else arktest.assertTrue({{c.bool2}}) // Executes only if: Cond1 && !Cond2 +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_1.params.yaml b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_1.params.yaml new file mode 100644 index 0000000000000000000000000000000000000000..333c03d4fa94346619a7a15f85b411f48e465893 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_1.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: + - { cond1: 'true', cond2: 'true', bool1: 'true', bool2: 'false' } + - { cond1: 'true', cond2: 'false', bool1: 'false', bool2: 'true' } + - { cond1: 'false', cond2: 'true', bool1: 'false', bool2: 'false' } + - { cond1: 'false', cond2: 'false', bool1: 'false', bool2: 'false' } diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_2.ets b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..6ce4d0e9027dbb1c45e433aa0c8dc0f1cf6e7fbb --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_2.ets @@ -0,0 +1,27 @@ +/*--- +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. +---*/ + +{% for c in cases %} +/*--- +desc: A Block can be used to combine the else part with the initial if. +---*/ + +function main(): void { + if ({{c.cond1}}) { + if ({{c.cond2}}) arktest.assertTrue({{c.bool1}}); + } + else arktest.assertTrue({{c.bool2}}); // Executes if: !Cond1 +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_2.params.yaml b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_2.params.yaml new file mode 100644 index 0000000000000000000000000000000000000000..32569fd75b388b8266074f54ce41bd182329d0cb --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_2.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: + - { cond1: 'true', cond2: 'true', bool1: 'true', bool2: 'false' } + - { cond1: 'true', cond2: 'false', bool1: 'false', bool2: 'false' } + - { cond1: 'false', cond2: 'true', bool1: 'false', bool2: 'true' } + - { cond1: 'false', cond2: 'false', bool1: 'false', bool2: 'true' } diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3.ets b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3.ets new file mode 100644 index 0000000000000000000000000000000000000000..0223fa8bd66c3dc7a6491b32fea9238344635d5b --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3.ets @@ -0,0 +1,37 @@ +/*--- +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. +---*/ + +{% for c in cases %} +/*--- +desc: >- + If thenStatement or elseStatement is any kind of a statement but not a block, + then no block scope is created for such a statement. +---*/ + +function main(): void { + if ({{c.cond}}) let x: number = 1; + x = 2; // OK + + if ({{c.cond}}) { + let x: number = 10; // OK, then-block scope + let y: number = x; + } + else { + let x: number = 20; // OK, no conflict, else-block scope + } + + arktest.assertEQ(x, 2); +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3.params.yaml b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3.params.yaml new file mode 100644 index 0000000000000000000000000000000000000000..61823acc9cc49cc430a4cc8760a72bbe31a93998 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3.params.yaml @@ -0,0 +1,17 @@ +# 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: + - cond: 'true' + - cond: 'false' diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3_negative.ets b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3_negative.ets new file mode 100644 index 0000000000000000000000000000000000000000..5a86521e2555ab10550ceac54df424087f268a16 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3_negative.ets @@ -0,0 +1,40 @@ +/*--- +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. +---*/ + +{% for c in cases %} +/*--- +desc: >- + If thenStatement or elseStatement is any kind of a statement but not a block, + then no block scope is created for such a statement. +tags: [compile-only, negative] +---*/ + +function main(): void { + if (true) let x: number = 1; + x = 2; // OK + + if (false) { + let x: number = 10; // OK, then-block scope + let y: number = x; + } + else { + let x: number = 20; // OK, no conflict, else-block scope + {{c.cte1}}; + } + + arktest.assertEQ(x, 2); + {{c.cte2}} +} +{% endfor %} diff --git a/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3_negative.params.yaml b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3_negative.params.yaml new file mode 100644 index 0000000000000000000000000000000000000000..3f1822c87a8f93d128aec8163adcc10f8094cd61 --- /dev/null +++ b/static_core/plugins/ets/tests/ets-templates/08.statements/05.if_statements/spec_if_3_negative.params.yaml @@ -0,0 +1,17 @@ +# 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: + - { cte1: 'y = x; // CTE, no y in scope', cte2: '' } + - { cte1: '', cte2: 'x = y; // CTE, y unknown' }