commonTest.highlighting.TestHighlightTokenizer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of instantsearch-core Show documentation
Show all versions of instantsearch-core Show documentation
InstantSearch Android is a library providing widgets and helpers to help you build the best instant-search experience on Android with Algolia. It is built on top of Algolia's Kotlin API Client to provide you a high-level solution to quickly build various search interfaces.
package highlighting
import com.algolia.instantsearch.core.highlighting.HighlightTokenizer
import shouldBeFalse
import shouldBeTrue
import shouldEqual
import kotlin.test.Test
class TestHighlightTokenizer {
/*
* matchStart
* matchEnd
* matchMiddle
* */
@Test
fun matchNone() = shouldMatch(
"This John Doe looks like Johnathan.",
1
)
@Test
fun matchAll() = shouldMatch(
"JohnJohn",
2, listOf("John", "John")
)
@Test
fun matchStart() = shouldMatch(
"John is a nice guy.",
2, listOf("John")
)
@Test
fun matchEnd() = shouldMatch(
"I love John",
2, listOf("John")
)
@Test
fun matchTwiceMiddle() = shouldMatch(
"This John Doe looks like Johnathan.",
5, listOf("John", "John")
)
@Test
fun matchWithCustomTags() =
shouldMatch(
"This ^Game$ is really ^funny$, don't you ^think$?",
7, listOf("Game", "funny", "think"),
Pair("^", "$")
)
private fun shouldMatch(
input: String,
expectNbParts: Int = 1,
expectHighlightedParts: List = listOf(),
tags: Pair? = null
) {
val tokenizer = if (tags != null) HighlightTokenizer(tags.first, tags.second) else HighlightTokenizer()
val output = tokenizer(input)
output.original shouldEqual input
output.tokens.size shouldEqual expectNbParts
output.highlightedTokens shouldEqual expectHighlightedParts
if (expectHighlightedParts.isEmpty()) { // No part should be highlighted
output.tokens.any { it.highlighted }.shouldBeFalse()
} else if (expectNbParts == expectHighlightedParts.size) { // All tokens should be highlighted
output.tokens.all { it.highlighted }.shouldBeTrue()
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy