diff --git a/tests/jballerina-debugger-integration-test/src/test/java/org/ballerinalang/debugger/test/adapter/variables/VariableVisibilityTest.java b/tests/jballerina-debugger-integration-test/src/test/java/org/ballerinalang/debugger/test/adapter/variables/VariableVisibilityTest.java index 9db72a68f1fb..e9835ebd76fe 100644 --- a/tests/jballerina-debugger-integration-test/src/test/java/org/ballerinalang/debugger/test/adapter/variables/VariableVisibilityTest.java +++ b/tests/jballerina-debugger-integration-test/src/test/java/org/ballerinalang/debugger/test/adapter/variables/VariableVisibilityTest.java @@ -35,6 +35,7 @@ import java.util.Map; import static org.ballerinalang.debugger.test.utils.DebugTestRunner.VariableScope; + /** * Test class for variable visibility. */ @@ -48,13 +49,14 @@ public class VariableVisibilityTest extends BaseTestCase { @Override @BeforeClass public void setup() { - String testProjectName = "variable-tests"; - String testModuleFileName = "main.bal"; - debugTestRunner = new DebugTestRunner(testProjectName, testModuleFileName, true); } @Test(description = "Variable visibility test at the beginning(first line) of the main() method") public void initialVariableVisibilityTest() throws BallerinaTestException { + String testProjectName = "variable-tests"; + String testModuleFileName = "main.bal"; + debugTestRunner = new DebugTestRunner(testProjectName, testModuleFileName, true); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 123)); debugTestRunner.initDebugSession(DebugUtils.DebuggeeExecutionKind.RUN); debugHitInfo = debugTestRunner.waitForDebugHit(25000); @@ -70,6 +72,10 @@ public void initialVariableVisibilityTest() throws BallerinaTestException { @Test(description = "Variable visibility test in the middle of the main() method for a new variable") public void newVariableVisibilityTest() throws BallerinaTestException { + String testProjectName = "variable-tests"; + String testModuleFileName = "main.bal"; + debugTestRunner = new DebugTestRunner(testProjectName, testModuleFileName, true); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 245)); debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 316)); debugTestRunner.initDebugSession(DebugUtils.DebuggeeExecutionKind.RUN); @@ -105,6 +111,10 @@ public void newVariableVisibilityTest() throws BallerinaTestException { @Test(description = "Variable visibility test in control flows") public void controlFlowVariableVisibilityTest() throws BallerinaTestException { + String testProjectName = "variable-tests"; + String testModuleFileName = "main.bal"; + debugTestRunner = new DebugTestRunner(testProjectName, testModuleFileName, true); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 266)); debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 270)); debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 277)); @@ -177,6 +187,10 @@ public void controlFlowVariableVisibilityTest() throws BallerinaTestException { @Test(description = "Variable visibility test for global variables") public void globalVariableVisibilityTest() throws BallerinaTestException { + String testProjectName = "variable-tests"; + String testModuleFileName = "main.bal"; + debugTestRunner = new DebugTestRunner(testProjectName, testModuleFileName, true); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 352)); debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 327)); debugTestRunner.initDebugSession(DebugUtils.DebuggeeExecutionKind.RUN); @@ -215,6 +229,10 @@ public void globalVariableVisibilityTest() throws BallerinaTestException { @Test(description = "Variable visibility test for local variables at the last line of main() method") public void localVariableVisibilityTest() throws BallerinaTestException { + String testProjectName = "variable-tests"; + String testModuleFileName = "main.bal"; + debugTestRunner = new DebugTestRunner(testProjectName, testModuleFileName, true); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 327)); debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 360)); debugTestRunner.initDebugSession(DebugUtils.DebuggeeExecutionKind.RUN); @@ -339,6 +357,10 @@ public void localVariableVisibilityTest() throws BallerinaTestException { @Test(enabled = false, description = "Child variable visibility test for local variables at the last line of main" + "() method") public void localVariableChildrenVisibilityTest() throws BallerinaTestException { + String testProjectName = "variable-tests"; + String testModuleFileName = "main.bal"; + debugTestRunner = new DebugTestRunner(testProjectName, testModuleFileName, true); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 327)); debugTestRunner.initDebugSession(DebugUtils.DebuggeeExecutionKind.RUN); debugHitInfo = debugTestRunner.waitForDebugHit(25000); @@ -352,18 +374,18 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException // xml attributes child variable visibility test Map xmlAttributesChildVariables = - debugTestRunner.fetchChildVariables(xmlChildVariables.get("attributes")); + debugTestRunner.fetchChildVariables(xmlChildVariables.get("attributes")); debugTestRunner.assertVariable(xmlAttributesChildVariables, "gender", "\"male\"", "string"); // xml children variable visibility test Map xmlChildrenVariables = - debugTestRunner.fetchChildVariables(xmlChildVariables.get("children")); + debugTestRunner.fetchChildVariables(xmlChildVariables.get("children")); debugTestRunner.assertVariable(xmlChildrenVariables, "[0]", "XMLElement", "xml"); debugTestRunner.assertVariable(xmlChildrenVariables, "[1]", "XMLElement", "xml"); // xml grand children variable visibility test Map xmlGrandChildrenVariables = - debugTestRunner.fetchChildVariables(xmlChildrenVariables.get("[0]")); + debugTestRunner.fetchChildVariables(xmlChildrenVariables.get("[0]")); debugTestRunner.assertVariable(xmlGrandChildrenVariables, "children", "XMLSequence (size = 1)", "xml"); // array child variable visibility test @@ -391,7 +413,7 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException // record child variable visibility test (Student record) Map studentRecordChildVariables = - debugTestRunner.fetchChildVariables(localVariables.get("recordVar")); + debugTestRunner.fetchChildVariables(localVariables.get("recordVar")); debugTestRunner.assertVariable(studentRecordChildVariables, "1st_name", "\"John Doe\"", "string"); debugTestRunner.assertVariable(studentRecordChildVariables, "grades", "Grades", "record"); debugTestRunner.assertVariable(studentRecordChildVariables, "Ȧɢέ_ /:@[`{~π", "20", "int"); @@ -399,7 +421,7 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException // record child variable visibility test (Grades record) Map gradesChildVariables = - debugTestRunner.fetchChildVariables(studentRecordChildVariables.get("grades")); + debugTestRunner.fetchChildVariables(studentRecordChildVariables.get("grades")); debugTestRunner.assertVariable(gradesChildVariables, "chemistry", "65", "int"); debugTestRunner.assertVariable(gradesChildVariables, "maths", "80", "int"); debugTestRunner.assertVariable(gradesChildVariables, "physics", "75", "int"); @@ -407,7 +429,7 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException // anonymous record child variable visibility test Map recordChildVariables = - debugTestRunner.fetchChildVariables(localVariables.get("anonRecord")); + debugTestRunner.fetchChildVariables(localVariables.get("anonRecord")); debugTestRunner.assertVariable(recordChildVariables, "city", "\"London\"", "string"); debugTestRunner.assertVariable(recordChildVariables, "country", "\"UK\"", "string"); @@ -418,18 +440,18 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException // error details child variable visibility test Map errorDetailsChildVariables = - debugTestRunner.fetchChildVariables(errorChildVariables.get("details")); + debugTestRunner.fetchChildVariables(errorChildVariables.get("details")); debugTestRunner.assertVariable(errorDetailsChildVariables, "message", "\"Simple error occurred\"", "string"); // future child variable visibility test Map futureChildVariables = - debugTestRunner.fetchChildVariables(localVariables.get("futureVar")); + debugTestRunner.fetchChildVariables(localVariables.get("futureVar")); debugTestRunner.assertVariable(futureChildVariables, "isDone", "true", "boolean"); debugTestRunner.assertVariable(futureChildVariables, "result", "90", "int"); // object child variable visibility test (Person object) Map personObjectChildVariables = - debugTestRunner.fetchChildVariables(localVariables.get("objectVar")); + debugTestRunner.fetchChildVariables(localVariables.get("objectVar")); debugTestRunner.assertVariable(personObjectChildVariables, "1st_name", "\"John\"", "string"); debugTestRunner.assertVariable(personObjectChildVariables, "address", "\"No 20, Palm grove\"", "string"); debugTestRunner.assertVariable(personObjectChildVariables, "parent", "()", "nil"); @@ -438,7 +460,7 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException // anonymous object child variable visibility test (AnonPerson object) Map anonObjectChildVariables = - debugTestRunner.fetchChildVariables(localVariables.get("anonObjectVar")); + debugTestRunner.fetchChildVariables(localVariables.get("anonObjectVar")); debugTestRunner.assertVariable(anonObjectChildVariables, "1st_name", "\"John\"", "string"); debugTestRunner.assertVariable(anonObjectChildVariables, "address", "\"No 20, Palm grove\"", "string"); debugTestRunner.assertVariable(anonObjectChildVariables, "parent", "()", "nil"); @@ -459,21 +481,21 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException // table with key child variable visibility test Map tableWithKeyChildVariables = - debugTestRunner.fetchChildVariables(localVariables.get("tableWithKeyVar")); + debugTestRunner.fetchChildVariables(localVariables.get("tableWithKeyVar")); debugTestRunner.assertVariable(tableWithKeyChildVariables, "[0]", "Employee", "record"); debugTestRunner.assertVariable(tableWithKeyChildVariables, "[1]", "Employee", "record"); debugTestRunner.assertVariable(tableWithKeyChildVariables, "[2]", "Employee", "record"); // table without key child variable visibility test Map tableWithoutKeyChildVariables = - debugTestRunner.fetchChildVariables(localVariables.get("tableWithoutKeyVar")); + debugTestRunner.fetchChildVariables(localVariables.get("tableWithoutKeyVar")); debugTestRunner.assertVariable(tableWithoutKeyChildVariables, "[0]", "Employee", "record"); debugTestRunner.assertVariable(tableWithoutKeyChildVariables, "[1]", "Employee", "record"); debugTestRunner.assertVariable(tableWithoutKeyChildVariables, "[2]", "Employee", "record"); // service child variable visibility test Map serviceChildVariables = - debugTestRunner.fetchChildVariables(localVariables.get("serviceVar")); + debugTestRunner.fetchChildVariables(localVariables.get("serviceVar")); debugTestRunner.assertVariable(serviceChildVariables, "i", "5", "int"); } @@ -550,6 +572,68 @@ public void workerVariableVisibilityTest() throws BallerinaTestException { } } + @Test(description = "Binding pattern variables related visibility test") + public void bindingPatternVariableVisibilityTest() throws BallerinaTestException { + String testProjectName = "variable-tests-2"; + String testModuleFileName = "main.bal"; + debugTestRunner = new DebugTestRunner(testProjectName, testModuleFileName, true); + + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 35)); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 40)); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 43)); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 46)); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 49)); + debugTestRunner.addBreakPoint(new BallerinaTestDebugPoint(debugTestRunner.testEntryFilePath, 80)); + + debugTestRunner.initDebugSession(DebugUtils.DebuggeeExecutionKind.RUN); + Pair debugHitInfo = debugTestRunner.waitForDebugHit(25000); + + // simple binding pattern variables + localVariables = debugTestRunner.fetchVariables(debugHitInfo.getRight(), DebugTestRunner.VariableScope.LOCAL); + debugTestRunner.assertVariable(localVariables, "profession", "\"Software Engineer\"", "string"); + + // list binding pattern variables + debugTestRunner.resumeProgram(debugHitInfo.getRight(), DebugTestRunner.DebugResumeKind.NEXT_BREAKPOINT); + debugHitInfo = debugTestRunner.waitForDebugHit(10000); + // TODO: enable after fixing runtime issue https://github.com/ballerina-platform/ballerina-lang/issues/43623 +// localVariables = debugTestRunner.fetchVariables(debugHitInfo.getRight(), DebugTestRunner.VariableScope.LOCAL); +// debugTestRunner.assertVariable(localVariables, "id", "1234", "int"); +// debugTestRunner.assertVariable(localVariables, "firstName", "\"John Doe\"", "string"); + + // mapping binding pattern variables + debugTestRunner.resumeProgram(debugHitInfo.getRight(), DebugTestRunner.DebugResumeKind.NEXT_BREAKPOINT); + debugHitInfo = debugTestRunner.waitForDebugHit(10000); + // TODO: enable after fixing runtime issue https://github.com/ballerina-platform/ballerina-lang/issues/43623 +// localVariables = debugTestRunner.fetchVariables(debugHitInfo.getRight(), DebugTestRunner.VariableScope.LOCAL); +// debugTestRunner.assertVariable(localVariables, "givenName", "\"Anne\"", "string"); +// debugTestRunner.assertVariable(localVariables, "surName", "\"Frank\"", "string"); + + // error binding pattern variables + debugTestRunner.resumeProgram(debugHitInfo.getRight(), DebugTestRunner.DebugResumeKind.NEXT_BREAKPOINT); + debugHitInfo = debugTestRunner.waitForDebugHit(10000); + // TODO: enable after fixing runtime issue https://github.com/ballerina-platform/ballerina-lang/issues/43623 +// localVariables = debugTestRunner.fetchVariables(debugHitInfo.getRight(), DebugTestRunner.VariableScope.LOCAL); +// debugTestRunner.assertVariable(localVariables, "cause", "\"Database Error\"", "error"); +// debugTestRunner.assertVariable(localVariables, "code", "20", "int"); +// debugTestRunner.assertVariable(localVariables, "reason", "\"deadlock condition\"", "string"); + + // list binding pattern inside foreach statement + debugTestRunner.resumeProgram(debugHitInfo.getRight(), DebugTestRunner.DebugResumeKind.NEXT_BREAKPOINT); + debugHitInfo = debugTestRunner.waitForDebugHit(10000); + localVariables = debugTestRunner.fetchVariables(debugHitInfo.getRight(), DebugTestRunner.VariableScope.LOCAL); + debugTestRunner.assertVariable(localVariables, "name", "\"John\"", "string"); + debugTestRunner.assertVariable(localVariables, "age", "30", "int"); + + // list binding patterns inside match statement + debugTestRunner.resumeProgram(debugHitInfo.getRight(), DebugTestRunner.DebugResumeKind.NEXT_BREAKPOINT); + debugHitInfo = debugTestRunner.waitForDebugHit(10000); + // TODO: enable after fixing runtime issue https://github.com/ballerina-platform/ballerina-lang/issues/43623 +// localVariables = debugTestRunner.fetchVariables(debugHitInfo.getRight(), DebugTestRunner.VariableScope.LOCAL); +// debugTestRunner.assertVariable(localVariables, "remove", "Remove", "string"); +// debugTestRunner.assertVariable(localVariables, "all", "*", "string"); +// debugTestRunner.assertVariable(localVariables, "isDir", "true", "boolean"); + } + @Override @AfterMethod(alwaysRun = true) public void cleanUp() { diff --git a/tests/jballerina-debugger-integration-test/src/test/resources/project-based-tests/variable-tests-2/Ballerina.toml b/tests/jballerina-debugger-integration-test/src/test/resources/project-based-tests/variable-tests-2/Ballerina.toml new file mode 100644 index 000000000000..7352e4440b5f --- /dev/null +++ b/tests/jballerina-debugger-integration-test/src/test/resources/project-based-tests/variable-tests-2/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "debug_test_resources" +name = "variable_tests_2" +version = "0.1.0" diff --git a/tests/jballerina-debugger-integration-test/src/test/resources/project-based-tests/variable-tests-2/main.bal b/tests/jballerina-debugger-integration-test/src/test/resources/project-based-tests/variable-tests-2/main.bal new file mode 100644 index 000000000000..12fefe845eb4 --- /dev/null +++ b/tests/jballerina-debugger-integration-test/src/test/resources/project-based-tests/variable-tests-2/main.bal @@ -0,0 +1,99 @@ +// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org). +// +// WSO2 LLC. 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. + +type Person record {| + int id; + string fname; + string lname; +|}; + +type SampleErrorData record {| + int code; + string reason; +|}; + +type SampleError error; + +public function main() { + // 1. simple binding pattern + var profession = "Software Engineer"; + + // 2. list binding pattern + [int, [string, string]] [id, [firstName, _]] = getDetails(); + + // 3. mapping binding pattern + string givenName; + string surname; + {fname: givenName, lname: surname} = getPerson(); + + // 4. error binding pattern + var error(_, cause, code = code, reason = reason) = getSampleError(); + + // 5. binding patterns inside a foreach statement + string names = ""; + [string, int][] personInfoList = getPersonInfo(); + foreach [string, int] [name, age] in personInfoList { + names += " " + name; + } + + // 6. binding patterns inside a match statement + matchCommand(["Remove", "*", true]); +} + +function getDetails() returns [int, [string, string]] { + return [ + 1234, + ["John", "Doe"] + ]; +} + +function getPerson() returns Person { + Person person = {id: 1001, fname: "Anne", lname: "Frank"}; + return person; +} + +function getSampleError() returns SampleError { + return error("Transaction Failure", error("Database Error"), code = 20, reason = "deadlock condition"); +} + +function matchCommand(any commands) { + match commands { + var [show] => { + string name = "show"; + } + // The list binding pattern below binds lists that contain three list items + // where the third element in the list is the boolean value `true`. + var [remove, all, isDir] if isDir is true => { + string name = "remove"; + } + // The list binding pattern below binds lists that contain three list items. + var [remove, all, _] => { + string name = "remove"; + } + // The list binding pattern below binds lists that contain two list items, + // in which the second list item is also a list of two items. + var [copy, [file1, file2]] => { + string name = "copy"; + } + _ => { + string name = "unknown"; + } + } +} + +function getPersonInfo() returns [string, int][] { + return [["John", 30]]; +}