/
bookmark_chrome_test.go
65 lines (54 loc) · 1.42 KB
/
bookmark_chrome_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
package papernet
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func loadTests(t *testing.T) map[string]struct {
file *os.File
papers []chromeBookmarkPaper
} {
dirname := path.Join("testfiles", "chrome")
files, err := ioutil.ReadDir(dirname)
require.NoError(t, err)
tts := make(map[string]struct {
file *os.File
papers []chromeBookmarkPaper
})
for _, f := range files {
ext := path.Ext(f.Name())
if ext != ".html" {
continue
}
base := strings.TrimSuffix(f.Name(), ext)
tt := tts[base] // Just to get a default struct to fill
// Set file
htmlFile := path.Join(dirname, f.Name())
tt.file, err = os.Open(htmlFile)
require.NoError(t, err, "could not open file %s", htmlFile)
// Load expected papers
jsonFile := path.Join(dirname, fmt.Sprintf("%s.json", base))
file, err := os.Open(jsonFile)
require.NoError(t, err, "could not open file %s", jsonFile)
var papers []chromeBookmarkPaper
err = json.NewDecoder(file).Decode(&papers)
require.NoError(t, err, "could not load expected papers for %s", base)
tt.papers = papers
tts[base] = tt
}
return tts
}
func TestImportChromeBookmarks(t *testing.T) {
tts := loadTests(t)
for name, tt := range tts {
papers, err := importChromeBookmarks(tt.file)
assert.NoError(t, err, name)
assert.Equal(t, tt.papers, papers, name)
}
}