-
Notifications
You must be signed in to change notification settings - Fork 7
/
search_test.go
144 lines (138 loc) · 3.07 KB
/
search_test.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package docsite
import (
"context"
"fmt"
"net/url"
"reflect"
"strconv"
"testing"
"github.com/sourcegraph/docsite/internal/search"
"github.com/sourcegraph/docsite/internal/search/query"
"github.com/sourcegraph/docsite/markdown"
"golang.org/x/tools/godoc/vfs/httpfs"
"golang.org/x/tools/godoc/vfs/mapfs"
)
func TestSearch(t *testing.T) {
testMarkdownFuncs = markdown.FuncMap{
"test-func": func(context.Context, markdown.FuncInfo, map[string]string) (string, error) {
return "xyz", nil
},
}
t.Cleanup(func() { testMarkdownFuncs = nil })
tests := map[string]struct {
pages map[string]string
wantQueryResults map[string][]string
}{
"simple": {
pages: map[string]string{
"a.md": "a",
"b.md": "b",
},
wantQueryResults: map[string][]string{
"a": {"a.md#: a"},
"b": {"b.md#: b"},
},
},
"boost path match": {
pages: map[string]string{
"a.md": "b",
"b.md": "",
},
wantQueryResults: map[string][]string{
"b": {"b.md", "a.md#: b"},
},
},
"evaluate markdown funcs": {
pages: map[string]string{
"a.md": "<div markdown-func=test-func />",
"b.md": "",
},
wantQueryResults: map[string][]string{
"x": {"a.md#: xyz"},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
ctx := context.Background()
site := Site{
Content: versionedFileSystem{
"": httpfs.New(mapfs.New(test.pages)),
},
Base: &url.URL{Path: "/"},
}
for query, wantResults := range test.wantQueryResults {
t.Run(query, func(t *testing.T) {
result, err := site.Search(ctx, "", query)
if err != nil {
t.Fatal(err)
}
gotResults := toResultsList(result)
if !reflect.DeepEqual(gotResults, wantResults) {
t.Errorf("got results %v, want %v", gotResults, wantResults)
}
})
}
})
}
}
func toResultsList(result *search.Result) []string {
var l []string
for _, dr := range result.DocumentResults {
if len(dr.SectionResults) == 0 {
l = append(l, string(dr.ID))
}
for _, sr := range dr.SectionResults {
for _, excerpt := range sr.Excerpts {
l = append(l, fmt.Sprintf("%s#%s: %s", dr.ID, sr.ID, excerpt))
}
}
}
return l
}
func TestHighlight(t *testing.T) {
tests := []struct {
query, text string
want string
}{
{
query: "a",
text: "a b a",
want: "<strong>a</strong> b <strong>a</strong>",
},
{
query: "a",
text: "b<a>a",
want: "b<<strong>a</strong>><strong>a</strong>",
},
{
query: "a aa",
text: "aa",
want: "<strong>aa</strong>",
},
{
query: "ab bc",
text: "abc",
want: "<strong>ab</strong><strong>c</strong>",
},
{
query: "a b c ab abc bc",
text: "abc",
want: "<strong>abc</strong>",
},
{
query: "aaaAaaa",
text: "\x9f\x92\xa1 aaaaaaA",
want: "\x9f\x92\xa1 <strong>aaaaaaA</strong>",
},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
query := query.Parse(test.query)
got := highlight(query, test.text)
if string(got) != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
})
}
}