-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_test.go
103 lines (92 loc) · 1.72 KB
/
query_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
// Copyright 2020 by Chris Palmer (https://noncombatant.org)
// SPDX-License-Identifier: GPL-3.0
package main
import (
"testing"
)
var (
rawTerms = `Foo bar kw:term kw2 : term2 -greeb graggle kw3: -"term 3"`
expectedParsedTerms = []string{
"Foo",
"bar",
"kw",
":",
"term",
"kw2",
":",
"term2",
"-",
"greeb",
"graggle",
"kw3",
":",
"-",
"term 3",
}
expectedQueries = []Query{
{"", "Foo", false},
{"", "bar", false},
{"kw", "term", false},
{"kw2", "term2", false},
{"", "greeb", true},
{"", "graggle", false},
{"kw3", "term 3", true},
}
)
func stringSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func testParseTermsHelper(t *testing.T) []string {
terms := parseTerms(rawTerms)
if !stringSlicesEqual(expectedParsedTerms, terms) {
t.Error(expectedParsedTerms, terms)
}
return terms
}
func TestParseTerms(t *testing.T) {
testParseTermsHelper(t)
}
func TestReconstructQueries(t *testing.T) {
terms := testParseTermsHelper(t)
queries := reconstructQueries(terms)
if len(expectedQueries) != len(queries) {
t.Errorf("mismatched lengths")
}
for i := range expectedQueries {
if expectedQueries[i] != queries[i] {
t.Errorf("%v != %v\n", expectedQueries[i], queries[i])
}
}
}
func TestNormalizeStringForSearch(t *testing.T) {
input := []string{
"Monáe",
"monÁe",
"MONÁE",
"gürg",
"ALLCAPS",
}
expected := []string{
"monae",
"monae",
"monae",
"gurg",
"allcaps",
}
for i, v := range input {
received := normalizeStringForSearch(v)
ex := expected[i]
if received != ex {
t.Errorf("%q != %q", ex, received)
}
t.Logf("%q == %q", ex, received)
}
}