LeoColman/kotest-android

Support matching string resources in assertions

Open

#33 opened on Jan 6, 2025

View on GitHub
 (0 comments) (1 reaction) (0 assignees)Kotlin (4 forks)auto 404
enhancementgood first issue

Repository metrics

Stars
 (36 stars)
PR merge metrics
 (PR metrics pending)

Description

infix fun TextView.shouldHaveText(text: String) = this should haveText(text)
infix fun TextView.shouldNotHaveText(text: String) = this shouldNot haveText(text)

fun haveText(text: String) = object : Matcher<TextView> {
  override fun test(value: TextView) = MatcherResult(
    text.contentEquals(value.text),
    "TextView should have text $text but was ${value.text}",
    "TextView should not have text $text, but had"
  )
}

This matcher requires a hardcoded String to check text, but TextViews often get their text from string resources. String resources can also have formatting tags, which makes comparing to a hardcoded String impractical. There should be a way to compare text and string attributes with string resources. This goes for other matchers that compare text too, such as View.hasContentDescription.

Contributor guide