Skip to content

Commit

Permalink
Merge pull request #41996 from HindujaB/fix-type-check-xml-sequence
Browse files Browse the repository at this point in the history
Fix type creation of xml sequence with `xml:Text`
  • Loading branch information
warunalakshitha authored Feb 15, 2024
2 parents 45ab4cd + 64705e9 commit 8e96ba6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public class PredefinedTypes {
TYPE_TEXT)), EMPTY_MODULE);

public static final Type TYPE_READONLY_XML = ReadOnlyUtils.setImmutableTypeAndGetEffectiveType(TYPE_XML);
public static final Type TYPE_XML_ELEMENT_SEQUENCE = new BXmlType(PredefinedTypes.TYPE_ELEMENT, false);
public static final Type TYPE_XML_COMMENT_SEQUENCE = new BXmlType(PredefinedTypes.TYPE_COMMENT, false);
public static final Type TYPE_XML_PI_SEQUENCE = new BXmlType(PredefinedTypes.TYPE_PROCESSING_INSTRUCTION, false);
public static final Type TYPE_XML_TEXT_SEQUENCE = new BXmlType(PredefinedTypes.TYPE_TEXT, false);

public static final AnyType TYPE_ANY = new BAnyType(TypeConstants.ANY_TNAME, EMPTY_MODULE, false);
public static final AnyType TYPE_READONLY_ANY = new BAnyType(TypeConstants.READONLY_ANY_TNAME, EMPTY_MODULE, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2981,8 +2981,8 @@ private static boolean checkValueEquals(Object lhsValue, Object rhsValue, List<V
return checkDecimalEqual((DecimalValue) lhsValue, (DecimalValue) rhsValue);
case TypeTags.XML_TAG:
// Instance of xml never
if (lhsValue instanceof XmlText) {
return TypeTags.isXMLTypeTag(rhsValTypeTag) && isEqual((XmlText) lhsValue, (XmlValue) rhsValue);
if (lhsValue instanceof XmlText xmlText) {
return TypeTags.isXMLTypeTag(rhsValTypeTag) && isEqual(xmlText, (XmlValue) rhsValue);
}
return TypeTags.isXMLTypeTag(rhsValTypeTag) && isEqual((XmlSequence) lhsValue, (XmlValue) rhsValue);
case TypeTags.XML_ELEMENT_TAG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import io.ballerina.runtime.internal.errors.ErrorHelper;
import io.ballerina.runtime.internal.types.BArrayType;
import io.ballerina.runtime.internal.types.BUnionType;
import io.ballerina.runtime.internal.types.BXmlType;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -626,17 +625,12 @@ public Object next() {
}

private Type getSequenceType(Type tempExprType) {
switch (tempExprType.getTag()) {
case TypeTags.XML_ELEMENT_TAG:
return new BXmlType(PredefinedTypes.TYPE_ELEMENT, false);
case TypeTags.XML_COMMENT_TAG:
return new BXmlType(PredefinedTypes.TYPE_COMMENT, false);
case TypeTags.XML_PI_TAG:
return new BXmlType(PredefinedTypes.TYPE_PROCESSING_INSTRUCTION, false);
default:
// Since 'xml:Text is same as xml<'xml:Text>
return PredefinedTypes.TYPE_TEXT;
}
return switch (tempExprType.getTag()) {
case TypeTags.XML_ELEMENT_TAG -> PredefinedTypes.TYPE_XML_ELEMENT_SEQUENCE;
case TypeTags.XML_COMMENT_TAG -> PredefinedTypes.TYPE_XML_COMMENT_SEQUENCE;
case TypeTags.XML_PI_TAG -> PredefinedTypes.TYPE_XML_PI_SEQUENCE;
default -> PredefinedTypes.TYPE_XML_TEXT_SEQUENCE;
};
}

private void initializeIteratorNextReturnType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ public Object[] getXmlTestFunctions() {
"testUnequalXmlIgnoringAttributeOrder", "testEqualXmlWithPI", "testUnequalXmlWithUnequalPI",
"testUnequalXmlWithPIInWrongOrder", "testUnequalXmlWithMultiplePIInWrongOrder",
"testUnequalXmlWithMissingPI", "testXmlWithNamespacesPositive", "testXmlWithNamespacesNegative",
"testXmlSequenceAndXmlItemEqualityPositive", "testXmlSequenceAndXmlItemEqualityNegative"
"testXmlSequenceAndXmlItemEqualityPositive", "testXmlSequenceAndXmlItemEqualityNegative",
"testXmlSequenceLHSEquals"
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,17 @@ public function testXmlSequenceAndXmlItemEqualityNegative() {
test:assertFalse(x1 == x3 || !(x1 != x3) || x3 == x1 || !(x3 != x1));
}

public function testXmlSequenceLHSEquals() {
string a = "hello";
xml x1 = xml `AS-${a}`;
xml x2 = xml `AS-${a}`;
test:assertTrue(x1 == x2 && !(x1 != x2));

x1 = xml `<?target data?><?target_two data_two?>`;
x2 = xml `<?target data?><?target_two data_two?>`;
test:assertTrue(x1 == x2 && !(x1 != x2));
}

function testXmlStringNegative() {
anydata x1 = xml `<book>The Lost World</book>`;
anydata x2 = "<book>The Lost World</book>";
Expand Down

0 comments on commit 8e96ba6

Please sign in to comment.