Skip to content
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

Improving opt test to include missing values #54

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 28 additions & 7 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1714,28 +1714,49 @@ public void jsonObjectPutOnceNull() {
}

/**
* Exercise JSONObject opt(key, default) method
* Exercise JSONObject opt(key, default) method, when type can't be converted
*/
@Test
public void jsonObjectOptDefault() {

String str = "{\"myKey\": \"myval\"}";
JSONObject jsonObject = new JSONObject(str);

assertTrue("optBoolean() should return default boolean",
assertTrue("optBoolean() should return default boolean when type doesn't match",
Boolean.TRUE == jsonObject.optBoolean("myKey", Boolean.TRUE));
assertTrue("optInt() should return default int",
assertTrue("optInt() should return default int when type doesn't match",
42 == jsonObject.optInt("myKey", 42));
assertTrue("optInt() should return default int",
assertTrue("optInt() should return default int when type doesn't match",
42 == jsonObject.optInt("myKey", 42));
assertTrue("optLong() should return default long",
assertTrue("optLong() should return default long when type doesn't match",
42 == jsonObject.optLong("myKey", 42));
assertTrue("optDouble() should return default double",
assertTrue("optDouble() should return default double when type doesn't match",
42.3 == jsonObject.optDouble("myKey", 42.3));
}

/**
* Exercise JSONObject opt(key, default) method with missing values
*/
@Test
public void jsonObjectOptMissing() {

String str = "{\"myKey\": \"myval\"}";
JSONObject jsonObject = new JSONObject(str);

assertTrue("optBoolean() should return default boolean",
Boolean.TRUE == jsonObject.optBoolean("hiKey", Boolean.TRUE));
assertTrue("optInt() should return default int",
42 == jsonObject.optInt("hiKey", 42));
assertTrue("optInt() should return default int",
42 == jsonObject.optInt("hiKey", 42));
assertTrue("optLong() should return default long",
42 == jsonObject.optLong("hiKey", 42));
assertTrue("optDouble() should return default double",
42.3 == jsonObject.optDouble("hiKey", 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