Partial solution for #816 plus improved conversion test coverage

This commit is contained in:
Tom Morris 2013-09-18 11:14:48 -04:00
parent d84f897ae0
commit 1261734f15
2 changed files with 15 additions and 11 deletions

View File

@ -52,13 +52,13 @@ public class ToString implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length >= 1) {
Object o1 = args[0];
if (args.length == 2) {
if (o1 instanceof Calendar || o1 instanceof Date) {
if (args.length == 2 && args[1] instanceof String) {
Object o2 = args[1];
if (o2 instanceof String) {
if (o1 instanceof Calendar || o1 instanceof Date) {
DateFormat formatter = new SimpleDateFormat((String) o2);
return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime());
}
} else if (o1 instanceof Number) {
return String.format((String) o2, (Number) o1);
}
} else if (args.length == 1) {
if (o1 instanceof String) {

View File

@ -103,6 +103,7 @@ public class ToFromConversionTests extends RefineTest {
Assert.assertNull(invoke("toNumber", (Object) null));
Assert.assertTrue(invoke("toNumber", "string") instanceof EvalError);
Assert.assertEquals(invoke("toNumber", "0.0"), 0.0);
Assert.assertEquals(invoke("toNumber", "123"), Long.valueOf(123));
Assert.assertTrue(Math.abs((Double) invoke("toNumber", "123.456") - 123.456) < EPSILON);
Assert.assertTrue(Math.abs((Double) invoke("toNumber", "001.234") - 1.234) < EPSILON);
Assert.assertTrue(Math.abs((Double) invoke("toNumber", "1e2") - 100.0) < EPSILON);
@ -110,13 +111,16 @@ public class ToFromConversionTests extends RefineTest {
}
@Test
public void testToString() {
public void testToString() throws CalendarParserException {
Assert.assertTrue(invoke("toString") instanceof EvalError);
Assert.assertTrue(invoke("toString", (Object) null) instanceof EvalError);
Assert.assertEquals(invoke("toString", (Object) null), "null");
Assert.assertEquals(invoke("toString", Long.valueOf(100)),"100");
Assert.assertEquals(invoke("toString", Double.valueOf(100.0)),"100.0");
// Calendar
// Date
// Date&Calendar with 2nd parameter with format
Assert.assertEquals(invoke("toString", Double.valueOf(100.0),"%.0f"),"100");
Assert.assertEquals(invoke("toString", CalendarParser.parse("2013-06-01")),"Jun 1, 2013");
Assert.assertEquals(invoke("toString", CalendarParser.parse("2013-06-01").getTime()),"Jun 1, 2013");
Assert.assertEquals(invoke("toString", CalendarParser.parse("2013-06-01"),"Y"),"2013");
Assert.assertEquals(invoke("toString", CalendarParser.parse("2013-06-01"),"YYYY-MM-dd"),"2013-06-01");
}
@Test