File: score_test.go

package info (click to toggle)
kitty 0.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 27,468 kB
  • sloc: ansic: 84,285; python: 57,992; objc: 5,432; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (99 lines) | stat: -rw-r--r-- 2,536 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>

package subseq

import (
	"fmt"
	"github.com/kovidgoyal/kitty/tools/utils"
	"strconv"
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
)

var _ = fmt.Print

func TestSubseq(t *testing.T) {
	var positions [][]int
	sort_by_score := false

	simple := func(items, query string, expected ...string) {
		matches := ScoreItems(query, utils.Splitlines(items), Options{})
		if sort_by_score {
			matches = utils.StableSort(matches, func(a, b *Match) int {
				if b.Score < a.Score {
					return -1
				}
				if b.Score > a.Score {
					return 1
				}
				return 0
			})
		}
		actual := make([]string, 0, len(matches))
		actual_positions := make([][]int, 0, len(matches))
		for _, m := range matches {
			if m.Score > 0 {
				actual = append(actual, m.Text)
				actual_positions = append(actual_positions, m.Positions)
			}
		}
		if expected == nil {
			expected = []string{}
		}
		if diff := cmp.Diff(expected, actual); diff != "" {
			t.Fatalf("Failed for items: %v\nMatches: %#v\n%s", utils.Splitlines(items), matches, diff)
		}
		if positions != nil {
			if diff := cmp.Diff(positions, actual_positions); diff != "" {
				t.Fatalf("Failed positions for items: %v\n%s", utils.Splitlines(items), diff)
			}
			positions = nil
		}
	}
	simple("test\nxyz", "te", "test")
	simple("abc\nxyz", "ba")
	simple("abc\n123", "abc", "abc")
	simple("test\nxyz", "Te", "test")
	simple("test\nxyz", "XY", "xyz")
	simple("test\nXYZ", "xy", "XYZ")
	simple("test\nXYZ", "mn")

	positions = [][]int{{0, 2}, {0, 1}}
	simple("abc\nac", "ac", "abc", "ac")
	positions = [][]int{{0}}
	simple("abc\nv", "a", "abc")
	positions = [][]int{{len("汉"), 7}}
	simple("汉a字b\nxyz", "ab", "汉a字b")

	sort_by_score = true
	// Match at start
	simple("archer\nelementary", "e", "elementary", "archer")
	// Match at level factor
	simple("xxxy\nxx/y", "y", "xx/y", "xxxy")
	// CamelCase
	simple("xxxy\nxxxY", "y", "xxxY", "xxxy")
	// Total length
	simple("xxxya\nxxxy", "y", "xxxy", "xxxya")
	// Distance
	simple("abbc\nabc", "ac", "abc", "abbc")
	// Extreme chars
	simple("xxa\naxx", "a", "axx", "xxa")
	// Highest score
	positions = [][]int{{3}}
	simple("xa/a", "a", "xa/a")

	sort_by_score = false
	items := make([]string, 256)
	for i := range items {
		items[i] = strconv.Itoa(i)
	}
	expected := make([]string, 0, len(items))
	for _, x := range items {
		if strings.ContainsRune(x, rune('2')) {
			expected = append(expected, x)
		}
	}
	simple(strings.Join(items, "\n"), "2", expected...)
}