-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbookmark_methods_test.go
More file actions
300 lines (264 loc) · 8.06 KB
/
bookmark_methods_test.go
File metadata and controls
300 lines (264 loc) · 8.06 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package gobookmarks
import (
_ "embed"
"testing"
)
var (
//go:embed testdata/insert_category_input.txt
insertCategoryInput string
//go:embed testdata/insert_category_expected.txt
insertCategoryExpected string
//go:embed testdata/add_category_input.txt
addCategoryInput string
//go:embed testdata/add_category_expected.txt
addCategoryExpected string
//go:embed testdata/switch_category_input.txt
switchCategoryInput string
//go:embed testdata/switch_category_expected.txt
switchCategoryExpected string
//go:embed testdata/add_page_input.txt
addPageInput string
//go:embed testdata/add_page_expected.txt
addPageExpected string
//go:embed testdata/insert_page_input.txt
insertPageInput string
//go:embed testdata/insert_page_expected.txt
insertPageExpected string
//go:embed testdata/switch_page_input.txt
switchPageInput string
//go:embed testdata/switch_page_expected.txt
switchPageExpected string
//go:embed testdata/add_tab_input.txt
addTabInput string
//go:embed testdata/add_tab_expected.txt
addTabExpected string
//go:embed testdata/insert_tab_input.txt
insertTabInput string
//go:embed testdata/insert_tab_expected.txt
insertTabExpected string
//go:embed testdata/switch_tab_input.txt
switchTabInput string
//go:embed testdata/switch_tab_expected.txt
switchTabExpected string
)
func TestInsertCategory(t *testing.T) {
tabs := ParseBookmarks(insertCategoryInput)
col := tabs[0].Pages[0].Blocks[0].Columns[0]
col.InsertCategory(1, &BookmarkCategory{Name: "C"})
got := tabs.String()
if got != insertCategoryExpected {
t.Fatalf("expected %q got %q", insertCategoryExpected, got)
}
}
func TestAddCategory(t *testing.T) {
tabs := ParseBookmarks(addCategoryInput)
col := tabs[0].Pages[0].Blocks[0].Columns[0]
col.AddCategory(&BookmarkCategory{Name: "B"})
got := tabs.String()
if got != addCategoryExpected {
t.Fatalf("expected %q got %q", addCategoryExpected, got)
}
}
func TestSwitchCategory(t *testing.T) {
tabs := ParseBookmarks(switchCategoryInput)
col := tabs[0].Pages[0].Blocks[0].Columns[0]
col.SwitchCategories(0, 1)
got := tabs.String()
if got != switchCategoryExpected {
t.Fatalf("expected %q got %q", switchCategoryExpected, got)
}
}
func TestAddPage(t *testing.T) {
tabs := ParseBookmarks(addPageInput)
p := &BookmarkPage{Blocks: []*BookmarkBlock{{Columns: []*BookmarkColumn{{}}}}}
p.Blocks[0].Columns[0].AddCategory(&BookmarkCategory{Name: "B"})
tabs[0].AddPage(p)
got := tabs.String()
if got != addPageExpected {
t.Fatalf("expected %q got %q", addPageExpected, got)
}
}
func TestInsertPage(t *testing.T) {
tabs := ParseBookmarks(insertPageInput)
p := &BookmarkPage{Blocks: []*BookmarkBlock{{Columns: []*BookmarkColumn{{}}}}}
p.Blocks[0].Columns[0].AddCategory(&BookmarkCategory{Name: "X"})
tabs[0].InsertPage(1, p)
got := tabs.String()
if got != insertPageExpected {
t.Fatalf("expected %q got %q", insertPageExpected, got)
}
}
func TestSwitchPage(t *testing.T) {
tabs := ParseBookmarks(switchPageInput)
tabs[0].SwitchPages(0, 1)
got := tabs.String()
if got != switchPageExpected {
t.Fatalf("expected %q got %q", switchPageExpected, got)
}
}
func TestAddTab(t *testing.T) {
tabs := ParseBookmarks(addTabInput)
nl := &BookmarkTab{Name: "Three"}
p := &BookmarkPage{Blocks: []*BookmarkBlock{{Columns: []*BookmarkColumn{{}}}}}
p.Blocks[0].Columns[0].AddCategory(&BookmarkCategory{Name: "C"})
nl.AddPage(p)
var list = tabs
list.AddTab(nl)
got := list.String()
if got != addTabExpected {
t.Fatalf("expected %q got %q", addTabExpected, got)
}
}
func TestInsertTab(t *testing.T) {
tabs := ParseBookmarks(insertTabInput)
nl := &BookmarkTab{Name: "Mid"}
p := &BookmarkPage{Blocks: []*BookmarkBlock{{Columns: []*BookmarkColumn{{}}}}}
p.Blocks[0].Columns[0].AddCategory(&BookmarkCategory{Name: "X"})
nl.AddPage(p)
var list = tabs
list.InsertTab(1, nl)
got := list.String()
if got != insertTabExpected {
t.Fatalf("expected %q got %q", insertTabExpected, got)
}
}
func TestSwitchTab(t *testing.T) {
tabs := ParseBookmarks(switchTabInput)
var list = tabs
list.SwitchTabs(0, 1)
got := list.String()
if got != switchTabExpected {
t.Fatalf("expected %q got %q", switchTabExpected, got)
}
}
func TestMoveTab(t *testing.T) {
tabs := ParseBookmarks(switchTabInput)
var list = tabs
list.MoveTab(0, 1)
got := list.String()
if got != switchTabExpected {
t.Fatalf("expected %q got %q", switchTabExpected, got)
}
}
func TestMovePage(t *testing.T) {
tabs := ParseBookmarks(switchPageInput)
tabs[0].MovePage(0, 1)
got := tabs.String()
if got != switchPageExpected {
t.Fatalf("expected %q got %q", switchPageExpected, got)
}
}
func TestMoveEntry(t *testing.T) {
cat := &BookmarkCategory{Name: "C", Entries: []*BookmarkEntry{{Url: "u1"}, {Url: "u2"}}}
cat.MoveEntry(0, 1)
expected := "Category: C\nu2\nu1\n"
if got := cat.String(); got != expected {
t.Fatalf("expected %q got %q", expected, got)
}
}
func TestInvalidOperations(t *testing.T) {
tabs := ParseBookmarks(insertCategoryInput)
col := tabs[0].Pages[0].Blocks[0].Columns[0]
orig := len(col.Categories)
col.InsertCategory(-1, &BookmarkCategory{Name: "X"})
if len(col.Categories) != orig {
t.Fatalf("invalid insert changed categories")
}
col.SwitchCategories(-1, 3)
if len(col.Categories) != orig {
t.Fatalf("invalid switch changed categories")
}
tabs = ParseBookmarks(insertPageInput)
pcount := len(tabs[0].Pages)
tabs[0].InsertPage(-1, &BookmarkPage{})
if len(tabs[0].Pages) != pcount {
t.Fatalf("invalid page insert changed pages")
}
tabs[0].SwitchPages(-1, 5)
if len(tabs[0].Pages) != pcount {
t.Fatalf("invalid page switch changed pages")
}
tabs = ParseBookmarks(insertTabInput)
l := BookmarkList(tabs)
tcount := len(l)
l.InsertTab(-1, &BookmarkTab{})
if len(l) != tcount {
t.Fatalf("invalid tab insert changed tabs")
}
l.SwitchTabs(-1, 9)
if len(l) != tcount {
t.Fatalf("invalid tab switch changed tabs")
}
}
func TestParseEmpty(t *testing.T) {
tabs := ParseBookmarks("")
if len(tabs) != 1 || len(tabs[0].Pages) != 1 {
t.Fatalf("expected single empty tab and page")
}
if got := tabs.String(); got != "" {
t.Fatalf("expected empty serialization got %q", got)
}
}
func TestStringers(t *testing.T) {
e := &BookmarkEntry{Url: "u", Name: "n"}
if e.String() != "u n\n" {
t.Fatalf("entry string")
}
e2 := &BookmarkEntry{Url: "u"}
if e2.String() != "u\n" {
t.Fatalf("entry fallback")
}
var nilEntry *BookmarkEntry
if nilEntry.String() != "" {
t.Fatalf("nil entry")
}
c := &BookmarkCategory{Name: "C", Entries: []*BookmarkEntry{e}}
if c.String() != "Category: C\nu n\n" {
t.Fatalf("category string")
}
col := &BookmarkColumn{Categories: []*BookmarkCategory{c}}
if col.String() != "Category: C\nu n\n" {
t.Fatalf("column string")
}
blk := &BookmarkBlock{Columns: []*BookmarkColumn{col, {}}}
blk.Columns[1].AddCategory(&BookmarkCategory{Name: "D"})
expectedBlk := "Category: C\nu n\nColumn\nCategory: D\n"
if blk.String() != expectedBlk {
t.Fatalf("block string")
}
page := &BookmarkPage{Name: "First", Blocks: []*BookmarkBlock{blk, {HR: true}}}
expectedPage := expectedBlk + "--\n"
if page.String() != expectedPage {
t.Fatalf("page string")
}
tab := &BookmarkTab{Name: "T", Pages: []*BookmarkPage{page}}
expectTab := "Tab: T\nPage: First\n" + expectedPage
if tab.String() != expectTab {
t.Fatalf("tab string")
}
anon := &BookmarkTab{Pages: []*BookmarkPage{page}}
if anon.String() != "Tab\nPage: First\n"+expectedPage {
t.Fatalf("anon tab string")
}
list := BookmarkList{tab}
if list.String() != expectTab {
t.Fatalf("list string")
}
// coverage of additional branches
page2 := &BookmarkPage{Name: "N2"}
tab2 := &BookmarkTab{Name: "X", Pages: []*BookmarkPage{page, page2}}
full := BookmarkList{tab2}
want := "Tab: X\nPage: First\n" + expectedPage + "Page: N2\n"
if full.String() != want {
t.Fatalf("full list string")
}
if tab.stringWithContext(true) != expectTab {
t.Fatalf("stringWithContext named")
}
if anon.stringWithContext(false) != "Tab\nPage: First\n"+expectedPage {
t.Fatalf("stringWithContext anon")
}
if anon.stringWithContext(true) != "Page: First\n"+expectedPage {
t.Fatalf("stringWithContext omit")
}
}