Skip to content

Commit 2867aaa

Browse files
author
John J. Aylward
committed
Updates test cases to support new optFloat and optNumber
1 parent 49d47e3 commit 2867aaa

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,20 @@ public void opt() {
393393
assertTrue("Array opt double default implicit",
394394
new Double(jsonArray.optDouble(99)).isNaN());
395395

396+
assertTrue("Array opt float",
397+
new Float(23.45e-4).equals(jsonArray.optFloat(5)));
398+
assertTrue("Array opt float default",
399+
new Float(1).equals(jsonArray.optFloat(0, 1)));
400+
assertTrue("Array opt float default implicit",
401+
new Float(jsonArray.optFloat(99)).isNaN());
402+
403+
assertTrue("Array opt Number",
404+
new Double(23.45e-4).equals(jsonArray.optNumber(5)));
405+
assertTrue("Array opt Number default",
406+
new Double(1).equals(jsonArray.optNumber(0, 1d)));
407+
assertTrue("Array opt Number default implicit",
408+
new Double(jsonArray.optNumber(99,Double.NaN).doubleValue()).isNaN());
409+
396410
assertTrue("Array opt int",
397411
new Integer(42).equals(jsonArray.optInt(7)));
398412
assertTrue("Array opt int default",

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,10 @@ public void jsonObjectValues() {
614614
jsonObject.optDouble("doubleKey") == -23.45e7);
615615
assertTrue("opt doubleKey with Default should be double",
616616
jsonObject.optDouble("doubleStrKey", Double.NaN) == 1);
617+
assertTrue("optFloat doubleKey should be float",
618+
jsonObject.optFloat("doubleKey") == -23.45e7f);
619+
assertTrue("optFloat doubleKey with Default should be float",
620+
jsonObject.optFloat("doubleStrKey", Float.NaN) == 1f);
617621
assertTrue("intKey should be int",
618622
jsonObject.optInt("intKey") == 42);
619623
assertTrue("opt intKey should be int",
@@ -630,6 +634,18 @@ public void jsonObjectValues() {
630634
jsonObject.optLong("longKey", 0) == 1234567890123456789L);
631635
assertTrue("longStrKey should be long",
632636
jsonObject.getLong("longStrKey") == 987654321098765432L);
637+
assertTrue("optNumber int should return Integer",
638+
jsonObject.optNumber("intKey") instanceof Integer);
639+
assertTrue("optNumber long should return Long",
640+
jsonObject.optNumber("longKey") instanceof Long);
641+
assertTrue("optNumber double should return Double",
642+
jsonObject.optNumber("doubleKey") instanceof Double);
643+
assertTrue("optNumber Str int should return BigDecimal",
644+
jsonObject.optNumber("intStrKey") instanceof BigDecimal);
645+
assertTrue("optNumber Str long should return BigDecimal",
646+
jsonObject.optNumber("longStrKey") instanceof BigDecimal);
647+
assertTrue("optNumber Str double should return BigDecimal",
648+
jsonObject.optNumber("doubleStrKey") instanceof BigDecimal);
633649
assertTrue("xKey should not exist",
634650
jsonObject.isNull("xKey"));
635651
assertTrue("stringKey should exist",
@@ -1937,9 +1953,13 @@ public void jsonObjectOptDefault() {
19371953
assertTrue("optJSONObject() should return null ",
19381954
null==jsonObject.optJSONObject("myKey"));
19391955
assertTrue("optLong() should return default long",
1940-
42 == jsonObject.optLong("myKey", 42));
1956+
42l == jsonObject.optLong("myKey", 42l));
19411957
assertTrue("optDouble() should return default double",
1942-
42.3 == jsonObject.optDouble("myKey", 42.3));
1958+
42.3d == jsonObject.optDouble("myKey", 42.3d));
1959+
assertTrue("optFloat() should return default float",
1960+
42.3f == jsonObject.optFloat("myKey", 42.3f));
1961+
assertTrue("optNumber() should return default Number",
1962+
42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
19431963
assertTrue("optString() should return default string",
19441964
"hi".equals(jsonObject.optString("hiKey", "hi")));
19451965
}
@@ -1967,9 +1987,13 @@ public void jsonObjectOptNoKey() {
19671987
assertTrue("optJSONObject() should return null ",
19681988
null==jsonObject.optJSONObject("myKey"));
19691989
assertTrue("optLong() should return default long",
1970-
42 == jsonObject.optLong("myKey", 42));
1990+
42l == jsonObject.optLong("myKey", 42l));
19711991
assertTrue("optDouble() should return default double",
1972-
42.3 == jsonObject.optDouble("myKey", 42.3));
1992+
42.3d == jsonObject.optDouble("myKey", 42.3d));
1993+
assertTrue("optFloat() should return default float",
1994+
42.3f == jsonObject.optFloat("myKey", 42.3f));
1995+
assertTrue("optNumber() should return default Number",
1996+
42l == jsonObject.optNumber("myKey", Long.valueOf(42)).longValue());
19731997
assertTrue("optString() should return default string",
19741998
"hi".equals(jsonObject.optString("hiKey", "hi")));
19751999
}
@@ -1983,11 +2007,13 @@ public void jsonObjectOptStringConversion() {
19832007
assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true);
19842008
assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false);
19852009
assertTrue("unexpected optInt value",jo.optInt("int",0)==123);
1986-
assertTrue("unexpected optLong value",jo.optLong("int",0)==123);
1987-
assertTrue("unexpected optDouble value",jo.optDouble("int",0.0)==123.0);
2010+
assertTrue("unexpected optLong value",jo.optLong("int",0)==123l);
2011+
assertTrue("unexpected optDouble value",jo.optDouble("int",0.0d)==123.0d);
2012+
assertTrue("unexpected optFloat value",jo.optFloat("int",0.0f)==123.0f);
19882013
assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
19892014
assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
1990-
2015+
assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
2016+
assertTrue("unexpected optNumber value",jo.optNumber("int",BigInteger.ZERO).longValue()==123l);
19912017
}
19922018

19932019
/**

0 commit comments

Comments
 (0)