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

Removal of magic numbers in CollectionTest #2091

Open
wants to merge 31 commits into
base: main
Choose a base branch
from

Conversation

Lhandi29
Copy link

Sync Forked Repo

@google-cla
Copy link

google-cla bot commented Mar 15, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

For more information, open the CLA check for this pull request.

@eamonnmcmanus
Copy link
Member

Can you explain what this PR is about? The description is not very helpful.

assertEquals(17, target.pop().intValue());
assertEquals(13, target.pop().intValue());
assertEquals(11, target.pop().intValue());
for (int i = 2; i > 0; i--){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be >= 0, otherwise it misses the element at value[0]. Maybe would also be good to to replace the 2 with value.length - 1.

@Lhandi29
Copy link
Author

Lhandi29 commented Apr 4, 2022

Can you explain what this PR is about? The description is not very helpful.

I wanted to remove magic numbers of this class. Magic numbers are not explicit to the project and subsequently one can forget what they correspond to. In my opinion, deleting them will allow future contributors to work more efficiently on the project.

@Lhandi29 Lhandi29 changed the title Merge pull request #2 from google/master Removal of magic numbers in CollectionTest Apr 4, 2022
@eamonnmcmanus
Copy link
Member

While I agree that magic numbers are often a hindrance, here I don't think there's much advantage to the rewrite. It's pretty obvious that the 3 in assertEquals(3, queue.size()) corresponds to the 3 elements in [10, 20, 31], and 3 explicit assertions are if anything easier to understand than a loop.

One change that I think would be worthwhile would be to use Truth for the assertions. Since PriorityQueue, Vector, and Stack all implement Iterable, you can use IterableSubject to write:

assertThat(queue).containsExactly(10, 20, 31);

instead of checking the size and removing the elements one by one. This doesn't test that the elements would be retrieved in the expected order from the queue, but they must be if PriorityQueue is behaving correctly, and it's not our job to test that there. For the other types, you could tack .inOrder() on the end of the assertion, since you do in fact expect the elements to be present in the given order.

I'm a bit surprised to see that Truth is not yet a dependency of the main gson/pom.xml project, though it is in proto/pom.xml. I think it would be worth adding it now.

Please do sign the CLA if you do decide to go ahead with this.

in CollectionTest.testCollectionOfStringsDeserialization
and
CollectionTest.testRawCollectionDeserializationNotAlllowed
in CollectionTest.testRawCollectionOfIntegersSerialization
in CollectionTest.testTopLevelCollectionOfIntegersSerialization
in CollectionTest.testTopLevelCollectionOfIntegersDeserialization
in CollectionTest.testTopLevelListOfIntegerCollectionsDeserialization
in CollectionTest.testLinkedListSerialization
in CollectionTest.testLinkedListDeserialization
in CollectionTest.testQueueSerialization
in CollectionTest.testQueueDeserialization
in CollectionTest.testPriorityQueue
in CollectionTest.testVector
in CollectionTest.testStack
in CollectionTest.testNullsInListSerialization
in CollectionTest.testNullsInListDeserialization
in CollectionTest.testCollectionOfObjectSerialization
in CollectionTest.testCollectionOfObjectWithNullSerialization
in CollectionTest.testCollectionOfStringsSerialization
in CollectionTest.testCollectionOfBagOfPrimitivesSerialization
in CollectionTest.testRawCollectionSerialization
in CollectionTest.testRawCollectionDeserializationNotAlllowed
in CollectionTest.testRawCollectionOfBagOfPrimitivesNotAllowed
in CollectionTest.testWildcardPrimitiveCollectionSerilaization
in CollectionTest.testWildcardPrimitiveCollectionDeserilaization
in CollectionTest.testFieldIsArrayList
in ObjectWithWildcardCollection.testSetSerialization
in ObjectWithWildcardCollection.testSetDeserialization
in SmallClass.testIssue1107
@@ -41,6 +45,10 @@
</configuration>
</execution>
</executions>
<configuration>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required for your change to work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants