Skip to content

Commit

Permalink
Fix failing langlib xml unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rdhananjaya committed Jan 21, 2020
1 parent dccefbf commit 1c796bc
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,6 @@ public BXML<?> getItem(long index) {
* {@inheritDoc}
*/
public long size() {
if (getNodeType() == XMLNodeType.TEXT) {
String textContent = ((OMText) this.omNode).getText();
return textContent.codePointCount(0, textContent.length());
}
return this.omNode == null ? 0 : 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public XMLValue children() {
*/
@Override
public XMLValue children(String qname) {
return children.children(qname);
return children.elements(qname);
}

/**
Expand Down Expand Up @@ -624,6 +624,7 @@ public XMLSequence getChildrenSeq() {

@Override
public IteratorValue getIterator() {
XMLItem that = this;
return new IteratorValue() {
boolean read = false;

Expand All @@ -638,7 +639,7 @@ public Object next() {
throw new NoSuchElementException();
}
read = true;
return this;
return that;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public XMLValue children() {

@Override
public XMLValue children(String qname) {
return null;
return new XMLSequence();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,49 @@ public XMLValue children() {
@Override
public XMLValue children(String qname) {
List<BXML> selected = new ArrayList<>();
if (this.children.size() == 1) {
BXML bxml = this.children.get(0);
return (XMLValue) bxml.children(qname);
}

// boolean prevChildIsText = false;
for (BXML elem : this.children) {
if (elem.getElementName().equals(qname)) {
selected.add(elem);
XMLSequence elements = (XMLSequence) elem.children().elements(qname);
List<BXML> childrenList = elements.getChildrenList();
if (childrenList.size() == 1) {
selected.add(childrenList.get(0));
} else if (childrenList.size() > 1) {
selected.addAll(childrenList);
}

// if (elem.getElementName().equals(qname)) {
// selected.add(elem);
// List<BXML> childrenList = ((XMLItem) elem).getChildrenSeq().getChildrenList();
// for (BXML child : childrenList) {
// if (child.getNodeType() == XMLNodeType.TEXT) {
// if (prevChildIsText) {
// int lastPos = selected.size() - 1;
// XMLText bxml = (XMLText) selected.get(lastPos);
// selected.set(lastPos, new XMLText(bxml.getTextValue() + child.getTextValue()));
// } else {
// selected.add(child);
// }
// prevChildIsText = true;
// } else {
// prevChildIsText = false;
// selected.add(child);
// }
// }
// }
// BXML children = elem.children(qname);
// if (children.getNodeType() == XMLNodeType.SEQUENCE) {
// List<BXML> list = ((XMLSequence) children).children;
// if (!list.isEmpty()) {
// selected.addAll(list);
// }
// } else {
// selected.add(children);
// }
}

if (selected.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.ballerinalang.jvm.values.ArrayValue;
import org.ballerinalang.jvm.values.XMLPi;
import org.ballerinalang.jvm.values.XMLSequence;
import org.ballerinalang.jvm.values.XMLText;
import org.ballerinalang.jvm.values.XMLValue;
import org.ballerinalang.jvm.values.api.BXML;
import org.ballerinalang.model.types.TypeKind;
Expand Down Expand Up @@ -55,9 +56,9 @@ public static XMLValue concat(Strand strand, ArrayValue arrayValue) {
if (refValue instanceof String) {
if (lastItem != null && lastItem.getNodeType() == XMLNodeType.TEXT) {
// If last added item is a string, then concat prev values with this values and replace prev value.
String concat = ((XMLPi) lastItem).getData() + refValue;
String concat = lastItem.getTextValue() + refValue;
XMLValue xmlText = XMLFactory.createXMLText(concat);
backingArray.add(backingArray.size() - 1, xmlText);
backingArray.set(backingArray.size() - 1, xmlText);
lastItem = xmlText;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Object[][] getXML() {
return new Object[][]{
{returns[0], 1},
{returns[1], 1},
{returns[2], 12}
{returns[2], 1}
};
}

Expand All @@ -87,8 +87,8 @@ public void testEmptyConcatCall() {
public void testConcat() {
BValue[] returns = BRunUtil.invoke(compileResult, "testConcat");
assertTrue(((BXML<?>) returns[0]).getNodeType() == XMLNodeType.SEQUENCE);
assertEquals(((BXML<?>) returns[0]).size(), 21);
assertEquals(((BXML<?>) returns[0]).stringValue(),
assertEquals(returns[0].size(), 5);
assertEquals(returns[0].stringValue(),
"<hello>xml content</hello><TITLE>Empire Burlesque</TITLE><TITLE>Hide your heart</TITLE>" +
"<TITLE>Greatest Hits</TITLE>hello from String");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testXMLWithArityChildren() {
"xmlns:q=\"bar\">\n" +
" <p:city>NY</p:city>\n" +
" <q:country>US</q:country>\n" +
" </p:address> 2:<q:ID xmlns:q=\"bar\" xmlns:p=\"foo\">1131313</q:ID> ";
" </p:address> 2:<q:ID xmlns:p=\"foo\" xmlns:q=\"bar\">1131313</q:ID> ";
BValue[] returns = BRunUtil.invoke(program, "testXMLWithArityChildren");
Assert.assertEquals(returns.length, 1);
Assert.assertEquals(returns[0].stringValue(), payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ForeachXMLTypedBindingPatternsTests {
"1:<p:address xmlns:p=\"foo\" xmlns:q=\"bar\">\n" +
" <p:city>NY</p:city>\n" +
" <q:country>US</q:country>\n" +
" </p:address> 2:<q:ID xmlns:q=\"bar\" xmlns:p=\"foo\">1131313</q:ID> ";
" </p:address> 2:<q:ID xmlns:p=\"foo\" xmlns:q=\"bar\">1131313</q:ID> ";

@BeforeClass
public void setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function testXmlInnerElementsWithSimpleVariableWithoutType() returns string {
output = "";

int i = 0;
foreach var v in xdata.* {
foreach var v in xdata.*.elements() {
if v is xml {
concatIntXml(i, v);
i += 1;
Expand All @@ -60,7 +60,7 @@ function testXmlInnerElementsWithSimpleVariableWithType() returns string {
output = "";

int i = 0;
foreach xml|string v in xdata.* {
foreach xml|string v in xdata.*.elements() {
if v is xml {
concatIntXml(i, v);
i += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ function testForEach() returns xml {
xml r = xmllib:concat();
foreach var x in catalog.* {
if (x is xml) {
r += x;
if (x.isElement()) {
r += x;
}
}
}
return r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,22 @@ private static Object getJVMValue(org.wso2.ballerinalang.compiler.semantics.mode
return jvmObject;
case TypeTags.XML_TAG:
org.ballerinalang.model.values.BXML xml = (org.ballerinalang.model.values.BXML) value;
if (xml.getNodeType() == org.ballerinalang.model.util.XMLNodeType.TEXT) {
return XMLFactory.createXMLText(xml.stringValue());
}
if (xml.getNodeType() != org.ballerinalang.model.util.XMLNodeType.SEQUENCE) {
return XMLFactory.parse(xml.stringValue());
}
BValueArray elements = ((BXMLSequence) xml).value();
ArrayValue arrayValue = (ArrayValue) getJVMValue(elements.getType(), elements);
} else {
BValueArray elements = ((BXMLSequence) xml).value();
ArrayValue arrayValue = (ArrayValue) getJVMValue(elements.getType(), elements);

List<BXML> list = new ArrayList<>();
for (Object arrayValueValue : arrayValue.getValues()) {
list.add((BXML) arrayValueValue);
}
List<BXML> list = new ArrayList<>();
for (Object arrayValueValue : arrayValue.getValues()) {
list.add((BXML) arrayValueValue);
}

return new XMLSequence(list);
return new XMLSequence(list);
}
case TypeTags.HANDLE_TAG:
BHandleValue handleValue = (BHandleValue) value;
return new HandleValue(handleValue.getValue());
Expand Down

0 comments on commit 1c796bc

Please sign in to comment.