Recently Written · git

sbm-android

sbm bookmarks for Android: fuzzy search, share to add, sync with bm and the browser add-on

git clone https://github.com/equwal/sbm-android

Log | Files | Refs


app/src/test/java/com/equwal/sbm/FuzzyTest.kt (2453 bytes)

1 package com.equwal.sbm
2 
3 import net.jqwik.api.Arbitraries
4 import net.jqwik.api.Arbitrary
5 import net.jqwik.api.Example
6 import net.jqwik.api.ForAll
7 import net.jqwik.api.Property
8 import net.jqwik.api.Provide
9 
10 class FuzzyTest {
11     @Provide
12     fun letters(): Arbitrary<String> = Arbitraries.strings().withCharRange('a', 'z').ofMinLength(1).ofMaxLength(30)
13 
14     @Provide
15     fun early(): Arbitrary<String> = Arbitraries.strings().withCharRange('a', 'm').ofMinLength(1).ofMaxLength(30)
16 
17     @Provide
18     fun late(): Arbitrary<String> = Arbitraries.strings().withCharRange('n', 'z').ofMinLength(1).ofMaxLength(5)
19 
20     @Provide
21     fun lists(): Arbitrary<List<String>> = letters().list().ofMaxSize(10)
22 
23     @Property
24     fun `letters of the text, in order, match`(@ForAll("letters") text: String, @ForAll mask: List<Boolean>): Boolean {
25         val query = text.filterIndexed { i, _ -> mask.getOrElse(i) { false } }.ifEmpty { text.take(1) }
26         return Fuzzy.score(query, text) != null
27     }
28 
29     @Property
30     fun `a letter that is not in the text does not match`(@ForAll("early") text: String, @ForAll("late") query: String): Boolean =
31         Fuzzy.score(query, text) == null
32 
33     @Property
34     fun `every word must match`(@ForAll("letters") a: String, @ForAll("letters") b: String, @ForAll("letters") text: String): Boolean =
35         (Fuzzy.score("$a $b", text) != null) == (Fuzzy.score(a, text) != null && Fuzzy.score(b, text) != null)
36 
37     @Property
38     fun `case does not matter`(@ForAll("letters") query: String, @ForAll("letters") text: String): Boolean =
39         Fuzzy.score(query.uppercase(), text) == Fuzzy.score(query, text.uppercase())
40 
41     @Property
42     fun `the filter keeps the matches and only them`(@ForAll("lists") items: List<String>, @ForAll("letters") query: String): Boolean =
43         Fuzzy.filter(items, query) { it } .sorted() == items.filter { Fuzzy.score(query, it) != null }.sorted()
44 
45     @Example
46     fun `a blank query keeps every item in its order`() {
47         check(Fuzzy.filter(listOf("b", "a"), " ") { it } == listOf("b", "a"))
48     }
49 
50     @Example
51     fun `the word in one place ranks above scattered letters`() {
52         check(Fuzzy.filter(listOf("s x b x m", "the sbm tool"), "sbm") { it } == listOf("the sbm tool", "s x b x m"))
53     }
54 
55     @Example
56     fun `the start of a word ranks above the inside of a word`() {
57         check(Fuzzy.filter(listOf("absbm", "a sbm"), "sbm") { it } == listOf("a sbm", "absbm"))
58     }
59 }