-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Replace additional function with custom matcher for better assertion errors. #911
Conversation
This project doesn't use Jetpack DataStore.
@mlykotom , excuse me, are you here? |
app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt
Outdated
Show resolved
Hide resolved
} | ||
|
||
override fun matchesSafely(actual: Calendar?, mismatchDescription: Description?): Boolean { | ||
return (actual?.let { |
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.
This is a bit difficult to read. I recommend returning early using an if
statement to check if actual
is null
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 was fixed
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 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
}
app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt
Outdated
Show resolved
Hide resolved
@arriolac excuse me, can you check this PR again (resolve conversations if problems are solved or left new comments) ? |
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.
Please make AGP version update in a separate PR. Otherwise, LGTM
gradle/libs.versions.toml
Outdated
@@ -16,7 +16,7 @@ | |||
[versions] | |||
accessibilityTestFramework = "4.0.0" | |||
activityCompose = "1.7.2" | |||
androidGradlePlugin = "8.0.2" | |||
androidGradlePlugin = "8.1.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.
Please revert. While I agree with this change, I'd prefer to have it as a separate PR and commit.
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:
#923
} | ||
|
||
override fun matchesSafely(actual: Calendar?, mismatchDescription: Description?): Boolean { | ||
return (actual?.let { |
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 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
}
app/src/test/java/com/google/samples/apps/sunflower/test/HasSameDateWith.kt
Outdated
Show resolved
Hide resolved
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.
LGTM
Before this PR if
GardenPlantingTest
throws an error, it were possible only to see errors like this:I add custom matcher and now it's easier to read assertion errors stack traces.
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.