-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers_test.go
188 lines (153 loc) · 4.16 KB
/
helpers_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
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
package main
import (
"fmt"
"os"
"reflect"
"testing"
"github.com/jroimartin/gocui"
nato "github.com/kaikaew13/manganato-api"
"github.com/kaikaew13/manganato-cli/views"
)
func TestValidateCommand(t *testing.T) {
type test struct {
description string
command string
valid bool
cmd string
args string
}
tests := []test{
{
description: "command for searching for mangas by name",
command: "search chainsaw man",
valid: true,
cmd: searchCommand,
args: "chainsaw man",
},
{
description: "command for searching for mangas by author name",
command: "search-author tatsuki fujimoto",
valid: true,
cmd: searchByAuthorCommand,
args: "tatsuki fujimoto",
},
{
description: "command for searching for mangas by genre",
command: "search-genre action",
valid: true,
cmd: searchByGenreCommand,
args: "action",
},
{
description: "invalid command",
command: "seach chainsaw man",
valid: false,
cmd: "",
args: "",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
valid, cmd, args := validateCommand(test.command)
if valid != test.valid {
t.Errorf("wanted valid to be %t, got %t", test.valid, valid)
}
if cmd != test.cmd {
t.Errorf("wanted cmd to be %s, got %s", test.cmd, cmd)
}
if args != test.args {
t.Errorf("wanted args to be %s, got %s", test.args, args)
}
})
}
}
func TestGetInitialScreen(t *testing.T) {
g, maxX, maxY := initHelper(t)
getSearchListHelper(t, maxX, maxY, g)
err := getInitialScreen()
if err != nil {
t.Errorf("not expected to have error: %s", err.Error())
}
mgs, err := screen.searcher.SearchLatestUpdatedManga()
if err != nil {
t.Errorf("not expected to have error: %s", err.Error())
}
if len(*mgs) != len(screen.sl.Mangas) {
t.Errorf("wanted mangas with length %d, got %d", len(*mgs), len(screen.sl.Mangas))
}
g = nil
}
func TestGetMangaScreen(t *testing.T) {
mgName, mgId := "Chainsaw Man", "dn980422"
g, maxX, maxY := initHelper(t)
getSearchListHelper(t, maxX, maxY, g)
getMangaDetailsHelper(t, maxX, maxY, g)
getChapterListHelper(t, maxX, maxY, g)
screen.sl.NameToIDMap[mgName] = mgId
err := getMangaScreen(fmt.Sprintf("%s %s", views.Selector, mgName))
if err != nil {
t.Errorf("not expected to have error: %s", err.Error())
}
mg, err := screen.searcher.PickManga(mgId)
if err != nil {
t.Errorf("not expected to have error: %s", err.Error())
}
if !reflect.DeepEqual(screen.md.Manga, *mg) {
t.Errorf("manga fields are not equal:\n wanted %v, got %v", *mg, screen.md.Manga)
}
g = nil
}
func TestGetDirPath(t *testing.T) {
dirpath, err := getDirPath("test", "1")
if err != nil {
t.Errorf("not expected to have error: %s", err.Error())
}
_, err = os.Stat(dirpath)
if err != nil && os.IsNotExist(err) {
t.Error("directory does not exist")
}
os.RemoveAll("test")
}
func initHelper(t testing.TB) (g *gocui.Gui, maxX, maxY int) {
t.Helper()
screen.searcher = nato.NewSearcher()
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
t.Errorf("not expected to have error: %s", err.Error())
}
maxX, maxY = g.Size()
return
}
func getSearchListHelper(t testing.TB, maxX, maxY int, g *gocui.Gui) {
t.Helper()
sl, err := views.GetSearchList(maxX, maxY, g)
if err != nil && err != gocui.ErrUnknownView {
t.Errorf("not expected to have error: %s", err.Error())
}
if sl == nil {
t.Error("not expected sl to be nil")
}
screen.sl = sl
}
func getMangaDetailsHelper(t testing.TB, maxX, maxY int, g *gocui.Gui) {
t.Helper()
md, err := views.GetMangaDetails(maxX, maxY, g)
if err != nil && err != gocui.ErrUnknownView {
t.Errorf("not expected to have error: %s", err.Error())
}
if md == nil {
t.Error("not expected md to be nil")
}
screen.md = md
}
func getChapterListHelper(t testing.TB, maxX, maxY int, g *gocui.Gui) {
t.Helper()
cl, err := views.GetChapterList(maxX, maxY, g)
if err != nil && err != gocui.ErrUnknownView {
t.Errorf("not expected to have error: %s", err.Error())
}
if cl == nil {
t.Error("not expected cl to be nil")
}
screen.cl = cl
}