Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Made IndexMatcher reusable #200

Merged
merged 2 commits into from
Feb 4, 2020
Merged
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 @@ -37,38 +37,11 @@ class ViewBuilder {
private val viewMatchers = arrayListOf<Matcher<View>>()

/**
* Matches only view at given [index], if there are multiple views that matches
*
* IMPORTANT: this matcher is single-use only, since it does not reset it's
* index counter due to specific espresso's matching process. Thus only one action
* and/or assertion can be performed on such a [KView][com.agoda.kakao.common.views.KView].
*
* If you need to match view with index multiple times, each time you should match
* with new instance of [withIndex]
*
* Take a look at the example:
* ```
* class InputScreen : Screen<InputScreen>() {
* fun inputLayout(lambda: KEditText.() -> Unit) = KEditText { withIndex(0, { withId(R.id.input_layout) }) }.invoke(lambda)
* }
*
* @Test
* fun test() {
* screen {
* inputLayout {
* replaceText("EXAMPLE")
* }
*
* inputLayout {
* hasAnyText()
* }
* }
* }
* ```
*
* @param index Index of the view to match
* @param function [ViewBuilder] that will result in matcher
*/
* Matches only view at given [index], if there are multiple views that matches
*
* @param index Index of the view to match
* @param function [ViewBuilder] that will result in matcher
*/
fun withIndex(index: Int, function: ViewBuilder.() -> Unit) {
viewMatchers.add(IndexMatcher(ViewBuilder().apply(function).getViewMatcher(), index))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ import org.hamcrest.TypeSafeMatcher
*/
class IndexMatcher(private val matcher: Matcher<View>, private val index: Int) : TypeSafeMatcher<View>() {
private var currentIndex = 0
private val seen = mutableSetOf<View>()

override fun describeTo(desc: Description) {
desc.appendText("${index}th view with: ")
.appendDescriptionOf(matcher)
}

public override fun matchesSafely(view: View): Boolean =
matcher.matches(view) && currentIndex++ == index
public override fun matchesSafely(view: View): Boolean {
if (seen.contains(view)) {
currentIndex = 0
seen.clear()
}

seen.add(view)
return matcher.matches(view) && currentIndex++ == index
}
}