Skip to content

Port all Junit assert to Truth asserts#2304

Merged
eamonnmcmanus merged 15 commits into
google:masterfrom
MaicolAntali:truth-asserts
Jan 31, 2023
Merged

Port all Junit assert to Truth asserts#2304
eamonnmcmanus merged 15 commits into
google:masterfrom
MaicolAntali:truth-asserts

Conversation

@MaicolAntali
Copy link
Copy Markdown
Contributor

All test inside the path gson/gson/src/test are ported to Truth

Copy link
Copy Markdown
Contributor

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

Thanks! Might be useful to adjust the tests to use Truth more effectively, for example in some cases there are assertions such as assertThat(... == ...).isTrue() (though these manual comparisons also existed before your changes).

I have started commenting on some of the cases below, but stopped after I noticed that commenting on every case is probably not worth it and useful.

Though I don't know how important Éamonn considers these adjustments. So feel free to ignore my suggestion.

Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
@MaicolAntali
Copy link
Copy Markdown
Contributor Author

MaicolAntali commented Jan 30, 2023

I have started commenting on some of the cases below, but stopped after I noticed that commenting on every case is probably not worth it and useful.

To port this amount of tests to Truth I had to use Regex (where was possible) because do it manually would have taken too long. There are a tons of things that can be changed or/and optimized but I don't know if it's worth.

My main idea was: Port the tests without change things that are not related to assert because I knew this PR was pretty huge and I wanted to keep the things much easier as possible. We could optimize tests in a next PR so it will be more easy to review

Anyway, thanks for the review and comments! 😄

Copy link
Copy Markdown
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.

We can tweak these assertions more or less indefinitely. I think it is fine if you don't. Perhaps just do the ones that @Marcono1234 commented on. This is still an improvement, and we may be able to use Error Prone to pick up further simplifications.

Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
Comment thread gson/src/test/java/com/google/gson/functional/ArrayTest.java Outdated
@MaicolAntali
Copy link
Copy Markdown
Contributor Author

we may be able to use Error Prone to pick up further simplifications.

I already looked how to setting up the maven-compiler-plugin to implement Error Prone. Probably my next PR will be related to this.

@eamonnmcmanus
Copy link
Copy Markdown
Member

OK, I think this is a good step on the way. Thanks again!

@eamonnmcmanus eamonnmcmanus merged commit 49b00d1 into google:master Jan 31, 2023
@MaicolAntali MaicolAntali deleted the truth-asserts branch February 1, 2023 07:45
Copy link
Copy Markdown
Contributor

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

Thanks for addressing my comments!

Sorry for this late follow-up review; I think in two cases the array comparison should use inOrder(). Though this might not be so important.

for (int i = 0; i < expected.length; ++i) {
assertThat(target[i]).isEqualTo(expected[i]);
}
assertThat(target).asList().containsAnyIn(expected);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should probably be containsExactly(...).inOrder() as well.

Suggested change
assertThat(target).asList().containsAnyIn(expected);
assertThat(target).asList().containsExactly(expected).inOrder();

String[] target = gson.fromJson(json, String[].class);
assertThat(target[0]).isEqualTo("Hello");
assertThat(target[1]).isEqualTo("World");
assertThat(target).asList().containsExactly("Hello", "World");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
assertThat(target).asList().containsExactly("Hello", "World");
assertThat(target).asList().containsExactly("Hello", "World").inOrder();

tibor-universe pushed a commit to getuniverse/gson that referenced this pull request Sep 14, 2024
* Port Junit assert to Truth in `com.google.gson.stream`

* Port Junit assert to Truth in `com.google.gson.regression`

* Port Junit assert to Truth in `om.google.gson.reflect`

* Port Junit assert to Truth in `com.google.gson.metrics`

* Port Junit assert to Truth in `com.google.gson.internal`

* Port Junit assert to Truth in `com.google.gson.internal.sql`

* Port Junit assert to Truth in `com.google.gson.internal.reflect`

* Port Junit assert to Truth in `com.google.gson.internal.bind`

* Port Junit assert to Truth in `com.google.gson.internal.bind.util`

* Port Junit assert to Truth in `com.google.gson.functional`

* Replaces `List.of` with `Arrays.asList` to grant legacy

* Simplify `==` asserts

* Simplify `.contain()` asserts + Minor fixes

* Simplify asserts
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