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

Test cases for testing the exceptional behavior of JsonArray get... methods #1909

Merged
merged 3 commits into from Aug 7, 2021

Conversation

HiFromAjay
Copy link
Contributor

Test cases for testing the exceptional behavior of get, getAsBoolean, 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 #1908). Note that we have adapted these test cases based on similar tests from the JSON-java project.

… 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).
@google-cla
Copy link

google-cla bot commented Jun 11, 2021

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 @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Jun 11, 2021
@HiFromAjay
Copy link
Contributor Author

@googlebot I signed it!

@google-cla google-cla bot added cla: yes and removed cla: no labels Jun 11, 2021
Copy link
Collaborator

@Marcono1234 Marcono1234 left a 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);
Copy link
Collaborator

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.

Copy link
Contributor Author

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);
Copy link
Collaborator

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).

Copy link
Contributor Author

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.

Copy link
Collaborator

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\"" + "]";
Copy link
Collaborator

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.

Copy link
Contributor Author

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());
Copy link
Collaborator

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:

Suggested change
"JsonObject",e.getMessage());
"JsonObject", e.getMessage());

(applies to other tests as well)

Copy link
Contributor Author

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]
Copy link
Member

@eamonnmcmanus eamonnmcmanus left a 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.*;
Copy link
Member

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?

Copy link
Contributor Author

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.

@eamonnmcmanus eamonnmcmanus merged commit 9edaeb3 into google:master Aug 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants