mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #55 from johnjaylward/verifyOptMissingKeys
Updates tests to include all opt methods and verify for missing keys.
This commit is contained in:
commit
a418d07460
2 changed files with 47 additions and 6 deletions
|
@ -373,6 +373,9 @@ public class JSONArrayTest {
|
||||||
assertTrue("Array opt value out of range",
|
assertTrue("Array opt value out of range",
|
||||||
null == jsonArray.opt(-1));
|
null == jsonArray.opt(-1));
|
||||||
|
|
||||||
|
assertTrue("Array opt value out of range",
|
||||||
|
null == jsonArray.opt(jsonArray.length()));
|
||||||
|
|
||||||
assertTrue("Array opt boolean",
|
assertTrue("Array opt boolean",
|
||||||
Boolean.TRUE == jsonArray.optBoolean(0));
|
Boolean.TRUE == jsonArray.optBoolean(0));
|
||||||
assertTrue("Array opt boolean default",
|
assertTrue("Array opt boolean default",
|
||||||
|
|
|
@ -1714,20 +1714,58 @@ public class JSONObjectTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exercise JSONObject opt(key, default) method
|
* Exercise JSONObject opt(key, default) method.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void jsonObjectOptDefault() {
|
public void jsonObjectOptDefault() {
|
||||||
|
|
||||||
String str = "{\"myKey\": \"myval\"}";
|
String str = "{\"myKey\": \"myval\", \"hiKey\": null}";
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
|
|
||||||
|
assertTrue("optBigDecimal() should return default BigDecimal",
|
||||||
|
BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0);
|
||||||
|
assertTrue("optBigInteger() should return default BigInteger",
|
||||||
|
BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0);
|
||||||
assertTrue("optBoolean() should return default boolean",
|
assertTrue("optBoolean() should return default boolean",
|
||||||
Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE));
|
jsonObject.optBoolean("myKey", true));
|
||||||
assertTrue("optInt() should return default int",
|
assertTrue("optInt() should return default int",
|
||||||
42 == jsonObject.optInt("myKey", 42));
|
42 == jsonObject.optInt("myKey", 42));
|
||||||
|
assertTrue("optEnum() should return default Enum",
|
||||||
|
MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1)));
|
||||||
|
assertTrue("optJSONArray() should return null ",
|
||||||
|
null==jsonObject.optJSONArray("myKey"));
|
||||||
|
assertTrue("optJSONObject() should return null ",
|
||||||
|
null==jsonObject.optJSONObject("myKey"));
|
||||||
|
assertTrue("optLong() should return default long",
|
||||||
|
42 == jsonObject.optLong("myKey", 42));
|
||||||
|
assertTrue("optDouble() should return default double",
|
||||||
|
42.3 == jsonObject.optDouble("myKey", 42.3));
|
||||||
|
assertTrue("optString() should return default string",
|
||||||
|
"hi".equals(jsonObject.optString("hiKey", "hi")));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exercise JSONObject opt(key, default) method when the key doesn't exist.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void jsonObjectOptNoKey() {
|
||||||
|
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
|
||||||
|
assertTrue("optBigDecimal() should return default BigDecimal",
|
||||||
|
BigDecimal.TEN.compareTo(jsonObject.optBigDecimal("myKey", BigDecimal.TEN))==0);
|
||||||
|
assertTrue("optBigInteger() should return default BigInteger",
|
||||||
|
BigInteger.TEN.compareTo(jsonObject.optBigInteger("myKey",BigInteger.TEN ))==0);
|
||||||
|
assertTrue("optBoolean() should return default boolean",
|
||||||
|
jsonObject.optBoolean("myKey", true));
|
||||||
assertTrue("optInt() should return default int",
|
assertTrue("optInt() should return default int",
|
||||||
42 == jsonObject.optInt("myKey", 42));
|
42 == jsonObject.optInt("myKey", 42));
|
||||||
|
assertTrue("optEnum() should return default Enum",
|
||||||
|
MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1)));
|
||||||
|
assertTrue("optJSONArray() should return null ",
|
||||||
|
null==jsonObject.optJSONArray("myKey"));
|
||||||
|
assertTrue("optJSONObject() should return null ",
|
||||||
|
null==jsonObject.optJSONObject("myKey"));
|
||||||
assertTrue("optLong() should return default long",
|
assertTrue("optLong() should return default long",
|
||||||
42 == jsonObject.optLong("myKey", 42));
|
42 == jsonObject.optLong("myKey", 42));
|
||||||
assertTrue("optDouble() should return default double",
|
assertTrue("optDouble() should return default double",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue