forked from rhysd/notes-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_tags_test.go
More file actions
126 lines (112 loc) · 2.31 KB
/
cmd_tags_test.go
File metadata and controls
126 lines (112 loc) · 2.31 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
package notes
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
func TestTagsCmd(t *testing.T) {
cwd, err := os.Getwd()
panicIfErr(err)
for _, tc := range []struct {
what string
cat string
want string
subdir string
}{
{
what: "flat and all categories",
cat: "",
want: "a-bit-long\nbar\nfoo\nfuture\n",
subdir: "normal",
},
{
what: "flat and specific category",
cat: "a",
want: "bar\nfoo\n",
subdir: "normal",
},
{
what: "nested and all categories",
cat: "",
want: "a\nb\nbar\nc\nd\nfoo\npiyo\n",
subdir: "nested",
},
{
what: "nested and specific category",
cat: "b",
want: "b\nfoo\n",
subdir: "nested",
},
{
what: "nested and specific nested category",
cat: "a/d/e",
want: "a\npiyo\n",
subdir: "nested",
},
} {
t.Run(tc.cat, func(t *testing.T) {
cfg := &Config{
HomePath: filepath.Join(cwd, "testdata", "list", tc.subdir),
}
var buf bytes.Buffer
cmd := TagsCmd{
Category: tc.cat,
Config: cfg,
Out: &buf,
}
if err := cmd.Do(); err != nil {
t.Fatal(err)
}
have := buf.String()
if have != tc.want {
t.Fatal("wanted:", tc.want, "but have", have)
}
})
}
}
func TestTagsInvalidHome(t *testing.T) {
cfg := &Config{HomePath: "/unknown/path/to/home"}
cmd := TagsCmd{Config: cfg}
err := cmd.Do()
if err == nil {
t.Fatal("Error did not occur")
}
if !strings.Contains(err.Error(), "Cannot read home") {
t.Fatal("Unexpected error:", err)
}
}
func TestTagsInvalidCategory(t *testing.T) {
cwd, err := os.Getwd()
panicIfErr(err)
cfg := &Config{
HomePath: filepath.Join(cwd, "testdata", "list", "normal"),
}
cmd := TagsCmd{
Category: "unknown",
Config: cfg,
}
err = cmd.Do()
if err == nil {
t.Fatal("Error did not occur")
}
if !strings.Contains(err.Error(), "Category 'unknown' does not exist") {
t.Fatal("Unexpected error:", err)
}
}
func TestTagsLoadNoteError(t *testing.T) {
cwd, err := os.Getwd()
panicIfErr(err)
cfg := &Config{
HomePath: filepath.Join(cwd, "testdata", "list", "fail"),
}
cmd := TagsCmd{Config: cfg}
err = cmd.Do()
if err == nil {
t.Fatal("Error did not occur")
}
if !strings.Contains(err.Error(), "Cannot parse created date time as RFC3339") {
t.Fatal("Unexpected error:", err)
}
}