Skip to content

Commit

Permalink
Merge pull request #43624 from NipunaRanasinghe/master
Browse files Browse the repository at this point in the history
[Debugger] Add binding pattern variable visibility tests
  • Loading branch information
NipunaRanasinghe authored Nov 21, 2024
2 parents 2e71207 + 25415e4 commit 43e6737
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map;

import static org.ballerinalang.debugger.test.utils.DebugTestRunner.VariableScope;

/**
* Test class for variable visibility.
*/
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -352,18 +374,18 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException

// xml attributes child variable visibility test
Map<String, Variable> xmlAttributesChildVariables =
debugTestRunner.fetchChildVariables(xmlChildVariables.get("attributes"));
debugTestRunner.fetchChildVariables(xmlChildVariables.get("attributes"));
debugTestRunner.assertVariable(xmlAttributesChildVariables, "gender", "\"male\"", "string");

// xml children variable visibility test
Map<String, Variable> 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<String, Variable> xmlGrandChildrenVariables =
debugTestRunner.fetchChildVariables(xmlChildrenVariables.get("[0]"));
debugTestRunner.fetchChildVariables(xmlChildrenVariables.get("[0]"));
debugTestRunner.assertVariable(xmlGrandChildrenVariables, "children", "XMLSequence (size = 1)", "xml");

// array child variable visibility test
Expand Down Expand Up @@ -391,23 +413,23 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException

// record child variable visibility test (Student record)
Map<String, Variable> 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");
debugTestRunner.assertVariable(studentRecordChildVariables, "course", "\"ballerina\"", "string");

// record child variable visibility test (Grades record)
Map<String, Variable> 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");
debugTestRunner.assertVariable(gradesChildVariables, "english", "80", "int");

// anonymous record child variable visibility test
Map<String, Variable> recordChildVariables =
debugTestRunner.fetchChildVariables(localVariables.get("anonRecord"));
debugTestRunner.fetchChildVariables(localVariables.get("anonRecord"));
debugTestRunner.assertVariable(recordChildVariables, "city", "\"London\"", "string");
debugTestRunner.assertVariable(recordChildVariables, "country", "\"UK\"", "string");

Expand All @@ -418,18 +440,18 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException

// error details child variable visibility test
Map<String, Variable> 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<String, Variable> 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<String, Variable> 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");
Expand All @@ -438,7 +460,7 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException

// anonymous object child variable visibility test (AnonPerson object)
Map<String, Variable> 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");
Expand All @@ -459,21 +481,21 @@ public void localVariableChildrenVisibilityTest() throws BallerinaTestException

// table with key child variable visibility test
Map<String, Variable> 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<String, Variable> 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<String, Variable> serviceChildVariables =
debugTestRunner.fetchChildVariables(localVariables.get("serviceVar"));
debugTestRunner.fetchChildVariables(localVariables.get("serviceVar"));
debugTestRunner.assertVariable(serviceChildVariables, "i", "5", "int");
}

Expand Down Expand Up @@ -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<BallerinaTestDebugPoint, StoppedEventArguments> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "debug_test_resources"
name = "variable_tests_2"
version = "0.1.0"
Loading

0 comments on commit 43e6737

Please sign in to comment.