-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes_test.go
112 lines (98 loc) · 2.3 KB
/
notes_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
package nve
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
var notes *Notes
func init() {
notes = NewNotes(NotesConfig{
Filepath: "./test_data",
DBPath: generateTempDBPath(),
})
}
func TestSearch(t *testing.T) {
/*
Following tests rely on the fixture files within "./test_data"
*/
testCases := []struct {
name string
input string
expected []string
}{
{
name: "handles empty input",
input: "",
expected: []string{
"test_data/apples in zoo.md",
"test_data/bananas_in_zoo.md",
"test_data/cats.md",
"test_data/nested/cucumbers.md",
"test_data/zebra in zoo.md",
},
},
{
name: "handles quote characters",
input: "\"",
expected: []string{},
},
{
name: "locates no files",
input: "nothing-matches-this-string~~",
expected: []string{},
},
{
name: "locates files by partial name match",
input: "apple",
expected: []string{"test_data/apples in zoo.md"},
},
{
name: "locates files by fragment match",
input: "app zoo",
expected: []string{"test_data/apples in zoo.md"},
},
{
name: "locates files by content match",
input: "new york",
expected: []string{"test_data/apples in zoo.md"},
},
{
name: "locates files by partial content match",
input: "yor",
expected: []string{"test_data/apples in zoo.md"},
},
{
name: "locates files by case-insensitive content match",
input: "YOR",
expected: []string{"test_data/apples in zoo.md"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
results, err := notes.Search(tc.input)
assert.NoError(t, err)
// sort both arrays before assertion
sort.Strings(tc.expected)
sort.Strings(results)
assert.Equal(t, tc.expected, results)
})
}
}
type mockObserver struct {
lastResult []*SearchResult
}
func (m *mockObserver) SearchResultsUpdate(notes *Notes) {
m.lastResult = notes.LastSearchResults
}
func TestNotifyObservers(t *testing.T) {
mock := mockObserver{}
notes.RegisterObservers(&mock)
notes.Search("seattle")
if assert.Len(t, mock.lastResult, 1) {
res := mock.lastResult[0]
// assert snippet
assert.Equal(t, "new york\n**seattle**\n", res.Snippet)
// assert filename
assert.Equal(t, "test_data/apples in zoo.md", res.Filename)
}
}