Fix rendering of integers in Wikidata extension. Closes #1775

This commit is contained in:
Antonin Delpeuch 2018-10-27 21:43:22 +02:00
parent 3fb282852d
commit e1014bfeed
2 changed files with 24 additions and 1 deletions

View File

@ -58,7 +58,11 @@ public class WbStringVariable extends WbVariableExpr<StringValue> {
public StringValue fromCell(Cell cell, ExpressionContext ctxt)
throws SkipSchemaExpressionException {
if (!cell.value.toString().isEmpty()) {
return Datamodel.makeStringValue(cell.value.toString());
String stringValue = cell.value.toString();
if (cell.value instanceof Double && ((Double)cell.value) % 1 == 0) {
stringValue = Integer.toString(((Double)cell.value).intValue());
}
return Datamodel.makeStringValue(stringValue);
}
throw new SkipSchemaExpressionException();
}

View File

@ -27,6 +27,8 @@ import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import com.google.refine.model.Cell;
public class WbStringVariableTest extends WbVariableTest<StringValue> {
@Override
@ -52,6 +54,23 @@ public class WbStringVariableTest extends WbVariableTest<StringValue> {
public void testTrailingWhitespace() {
evaluatesTo(Datamodel.makeStringValue("dirty \t"), "dirty \t");
}
/**
* Test that integers are correctly converted to strings
*/
@Test
public void testInteger() {
evaluatesTo(Datamodel.makeStringValue("45"), new Cell(45,null));
}
/**
* Test that floating point numbers with no decimal part are also converted
*/
@Test
public void testDoubleInteger() {
evaluatesTo(Datamodel.makeStringValue("45"), new Cell(45.0,null));
}
@Test
public void testLeadingWhitespace() {