Skip to content

Updates tests to include all opt methods and verify for missing keys. #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,10 @@ public void opt() {
assertTrue("Array opt value out of range",
null == jsonArray.opt(-1));

assertTrue("Array opt boolean",
assertTrue("Array opt value out of range",
null == jsonArray.opt(jsonArray.length()));

assertTrue("Array opt boolean",
Boolean.TRUE == jsonArray.optBoolean(0));
assertTrue("Array opt boolean default",
Boolean.FALSE == jsonArray.optBoolean(-1, Boolean.FALSE));
Expand Down
48 changes: 43 additions & 5 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1714,20 +1714,28 @@ public void jsonObjectPutOnceNull() {
}

/**
* Exercise JSONObject opt(key, default) method
* Exercise JSONObject opt(key, default) method.
*/
@Test
public void jsonObjectOptDefault() {

String str = "{\"myKey\": \"myval\"}";
String str = "{\"myKey\": \"myval\", \"hiKey\": null}";
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",
Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE));
assertTrue("optInt() should return default int",
42 == jsonObject.optInt("myKey", 42));
jsonObject.optBoolean("myKey", true));
assertTrue("optInt() should return default int",
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",
Expand All @@ -1736,6 +1744,36 @@ public void jsonObjectOptDefault() {
"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",
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")));
}

/**
* Verifies that the opt methods properly convert string values.
*/
Expand Down