Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Replace additional function with custom matcher for better assertion errors. #911

Merged
merged 13 commits into from
Oct 20, 2023

Conversation

Keworker
Copy link
Contributor

@Keworker Keworker commented Aug 6, 2023

Before this PR if GardenPlantingTest throws an error, it were possible only to see errors like this:

java.lang.AssertionError: 
Expected: <2024>
     but: was <2023>
	 
java.lang.AssertionError: 
Expected: <9>
     but: was <8>
	 
java.lang.AssertionError: 
Expected: <7>
     but: was <6>

I add custom matcher and now it's easier to read assertion errors stack traces.

java.lang.AssertionError: 
Expected: 06.08.2024
     but: was 06.08.2023
	 
java.lang.AssertionError: 
Expected: 06.09.2023
     but: was 06.08.2023
	 
java.lang.AssertionError: 
Expected: 07.08.2023
     but: was 06.08.2023

Additional advantage of this way is that asserts become more readable.

I also add visibility modifier internal for all unit-test classes, because they should have less visibility.

@Keworker Keworker requested a review from a team as a code owner August 6, 2023 14:36
@Keworker Keworker requested a review from mlykotom August 6, 2023 14:36
@Keworker
Copy link
Contributor Author

@mlykotom , excuse me, are you here?

@Keworker
Copy link
Contributor Author

Keworker commented Sep 9, 2023

I'm sorry, but i think that @mlykotom won't review it. @arriolac , can you review this PR, please?

}

override fun matchesSafely(actual: Calendar?, mismatchDescription: Description?): Boolean {
return (actual?.let {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit difficult to read. I recommend returning early using an if statement to check if actual is null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was fixed

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking more along the lines of:

if (actual == null)
  return false


if (actual.get(YEAR) == expected.get(YEAR) && actual.get(MONTH) == expected.get(MONTH) && actual.get(DAY_OF_MONTH) == expected.get(DAY_OF_MONTH)) 
  return true


mismatchDescription?.appendText("was ")?.appendText(actual?.time?.let { formatter.format(it) } ?: "null")
return false
}

@Keworker
Copy link
Contributor Author

@arriolac excuse me, can you check this PR again (resolve conversations if problems are solved or left new comments) ?

Copy link
Contributor

@arriolac arriolac left a comment

Choose a reason for hiding this comment

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

Please make AGP version update in a separate PR. Otherwise, LGTM

@@ -16,7 +16,7 @@
[versions]
accessibilityTestFramework = "4.0.0"
activityCompose = "1.7.2"
androidGradlePlugin = "8.0.2"
androidGradlePlugin = "8.1.1"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert. While I agree with this change, I'd prefer to have it as a separate PR and commit.

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:
#923

}

override fun matchesSafely(actual: Calendar?, mismatchDescription: Description?): Boolean {
return (actual?.let {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking more along the lines of:

if (actual == null)
  return false


if (actual.get(YEAR) == expected.get(YEAR) && actual.get(MONTH) == expected.get(MONTH) && actual.get(DAY_OF_MONTH) == expected.get(DAY_OF_MONTH)) 
  return true


mismatchDescription?.appendText("was ")?.appendText(actual?.time?.let { formatter.format(it) } ?: "null")
return false
}

Copy link
Contributor

@arriolac arriolac left a comment

Choose a reason for hiding this comment

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

LGTM

@arriolac arriolac merged commit 52e809d into android:main Oct 20, 2023
2 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants