square / assertj-android

A set of AssertJ helpers geared toward testing Android.
https://square.github.io/assertj-android/
Apache License 2.0
1.58k stars 156 forks source link

hasText() sometimes returns false negatives #49

Closed pgoodwin closed 11 years ago

pgoodwin commented 11 years ago

Sometimes fails with messages like: "Expected text but was " This happens because the text is actually a CharSequence that doesn't compare nicely with String. In my case it was a SpannableStringBuilder. Maybe a nice way to fix this would be to check whether the 'text' parameter is actually a String and call toString() on actualText before doing the comparison:

public S hasText(CharSequence text) { isNotNull(); CharSequence actualText = actual.getText(); if (text instanceOf String) actualText = actualText.toString(); assertThat(actualText) // .overridingErrorMessage("Expected text <%s> but was <%s>.", text, actualText) // .isEqualTo(text); return myself; }

That way in the 1% case where someone really wants to ensure that a text field has some exact configuration of a SpannableStringBuilder as it's text they can still build one up and pass it in, while the rest of the time we'll get the String comparison we'd intuitively expect.

JakeWharton commented 11 years ago

We have hasTextString for this.

JakeWharton commented 11 years ago

I am thinking of flipping the impls for a future version, though.

pgoodwin commented 11 years ago

Whoa, that was fast. I looked for something like that. Not hard enough it seems. Thanks.

pgoodwin commented 11 years ago

hasTextString Doesn't seem to be on TextViewAssert

JakeWharton commented 11 years ago

https://github.com/square/fest-android/blob/master/src/main/java/org/fest/assertions/api/android/widget/AbstractTextViewAssert.java#L484-496

pgoodwin commented 11 years ago

is there a snapshot release I can put in my pom.xml to get this?

On Thu, Mar 21, 2013 at 5:31 PM, Jake Wharton notifications@github.comwrote:

https://github.com/square/fest-android/blob/master/src/main/java/org/fest/assertions/api/android/widget/AbstractTextViewAssert.java#L484-496

— Reply to this email directly or view it on GitHubhttps://github.com/square/fest-android/issues/49#issuecomment-15274937 .

JakeWharton commented 11 years ago

It's in 1.0.3, the latest. On Mar 21, 2013 9:52 PM, "Phil Goodwin" notifications@github.com wrote:

is there a snapshot release I can put in my pom.xml to get this?

On Thu, Mar 21, 2013 at 5:31 PM, Jake Wharton notifications@github.comwrote:

https://github.com/square/fest-android/blob/master/src/main/java/org/fest/assertions/api/android/widget/AbstractTextViewAssert.java#L484-496

— Reply to this email directly or view it on GitHub< https://github.com/square/fest-android/issues/49#issuecomment-15274937> .

— Reply to this email directly or view it on GitHubhttps://github.com/square/fest-android/issues/49#issuecomment-15281188 .