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

Improve assertion error messages by adding mismatch descriptions #461

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ object BaristaAssistiveTextAssertions {
private fun matchAssistiveText(expectedAssistiveText: String): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("with helper text: ").appendText(expectedAssistiveText)
description.appendText("with helper text: ").appendValue(expectedAssistiveText)
}

override fun matchesSafely(item: View): Boolean {
return when (item) {
is TextInputLayout -> expectedAssistiveText == item.helperText.toString()
return item.tryGetHelperText() == expectedAssistiveText
}

override fun describeMismatchSafely(item: View, mismatchDescription: Description) {
mismatchDescription.appendText("with helper text: ").appendValue(item.tryGetHelperText())
}

private fun View.tryGetHelperText(): String? {
return when (this) {
is TextInputLayout -> helperText?.toString()
else -> {
throw UnsupportedOperationException("View of class ${item.javaClass.simpleName} not supported")
throw UnsupportedOperationException("View of class ${javaClass.simpleName} not supported")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,26 @@ object BaristaContentDescriptionAssertions {
override fun matchesSafely(item: View): Boolean {
return item.contentDescription.isNotBlank()
}

override fun describeMismatchSafely(item: View, mismatchDescription: Description) {
mismatchDescription.appendText("with content description: ").appendValue(item.contentDescription.toString())
}
}
}

private fun matchContentDescription(expectedContentDescription: String): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("with content description: ").appendText(expectedContentDescription)
description.appendText("with content description: ").appendValue(expectedContentDescription)
}

override fun matchesSafely(item: View): Boolean {
return item.contentDescription == expectedContentDescription
}

override fun describeMismatchSafely(item: View, mismatchDescription: Description) {
mismatchDescription.appendText("with content description: ").appendValue(item.contentDescription.toString())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ object BaristaErrorAssertions {
private fun matchError(expectedError: String): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("with error: ").appendText(expectedError)
description.appendText("with error: ").appendValue(expectedError)
}

override fun describeMismatchSafely(item: View, mismatchDescription: Description) {
mismatchDescription.appendText("with error: ").appendValue(item.tryGetErrorText())
}

override fun matchesSafely(item: View): Boolean {
return when (item) {
is TextView -> expectedError == item.error.toString()
is TextInputLayout -> expectedError == item.error.toString()
else -> {
throw UnsupportedOperationException("View of class ${item.javaClass.simpleName} not supported")
}
}
return expectedError == item.tryGetErrorText()
}
}
}
Expand All @@ -79,14 +77,22 @@ object BaristaErrorAssertions {
description.appendText("without error")
}

override fun describeMismatchSafely(item: View, mismatchDescription: Description) {
mismatchDescription.appendText("with error: ").appendValue(item.tryGetErrorText())
}

override fun matchesSafely(item: View): Boolean {
return when (item) {
is TextView -> item.error.isNullOrEmpty()
is TextInputLayout -> item.error.isNullOrEmpty()
else -> {
throw UnsupportedOperationException("View of class ${item.javaClass.simpleName} not supported")
}
}
return item.tryGetErrorText().isNullOrEmpty()
}
}
}

private fun View.tryGetErrorText(): String? {
return when (this) {
is TextView -> error?.toString()
is TextInputLayout -> error?.toString()
else -> {
throw UnsupportedOperationException("View of class ${javaClass.simpleName} not supported")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,31 @@ object BaristaHintAssertions {
private fun matchHint(expectedHint: String): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("with hint: ").appendText(expectedHint)
description.appendText("with hint: ").appendValue(expectedHint)
}

override fun matchesSafely(item: View): Boolean {
return when (item) {
return item.tryGetHintText() == expectedHint
}

override fun describeMismatchSafely(item: View, mismatchDescription: Description) {
mismatchDescription.appendText("with hint: ").appendValue(item.tryGetHintText())
}

private fun View.tryGetHintText(): String? {
return when (this) {
is TextInputLayout -> {
expectedHint == item.hint.toString()
hint?.toString()
}
is TextInputEditText -> {
val hint = ((item.parent as FrameLayout).parent as TextInputLayout).hint
expectedHint == hint
val hint = ((this.parent as FrameLayout).parent as TextInputLayout).hint
hint?.toString()
}
is EditText -> {
item.hint == expectedHint
hint?.toString()
}
else -> {
throw UnsupportedOperationException("View of class ${item.javaClass.simpleName} not supported")
throw UnsupportedOperationException("View of class ${javaClass.simpleName} not supported")
}
}
}
Expand Down