From afce44b60aef58867c8184ffa308f7356667e3c4 Mon Sep 17 00:00:00 2001 From: gabilang Date: Wed, 8 May 2024 18:24:12 +0530 Subject: [PATCH 1/3] Fix no such field error for escaped identifiers --- .../ballerinalang/compiler/bir/BIRGen.java | 3 +- .../EscapedIdentifiersValidationTest.java | 50 +++++++++++++++++++ ...ionTest-validateEscapedIdentifiersTest.txt | 17 +++++++ ...ionTest-validateEscapedIdentifiersTest.txt | 17 +++++++ .../Ballerina.toml | 4 ++ .../validate-escaped-identifiers/main.bal | 21 ++++++++ .../tests/test_main.bal | 22 ++++++++ .../src/test/resources/testng.xml | 1 + 8 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/EscapedIdentifiersValidationTest.java create mode 100644 tests/testerina-integration-test/src/test/resources/command-outputs/unix/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt create mode 100644 tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt create mode 100644 tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/Ballerina.toml create mode 100644 tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/main.bal create mode 100644 tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/tests/test_main.bal diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 2af9c4655574..582cd6af163f 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -1936,7 +1936,8 @@ private BIRGlobalVariableDcl getVarRef(BLangPackageVarRef astPackageVarRefExpr) this.globalVarMap.put(symbol, globalVarDcl); } - if (!isInSamePackage(astPackageVarRefExpr.varSymbol, env.enclPkg.packageID)) { + if (!isInSamePackage(astPackageVarRefExpr.varSymbol, env.enclPkg.packageID) || + env.enclPkg.packageID.isTestPkg) { this.env.enclPkg.importedGlobalVarsDummyVarDcls.add(globalVarDcl); } return globalVarDcl; diff --git a/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/EscapedIdentifiersValidationTest.java b/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/EscapedIdentifiersValidationTest.java new file mode 100644 index 000000000000..3b877701a566 --- /dev/null +++ b/tests/testerina-integration-test/src/test/java/org/ballerinalang/testerina/test/EscapedIdentifiersValidationTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://wso2.com). + * + * 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. + */ + +package org.ballerinalang.testerina.test; + +import org.ballerinalang.test.context.BMainInstance; +import org.ballerinalang.test.context.BallerinaTestException; +import org.ballerinalang.testerina.test.utils.AssertionUtils; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.util.HashMap; + +/** + * Test class containing tests related to escaped identifiers validation. + */ +public class EscapedIdentifiersValidationTest extends BaseTestCase { + + private BMainInstance balClient; + private String projectPath; + + @BeforeClass + public void setup() throws BallerinaTestException { + balClient = new BMainInstance(balServer); + projectPath = projectBasedTestsPath.toString(); + } + + @Test + public void validateEscapedIdentifiersTest() throws BallerinaTestException, IOException { + String[] args = mergeCoverageArgs(new String[]{"validate-escaped-identifiers"}); + String output = balClient.runMainAndReadStdOut("test", args, + new HashMap<>(), projectPath, true); + AssertionUtils.assertOutput("EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt", + output); + } +} diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/unix/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/unix/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt new file mode 100644 index 000000000000..af64e0c9a56f --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/unix/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt @@ -0,0 +1,17 @@ +Compiling source + intg_tests/validate_escaped_identifiers:0.0.0 + +Running Tests with Coverage + + validate_escaped_identifiers + [pass] test_add + + + 1 passing + 0 failing + 0 skipped + + Test execution time :*****s + +Generating Test Report + validate-escaped-identifiers/target/report/test_results.json diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt new file mode 100644 index 000000000000..d811ba3a9f54 --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt @@ -0,0 +1,17 @@ +Compiling source + intg_tests/validate_escaped_identifiers:0.0.0 + +Running Tests with Coverage + + validate_escaped_identifiers + [pass] test_add + + + 1 passing + 0 failing + 0 skipped + + Test execution time :*****s + +Generating Test Report + validate-escaped-identifiers/target/report/test_results.json diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/Ballerina.toml b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/Ballerina.toml new file mode 100644 index 000000000000..2108f46e5625 --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "intg_tests" +name = "validate_escaped_identifiers" +version = "0.0.0" diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/main.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/main.bal new file mode 100644 index 000000000000..bda3e9870421 --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/main.bal @@ -0,0 +1,21 @@ +// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). +// +// WSO2 Inc. licenses this file to you 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. + +public int int\-a = 10; + +function add(int a, int b) returns int { + return a + b; +} diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/tests/test_main.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/tests/test_main.bal new file mode 100644 index 000000000000..73d2a2d2f0a0 --- /dev/null +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/tests/test_main.bal @@ -0,0 +1,22 @@ +// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). +// +// WSO2 Inc. licenses this file to you 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. + +import ballerina/test; + +@test:Config {} +function test_add() { + test:assertEquals(add(1, int\-a), 11); +} diff --git a/tests/testerina-integration-test/src/test/resources/testng.xml b/tests/testerina-integration-test/src/test/resources/testng.xml index 03620f5dd31b..3c731b522f9a 100644 --- a/tests/testerina-integration-test/src/test/resources/testng.xml +++ b/tests/testerina-integration-test/src/test/resources/testng.xml @@ -60,6 +60,7 @@ under the License. + From 31970fdca3974e354d663b54a9ee881dda38ea6b Mon Sep 17 00:00:00 2001 From: gabilang Date: Thu, 9 May 2024 08:55:35 +0530 Subject: [PATCH 2/3] Add more test cases --- ...ionTest-validateEscapedIdentifiersTest.txt | 3 ++- ...ionTest-validateEscapedIdentifiersTest.txt | 5 +++-- .../validate-escaped-identifiers/main.bal | 19 +++++++++++++++++++ .../tests/test_main.bal | 6 ++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/unix/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/unix/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt index af64e0c9a56f..33250ed64b66 100644 --- a/tests/testerina-integration-test/src/test/resources/command-outputs/unix/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/unix/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt @@ -4,10 +4,11 @@ Compiling source Running Tests with Coverage validate_escaped_identifiers + [pass] test [pass] test_add - 1 passing + 2 passing 0 failing 0 skipped diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt index d811ba3a9f54..2f52af0033c1 100644 --- a/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt @@ -4,14 +4,15 @@ Compiling source Running Tests with Coverage validate_escaped_identifiers + [pass] test [pass] test_add - 1 passing + 2 passing 0 failing 0 skipped Test execution time :*****s Generating Test Report - validate-escaped-identifiers/target/report/test_results.json + validate-escaped-identifiers\target\report\test_results.json diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/main.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/main.bal index bda3e9870421..4e7c23b06edc 100644 --- a/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/main.bal +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/main.bal @@ -16,6 +16,25 @@ public int int\-a = 10; +client class Client { + resource function get users\-all(string 'order = "asc") returns json { + return { + "users": [ + { + "name": "Mitchell", + "age": 30 + }, + { + "name": "Andrew", + "age": 35 + } + ] + }; + } +} + +final Client clientEP = new; + function add(int a, int b) returns int { return a + b; } diff --git a/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/tests/test_main.bal b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/tests/test_main.bal index 73d2a2d2f0a0..d99a266dd888 100644 --- a/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/tests/test_main.bal +++ b/tests/testerina-integration-test/src/test/resources/project-based-tests/validate-escaped-identifiers/tests/test_main.bal @@ -20,3 +20,9 @@ import ballerina/test; function test_add() { test:assertEquals(add(1, int\-a), 11); } + +@test:Config {} +function test() { + json res = clientEP->/users\-all(); + test:assertEquals(res, {"users":[{"name":"Mitchell","age":30},{"name":"Andrew","age":35}]}); +} From 242f19293e2591683ac69c8700ad003dd69b1e49 Mon Sep 17 00:00:00 2001 From: gabilang Date: Mon, 13 May 2024 08:53:40 +0530 Subject: [PATCH 3/3] Fix windows formatting issue --- ...dIdentifiersValidationTest-validateEscapedIdentifiersTest.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt index 2f52af0033c1..dace5c3d8ae7 100644 --- a/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt +++ b/tests/testerina-integration-test/src/test/resources/command-outputs/windows/EscapedIdentifiersValidationTest-validateEscapedIdentifiersTest.txt @@ -16,3 +16,4 @@ Running Tests with Coverage Generating Test Report validate-escaped-identifiers\target\report\test_results.json +