-
Notifications
You must be signed in to change notification settings - Fork 13
/
epub_test.go
203 lines (177 loc) · 5.17 KB
/
epub_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2012 Ruben Pollan <[email protected]>
// Use of this source code is governed by a LGPL licence
// version 3 or later that can be found in the LICENSE file.
package epubgo
import "testing"
import (
"archive/zip"
"bytes"
"io/ioutil"
"os"
)
const (
bookPath = "testdata/a_dogs_tale.epub"
bookTitle = "A Dog's Tale"
bookLang = "en"
bookIdentifier = "http://www.gutenberg.org/ebooks/3174"
bookCreator = "Mark Twain"
bookSubject = "Dogs -- Fiction"
bookRights = "Public domain in the USA."
lenMetadatafields = 9
identifierScheme = "URI"
creatorFileAs = "Twain, Mark"
metaName = "cover"
htmlFile = "@public@vhost@g@gutenberg@html@files@3174@[email protected]"
fileId = "item8"
htmlPath = "3174/" + htmlFile
noNCXPath = "testdata/noncx.epub"
invalidNCXPath = "testdata/invalidncx.epub"
fileCapsPath = "testdata/fileCaps.epub"
)
func TestOpenClose(t *testing.T) {
f, err := Open(bookPath)
if err != nil {
t.Errorf("Open(%v) return an error: %v", bookPath, err)
}
f.Close()
}
func TestLoad(t *testing.T) {
file, _ := os.Open(bookPath)
fileInfo, _ := file.Stat()
f, err := Load(file, fileInfo.Size())
if err != nil {
t.Errorf("Open(%v) return an error: %v", bookPath, err)
}
f.Close()
}
func TestOpenFile(t *testing.T) {
f, _ := Open(bookPath)
defer f.Close()
html, err := f.OpenFile(htmlFile)
if err != nil {
t.Errorf("OpenFile(%v) return an error: %v", htmlFile, err)
return
}
defer html.Close()
zipFile, _ := zip.OpenReader(bookPath)
defer zipFile.Close()
var file *zip.File
for _, file = range zipFile.Reader.File {
if file.Name == htmlPath {
break
}
}
zipHTML, _ := file.Open()
buff1, err := ioutil.ReadAll(html)
if err != nil {
t.Errorf("Error reading the opened file: %v", err)
return
}
buff2, _ := ioutil.ReadAll(zipHTML)
if !bytes.Equal(buff1, buff2) {
t.Errorf("The files on zip and OpenFile are not equal")
return
}
}
func TestOpenFileId(t *testing.T) {
f, _ := Open(bookPath)
defer f.Close()
html, err := f.OpenFileId(fileId)
if err != nil {
t.Errorf("OpenFileId(%v) return an error: %v", fileId, err)
return
}
defer html.Close()
zipFile, _ := zip.OpenReader(bookPath)
defer zipFile.Close()
var file *zip.File
for _, file = range zipFile.Reader.File {
if file.Name == htmlPath {
break
}
}
zipHTML, _ := file.Open()
buff1, err := ioutil.ReadAll(html)
if err != nil {
t.Errorf("Error reading the opened file: %v", err)
return
}
buff2, _ := ioutil.ReadAll(zipHTML)
if !bytes.Equal(buff1, buff2) {
t.Errorf("The files on zip and OpenFile are not equal")
return
}
}
func TestNoNCX(t *testing.T) {
f, err := Open(noNCXPath)
if err != nil {
t.Errorf("Open(%v) return an error: %v", noNCXPath, err)
}
f.Close()
}
func TestInvalidNCX(t *testing.T) {
f, err := Open(invalidNCXPath)
if err != nil {
t.Errorf("Open(%v) return an error: %v", invalidNCXPath, err)
}
f.Close()
}
func TestFileCaps(t *testing.T) {
f, err := Open(fileCapsPath)
if err != nil {
t.Errorf("Open(%v) return an error: %v", fileCapsPath, err)
}
f.Close()
}
func TestMetadata(t *testing.T) {
f, _ := Open(bookPath)
defer f.Close()
if title, _ := f.Metadata("title"); title[0] != bookTitle {
t.Errorf("Metadata title '%v', the expected was '%v'", title[0], bookTitle)
}
if language, _ := f.Metadata("language"); language[0] != bookLang {
t.Errorf("Metadata language '%v', the expected was '%v'", language[0], bookLang)
}
if identifier, _ := f.Metadata("identifier"); identifier[0] != bookIdentifier {
t.Errorf("Metadata identifier '%v', the expected was '%v'", identifier[0], bookIdentifier)
}
if creator, _ := f.Metadata("creator"); creator[0] != bookCreator {
t.Errorf("Metadata creator '%v', the expected was '%v'", creator[0], bookCreator)
}
if subject, _ := f.Metadata("subject"); subject[0] != bookSubject {
t.Errorf("Metadata subject '%v', the expected was '%v'", subject[0], bookSubject)
}
if rights, _ := f.Metadata("rights"); rights[0] != bookRights {
t.Errorf("Metadata rights '%v', the expected was '%v'", rights[0], bookRights)
}
}
func TestMetadataFields(t *testing.T) {
f, _ := Open(bookPath)
defer f.Close()
fields := f.MetadataFields()
if len(fields) != lenMetadatafields {
t.Errorf("len(MetadataFields()) should be %v, but was %v", lenMetadatafields, len(fields))
}
isTitle := false
for _, field := range fields {
if field == "title" {
isTitle = true
}
}
if !isTitle {
t.Errorf("title is not in the metadata fields")
}
}
func TestMetadataAttr(t *testing.T) {
f, _ := Open(bookPath)
defer f.Close()
if identifier, _ := f.MetadataAttr("identifier"); identifier[0]["scheme"] != identifierScheme {
t.Errorf("Metadata identifier attr scheme '%v', the expected was '%v'", identifier[0]["scheme"], identifierScheme)
}
if creator, _ := f.MetadataAttr("creator"); creator[0]["file-as"] != creatorFileAs {
t.Errorf("Metadata creator attr file-as '%v', the expected was '%v'", creator[0]["file-as"], creatorFileAs)
}
if meta, _ := f.MetadataAttr("meta"); meta[0]["name"] != metaName {
t.Errorf("Metadata meta attr name '%v', the expected was '%v'", meta[0]["name"], metaName)
}
}