-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Test cases for testing the exceptional behavior of JsonArray get... methods #1909
Conversation
… getAsDouble, getAsInt, getAsJsonArray, getAsJsonObject, getAsLong, and getAsString methods of JsonArray class. These test cases, which we wrote according to the specified behavior of each method, that helped us in identifying the documentation bugs in JsonArray and JsonElement classes, which we submitted issues for (Issue google#1908). Note that we have adapted these test cases based on similar tests from the JSON-java project (https://github.com/stleary/JSON-java).
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here with What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
@googlebot I signed it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope the review comments are useful; note that I am not a member of this project so feel free to consider these only as suggestions.
JsonArray jsonArray = (JsonArray) JsonParser.parseString(arrayStr); | ||
try { | ||
jsonArray.get(10).getAsBoolean(); | ||
assertTrue("expected getBoolean to fail", false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of assertTrue(..., false)
it might be better to use fail(...)
; applies to other tests as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to fail(...)
"JsonObject",e.getMessage()); | ||
} | ||
try { | ||
jsonArray.get(-1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except for this test, all other ones are not specific to JsonArray, it might be better to put them to JsonPrimitiveTest
(or maybe there exists another more fitting test class).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed how the methods are used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Also my comment was incorrect, I missed that the getAs...
methods of JsonArray
actually try to convert the array element in case the array contains only a single element.
@@ -99,4 +102,65 @@ public void testDeepCopy() { | |||
assertEquals(1, original.get(0).getAsJsonArray().size()); | |||
assertEquals(0, copy.get(0).getAsJsonArray().size()); | |||
} | |||
|
|||
public void testFailedGetArrayValues() { | |||
String arrayStr = "[" + "true," + "false," + "\"true\"," + "\"false\"," + "\"hello\"," + "23.45e-4," + "\"23.45\"," + "42," + "\"43\"," + "[" + "\"world\"" + "]," + "{" + "\"key1\":\"value1\"," + "\"key2\":\"value2\"," + "\"key3\":\"value3\"," + "\"key4\":\"value4\"" + "}," + "0," + "\"-1\"" + "]"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all values of this array are used; might be good to remove unused values to make this test easier to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the unused values.
assertTrue("expected getBoolean to fail", false); | ||
} catch (UnsupportedOperationException e) { | ||
assertEquals("Expected an exception message", | ||
"JsonObject",e.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really minor, but for consistent code style there should probably be a space:
"JsonObject",e.getMessage()); | |
"JsonObject", e.getMessage()); |
(applies to other tests as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved the formatting issues.
…ods [use fail(...), use JsonArray methods, remove unused values, formatting, google#1909, google#1908]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! Just one small comment.
@@ -20,6 +20,9 @@ | |||
|
|||
import com.google.gson.common.MoreAsserts; | |||
|
|||
import static org.junit.Assert.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me why these imports are needed. Aren't assertEquals
etc inherited from TestCase
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed those imports.
Test cases for testing the exceptional behavior of
get
,getAsBoolean
,getAsDouble
,getAsInt
,getAsJsonArray
,getAsJsonObject
,getAsLong
, andgetAsString
methods ofJsonArray
class. These test cases, which we wrote according to the specified behavior of each method, that helped us in identifying the documentation bugs inJsonArray
andJsonElement
classes, which we submitted issues for (Issue #1908). Note that we have adapted these test cases based on similar tests from the JSON-java project.