Skip to content

Commit

Permalink
Add more tests in finders
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Sep 13, 2024
1 parent a379b9d commit 6bf09d9
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ public void visit(BLangXMLIndexedStepExtend xmlIndexedStepExtend) {
@Override
public void visit(BLangXMLFilterStepExtend xmlFilterStepExtend) {
lookupNodes(xmlFilterStepExtend.filters);
setEnclosingNode(xmlFilterStepExtend, xmlFilterStepExtend.pos);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,12 @@ public void visit(BLangXMLFilterStepExtend xmlFilterStepExtend) {

@Override
public void visit(BLangXMLMethodCallStepExtend xmlMethodCallStepExtend) {
find(xmlMethodCallStepExtend.invocation);
BLangInvocation invocation = xmlMethodCallStepExtend.invocation;
List<BLangExpression> argExprs = invocation.argExprs;
for (int i = 1; i < argExprs.size(); i++) {
find(argExprs.get(i));
}
addIfSameSymbol(invocation.symbol, invocation.name.pos);
}

@Override
Expand Down Expand Up @@ -1521,8 +1526,7 @@ private boolean addIfSameSymbol(BSymbol symbol, Location location) {
&& this.targetSymbol.name.equals(symbol.name)
&& this.targetSymbol.pkgID.equals(symbol.pkgID)
&& this.targetSymbol.pos.equals(symbol.pos)
&& (this.withDefinition || (symbol.getOrigin() != VIRTUAL && !symbol.pos.equals(location)))) {
this.referenceLocations.add(location);
&& (this.withDefinition || (symbol.getOrigin() != VIRTUAL && !symbol.pos.equals(location)))) {this.referenceLocations.add(location);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLCommentLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementAccess;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLFilterStepExtend;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLIndexedStepExtend;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLMethodCallStepExtend;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLNavigationAccess;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName;
Expand Down Expand Up @@ -1101,6 +1104,18 @@ public void visit(BLangExtendedXMLNavigationAccess extendedXMLNavigationAccess)
extendedXMLNavigationAccess.stepExpr = rewriteExpr(extendedXMLNavigationAccess.stepExpr);
result = extendedXMLNavigationAccess;
}

@Override
public void visit(BLangXMLIndexedStepExtend xmlIndexedStepExtend) {
xmlIndexedStepExtend.indexExpr = rewriteExpr(xmlIndexedStepExtend.indexExpr);
result = xmlIndexedStepExtend;
}

@Override
public void visit(BLangXMLMethodCallStepExtend xmlMethodCallStepExtend) {
xmlMethodCallStepExtend.invocation = rewriteExpr(xmlMethodCallStepExtend.invocation);
result = xmlMethodCallStepExtend;
}

@Override
public void visit(BLangIndexBasedAccess.BLangJSONAccessExpr jsonAccessExpr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLCommentLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementAccess;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLElementLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLIndexedStepExtend;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLMethodCallStepExtend;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLNavigationAccess;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLProcInsLiteral;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName;
Expand Down Expand Up @@ -1901,6 +1903,23 @@ public void visit(BLangIndexBasedAccess.BLangXMLAccessExpr xmlIndexAccessExpr) {
result = xmlIndexAccessExpr;
}

@Override
public void visit(BLangExtendedXMLNavigationAccess extendedXMLNavigationAccess) {
extendedXMLNavigationAccess.stepExpr = rewriteExpr(extendedXMLNavigationAccess.stepExpr);
result = extendedXMLNavigationAccess;
}

@Override
public void visit(BLangXMLIndexedStepExtend xmlIndexedStepExtend) {
xmlIndexedStepExtend.indexExpr = rewriteExpr(xmlIndexedStepExtend.indexExpr);
result = xmlIndexedStepExtend;
}

@Override
public void visit(BLangXMLMethodCallStepExtend xmlMethodCallStepExtend) {
xmlMethodCallStepExtend.invocation = rewriteExpr(xmlMethodCallStepExtend.invocation);
result = xmlMethodCallStepExtend;
}

@Override
public void visit(BLangXMLElementAccess xmlElementAccess) {
Expand All @@ -1914,12 +1933,6 @@ public void visit(BLangXMLNavigationAccess xmlNavigation) {
result = xmlNavigation;
}

@Override
public void visit(BLangExtendedXMLNavigationAccess extendedXmlNavigationAccess) {
extendedXmlNavigationAccess.stepExpr = rewriteExpr(extendedXmlNavigationAccess.stepExpr);
result = extendedXmlNavigationAccess;
}

@Override
public void visit(BLangIndexBasedAccess.BLangJSONAccessExpr jsonAccessExpr) {
jsonAccessExpr.indexExpr = rewriteExpr(jsonAccessExpr.indexExpr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6644,6 +6644,7 @@ private BLangInvocation rewriteXMLAttributeOrElemNameAccess(BLangFieldBasedAcces
}

// Handle element name access.
// Accessing field name using the "_" has been disallowed in the compile time.
if (fieldName.equals("_")) {
return createLanglibXMLInvocation(fieldAccessExpr.pos, XML_INTERNAL_GET_ELEMENT_NAME_NIL_LIFTING,
fieldAccessExpr.expr, Collections.emptyList(), Collections.emptyList());
Expand Down Expand Up @@ -10629,15 +10630,15 @@ protected void addTransactionInternalModuleImport() {
private void createMapFunctionForStepExpr(BLangXMLNavigationAccess xmlNavigation,
List<BLangXMLStepExtend> extensions) {
Location pos = xmlNavigation.pos;
ArrayList<BLangExpression> args = new ArrayList<>();
ArrayList<BLangExpression> filters = expandFilters(xmlNavigation.filters);
List<BLangExpression> args;
List<BLangExpression> filters = expandFilters(xmlNavigation.filters);

// xml/**/<elemName>
if (xmlNavigation.navAccessType == XMLNavigationAccess.NavAccessType.DESCENDANTS) {
args.add(createArrowFunctionForNavigation(pos, extensions, XML_GET_DESCENDANTS, filters));
args = List.of(createArrowFunctionForNavigation(pos, extensions, XML_GET_DESCENDANTS, filters));
} else {
// xml/*
args.add(createArrowFunctionForNavigation(pos, extensions, XML_INTERNAL_CHILDREN, filters));
args = List.of(createArrowFunctionForNavigation(pos, extensions, XML_INTERNAL_CHILDREN, filters));
}

BLangInvocation elements =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7097,7 +7097,7 @@ private List<BLangXMLStepExtend> createBLangXMLStepExtends(NodeList<Node> nodes,
XMLStepMethodCallExtendNode xmlStepMethodCallExtendNode = (XMLStepMethodCallExtendNode) node;
SimpleNameReferenceNode methodName = xmlStepMethodCallExtendNode.methodName();
BLangInvocation bLangInvocation = createBLangInvocation(methodName,
xmlStepMethodCallExtendNode.parenthesizedArgList().arguments(), methodName.location(),
xmlStepMethodCallExtendNode.parenthesizedArgList().arguments(), pos,
false);
bLangInvocation.expr = curExpr == null ? expr : curExpr;
curExpr = new BLangXMLMethodCallStepExtend(pos, bLangInvocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import org.wso2.ballerinalang.compiler.tree.expressions.BLangElvisExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangErrorConstructorExpr;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangErrorVarRef;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangExtendedXMLNavigationAccess;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess;
import org.wso2.ballerinalang.compiler.tree.expressions.BLangGroupExpr;
Expand Down Expand Up @@ -2316,20 +2317,17 @@ public void visit(BLangXMLElementFilter source) {

@Override
public void visit(BLangXMLIndexedStepExtend source) {
BLangXMLIndexedStepExtend clone = new BLangXMLIndexedStepExtend(source.pos, source.indexExpr);
source.cloneRef = clone;
source.cloneRef = new BLangXMLIndexedStepExtend(source.pos, clone(source.indexExpr));
}

@Override
public void visit(BLangXMLFilterStepExtend source) {
BLangXMLFilterStepExtend clone = new BLangXMLFilterStepExtend(source.pos, cloneList(source.filters));
source.cloneRef = clone;
source.cloneRef = new BLangXMLFilterStepExtend(source.pos, cloneList(source.filters));
}

@Override
public void visit(BLangXMLMethodCallStepExtend source) {
BLangXMLMethodCallStepExtend clone = new BLangXMLMethodCallStepExtend(source.pos, clone(source.invocation));
source.cloneRef = clone;
source.cloneRef = new BLangXMLMethodCallStepExtend(source.pos, clone(source.invocation));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,12 @@ public Object[][] getLogicalExprPos() {
// Group expr
{282, 12, 282, 34, INT},
{282, 14, 282, 28, INT},

// XML step expression with extension
{376, 17, 376, 18, INT},
{377, 13, 377, 19, UNION},
{377, 17, 377, 18, INT},
{378, 9, 378, 31, XML},
{378, 16, 378, 23, XML},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public Object[][] getLookupPositions() {
location(21, 24, 27),
location(26, 17, 20),
location(27, 17, 20),
location(29, 16, 19))
location(29, 16, 19),
location(34, 23, 26))
},
{23, 25, location(19, 37, 40),
List.of(location(19, 37, 40),
location(23, 25, 28),
location(27, 23, 26),
location(28, 17, 20))
location(28, 17, 20),
location(32, 20, 23))
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ public Object[][] getStepExprPos() {
};
}

@Test(dataProvider = "StepExprWithExtensionPosProvider")
public void testXmlStepExprWithExtension(int line, int col, SymbolKind kind, String name) {
assertSymbol(line, col, kind, name);
}

@DataProvider(name = "StepExprWithExtensionPosProvider")
public Object[][] getStepExprWithExtensionPos() {
return new Object[][]{
{49, 13, VARIABLE, "x1"},
{49, 18, FUNCTION, "get"},
{49, 22, VARIABLE, "indx"},
{50, 23, XMLNS, "ns"},
{51, 31, null, null},
{51, 37, VARIABLE, "indx"}
};
}

// utils

private void assertSymbol(int line, int col, SymbolKind kind, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,10 @@ function testRegexp() {
function name() {
_ = xml ``;
}

function testXMLStepExprWithExtension() {
xml x = xml `<root><parent><child>1</child></parent><parent>0</parent></root>`;
xml x2 = x/*[0];
x2 = x/*.get(0);
x2 = x/*[1].first().<child>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ function test() {
xml x3 = x1.<ns0:*|ns1:*>;
xml x4 = x1/<ns1:child>;
var v1 = x1.ns0:id;

int i = 1;
xml x6 = x1/**/<ns1:foo>;
xml x7 = x1/*[i];
xml x8 = x1/<foo>.<ns0:foo>.get(i);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ function testXMLStepExpr() {
int indx = 0;

function getXML() returns xml => xml `<greet>Hello World!</greet>`;

function testXmlStepExprWithExtension() {
xml x1 = xml `<item><ns:name>T-shirt</ns:name></item><item>text<name>Watch</name></item>`;
xml x2 = x1/*.get(indx);
x2 = x1/<ns:name>.<ns:item>;
x2 = x1/**/<name>.<ns:item|name>[indx];
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,13 @@ public void testXmlStepExtension(String function) {

@DataProvider
private Object[] xmlStepExtension() {
return new Object[]{"testXmlIndexedStepExtend", "testXmlFilterStepExtend", "testXmlIndexedAndFilterStepExtend",
"testXmlMethodCallStepExtend", "testXmlMethodCallIndexedAndFilterStepExtend"};
return new Object[]{
"testXmlIndexedStepExtend",
"testXmlFilterStepExtend",
"testXmlIndexedAndFilterStepExtend",
"testXmlMethodCallStepExtend",
"testXmlMethodCallIndexedAndFilterStepExtend"
};
}

@Test
Expand All @@ -311,20 +316,20 @@ public void testXMLNavExpressionNegative() {
BAssertUtil.validateError(navigationNegative, i++, "undefined symbol 'j'", 8, 18);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'int', found 'string'", 17,
18);
BAssertUtil.validateError(navigationNegative, i++, "too many arguments in call to 'get()'", 18, 14);
BAssertUtil.validateError(navigationNegative, i++, "too many arguments in call to 'get()'", 18, 13);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'int', found 'string'", 19,
18);
BAssertUtil.validateError(navigationNegative, i++, "undefined function 'foo' in type 'xml'", 21, 14);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'xml', found 'int'", 22, 14);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'xml', found 'int'", 22, 13);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'int', found 'string'", 23,
23);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'xml', found 'int'", 25, 23);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'xml', found 'int'", 25, 22);
BAssertUtil.validateError(navigationNegative, i++, "undefined symbol 'r'", 29, 23);
BAssertUtil.validateError(navigationNegative, i++,
"incompatible types: expected 'boolean', found 'xml:Element'", 31, 31);
BAssertUtil.validateError(navigationNegative, i++,
"incompatible types: expected 'xml:ProcessingInstruction', found 'xml:Element'", 33, 60);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'xml', found '()'", 34, 19);
BAssertUtil.validateError(navigationNegative, i++, "incompatible types: expected 'xml', found '()'", 34, 18);
BAssertUtil.validateError(navigationNegative, i++, "undefined symbol 'r'", 36, 26);
BAssertUtil.validateError(navigationNegative, i++,
"incompatible types: expected 'boolean', found 'xml:Element'", 38, 34);
Expand Down
Loading

0 comments on commit 6bf09d9

Please sign in to comment.