forked from rhysd/notes-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory_test.go
More file actions
202 lines (180 loc) · 4.2 KB
/
category_test.go
File metadata and controls
202 lines (180 loc) · 4.2 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package notes
import (
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)
func configForCategoryTest(subdir string) *Config {
cwd, err := os.Getwd()
panicIfErr(err)
return &Config{
HomePath: filepath.Join(cwd, "testdata", "category", subdir),
}
}
func TestCollectCategoriesOnlyFirstCategory(t *testing.T) {
cfg := configForCategoryTest("normal")
cats, err := CollectCategories(cfg, OnlyFirstCategory)
if err != nil {
t.Fatal(err)
}
if len(cats) != 1 {
t.Fatal(len(cats), "categories found", cats)
}
var name string
var cat *Category
for name, cat = range cats {
break
}
if cat == nil || name == "" {
t.Fatal(name, cat)
}
if len(cat.NotePaths) != 1 {
t.Fatal("Only first note should be collected", cat.NotePaths)
}
}
func TestCollectCategoriesOK(t *testing.T) {
cfg := configForCategoryTest("normal")
cats, err := CollectCategories(cfg, 0)
if err != nil {
t.Fatal(err)
}
// Check category names
{
want := []string{
"a",
"a/c",
"b",
}
have := cats.Names()
sort.Strings(have)
if !reflect.DeepEqual(want, have) {
t.Fatal("All categories are not detected. Wanted", want, "but have", have)
}
for n, c := range cats {
if n != c.Name {
t.Fatal("Name and its name mismatches", n, c.Name)
}
p := filepath.Join(cfg.HomePath, filepath.FromSlash(c.Name))
if p != c.Path {
t.Fatal("Name does not match to its path", c.Name, c.Path)
}
}
}
// Check note paths
{
want := []string{
"a/1.md",
"a/4.md",
"a/c/3.md",
"a/c/5.md",
"b/2.md",
"b/6.md",
}
for i, w := range want {
want[i] = filepath.Join(cfg.HomePath, filepath.FromSlash(w))
}
have := make([]string, 0, 6)
for _, c := range cats {
have = append(have, c.NotePaths...)
}
sort.Strings(have)
if !reflect.DeepEqual(want, have) {
t.Fatal("Note paths are unexpected. Wanted", want, "but have", have)
}
}
}
func TestCategoryNotesOK(t *testing.T) {
name := "a/c"
cfg := configForCategoryTest("normal")
dir := filepath.Join(cfg.HomePath, filepath.FromSlash(name))
cat := &Category{
Path: dir,
Name: name,
NotePaths: []string{
filepath.Join(dir, "3.md"),
filepath.Join(dir, "5.md"),
},
}
notes, err := cat.Notes(cfg)
if err != nil {
t.Fatal(err)
}
for _, note := range notes {
if note.Category != name {
t.Fatal("Category mismatch", note.Category, name)
}
if f := note.File; f != "3.md" && f != "5.md" {
t.Fatal("Unexpected note file loaded", f)
}
}
}
func TestCategoriesNotesOK(t *testing.T) {
cfg := configForCategoryTest("normal")
cats, err := CollectCategories(cfg, 0)
if err != nil {
t.Fatal(err)
}
want := make([]string, 0, 6)
for _, c := range cats {
want = append(want, c.NotePaths...)
}
sort.Strings(want)
notes, err := cats.Notes(cfg)
if err != nil {
t.Fatal(err)
}
have := make([]string, 0, len(notes))
for _, note := range notes {
c := filepath.Join(cfg.HomePath, filepath.FromSlash(note.Category), note.File)
have = append(have, c)
}
sort.Strings(have)
if !reflect.DeepEqual(want, have) {
t.Fatal("Wanted", want, "but have", have)
}
}
func TestCollectCategoriesNoHome(t *testing.T) {
cfg := &Config{HomePath: "/path/to/somewhere/unknown"}
_, err := CollectCategories(cfg, 0)
if err == nil || !strings.Contains(err.Error(), "Cannot read home") {
t.Fatal("Got unexpected", err)
}
}
func TestCategoryNotesError(t *testing.T) {
cfg := configForCategoryTest("fail")
name := "missing-created"
dir := filepath.Join(cfg.HomePath, name)
cat := &Category{
Path: dir,
Name: name,
NotePaths: []string{
filepath.Join(dir, "1.md"),
},
}
if _, err := cat.Notes(cfg); err == nil || !strings.Contains(err.Error(), "Missing metadata") {
t.Fatal("Got unexpected", err)
}
}
func TestCategoriesNotesError(t *testing.T) {
cfg := configForCategoryTest("fail")
cats, err := CollectCategories(cfg, 0)
if err != nil {
t.Fatal(err)
}
if _, err := cats.Notes(cfg); err == nil || !strings.Contains(err.Error(), "Missing metadata") {
t.Fatal("Got unexpected", err)
}
}
func TestCategoriesNoNote(t *testing.T) {
cfg := configForCategoryTest("empty")
cats, err := CollectCategories(cfg, 0)
if err != nil {
t.Fatal(err)
}
if len(cats) > 0 {
t.Fatal("No note should mean no category:", cats)
}
}