-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
106 lines (98 loc) · 2.14 KB
/
reader_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
package mangaconv
import (
"context"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/sync/errgroup"
)
func mustReadImg(path string) image.Image {
f, err := os.Open(path)
if err != nil {
panic(fmt.Sprintf("cannot open %s: %s", path, err))
}
defer f.Close()
i, _, err := image.Decode(f)
if err != nil {
panic(fmt.Sprintf("cannot decode %s: %s", path, err))
}
return i
}
func readHelper(path string) ([]page, error) {
read, err := selectReader(path)
if err != nil {
return nil, err
}
errg, ctx := errgroup.WithContext(context.Background())
pages := make(chan page, 100)
errg.Go(func() error {
defer close(pages)
return read(ctx, pages, path)
})
// since pages channel is buffered, this will run as soon as the processing is done.
if err := errg.Wait(); err != nil {
return nil, err
}
// at this point pages channel is already closed and safe to operate on synchronously.
out := make([]page, len(pages))
for p := range pages {
out[p.Index] = p
}
return out, nil
}
func TestReader(t *testing.T) {
tests := []struct {
name string
path string
want []page
err error
}{
{
name: "directory reader",
path: "testdata/",
want: []page{
{mustReadImg("testdata/wikipe-tan-0.png"), 0},
{mustReadImg("testdata/wikipe-tan-1.png"), 1},
},
},
{
name: "zip reader",
path: "testdata/wikipe-tan.zip",
want: []page{
{mustReadImg("testdata/wikipe-tan-0.png"), 0},
{mustReadImg("testdata/wikipe-tan-1.png"), 1},
},
},
{
name: "file without extension",
path: "testdata/file",
err: ErrUnsupportedFormat,
},
{
name: "unsupported file format",
path: "testdata/file.unsupported",
err: ErrUnsupportedFormat,
},
{
name: "nonexistant file",
path: "testdata/nothinghere",
err: ErrCannotReadPath,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readHelper(tt.path)
if err != tt.err {
t.Errorf("reader error %v, want %v", got, tt.want)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("reader mismatch (-want +got):\n%s", diff)
}
})
}
}