Merge pull request #1784 from OpenRefine/issue1775

Fix rendering of integers in Wikidata extension.
This commit is contained in:
Antonin Delpeuch 2018-11-01 12:03:43 +00:00 committed by GitHub
commit 3c7c549826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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() {