-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser_test.go
241 lines (193 loc) · 5.5 KB
/
parser_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
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
package pdf_parser
import (
logger "github.com/sirupsen/logrus"
//"fmt"
"os"
"path/filepath"
//"path/filepath"
"regexp"
"testing"
)
func TestParseBagElementsSingle(t *testing.T) {
reg := regexp.MustCompile(`(?m)\<dc:publisher>((.|\n)*)\<rdf:Bag>((.|\n)*)\<\/rdf:Bag\>((.|\n)*)\<\/dc:publisher>`)
data := []byte("<dc:publisher>\n <rdf:Bag>\n <rdf:li>No Starch Press</rdf:li>\n </rdf:Bag>\n </dc:publisher>")
resp := parseBagElements(reg, &data)
if len(resp) != 1 {
t.Error("Expected to be string array ", resp)
}
}
func TestParseBagElements(t *testing.T) {
reg := regexp.MustCompile(`(?m)\<dc:publisher>((.|\n)*)\<rdf:Bag>((.|\n)*)\<\/rdf:Bag\>((.|\n)*)\<\/dc:publisher>`)
data := []byte("<dc:publisher>\n <rdf:Bag>\n <rdf:li>No Starch Press</rdf:li>\n " +
"<rdf:li>No Starch Press</rdf:li>\n " +
" </rdf:Bag>\n </dc:publisher>")
resp := parseBagElements(reg, &data)
if len(resp) != 2 {
t.Error("Expected to be string array with size 2", resp)
}
}
func TestParseBagElementsZero(t *testing.T) {
reg := regexp.MustCompile(`(?m)\<dc:publisher>((.|\n)*)\<rdf:Bag>((.|\n)*)\<\/rdf:Bag\>((.|\n)*)\<\/dc:publisher>`)
data := []byte("<dc:publisher>\n </dc:publisher>")
resp := parseBagElements(reg, &data)
if len(resp) != 0 {
t.Error("Expected to be empty array", resp)
}
}
func TestParsePdf(t *testing.T) {
path, err := filepath.Abs("./resources/test.pdf")
if err != nil {
t.Error(err)
}
file, err := ParsePdf(path)
if err != nil {
t.Error(err)
}
if file == nil {
t.Error("general fail send regards")
}
}
func TestPdfPageCount(t *testing.T) {
file, err := os.Open("./resources/sample.pdf")
if err != nil {
t.Error(err)
}
if count := countPages(file) != 2; count {
t.Error("Wrong page count, should be 2 ")
}
}
func TestParseTestPdfInfo(t *testing.T) {
file, err := filepath.Abs("./resources/test.pdf")
if err != nil {
t.Error(err)
}
pdfInf, err := ParsePdf(file)
if err != nil {
t.Error(err)
}
if pdfInf.PdfVersion != "2.0" {
t.Error("Invalid pdf version parsing")
}
if pdfInf.PagesCount != 1 {
t.Error("Invalid pages count parsing")
}
testName := "Soda PDF Online"
if pdfInf.Info.Author != testName || pdfInf.Info.Creator != testName ||
pdfInf.Info.Producer != testName {
t.Error("Invalid info block parsing")
}
if pdfInf.OriginalXrefOffset != 16062 {
t.Error("Invalid original offset value parse")
}
if pdfInf.OriginalTrailerSection.Info.ObjectNumber != 11 ||
pdfInf.OriginalTrailerSection.Root.ObjectNumber != 1 ||
pdfInf.OriginalTrailerSection.Prev != 0 ||
pdfInf.OriginalTrailerSection.IdRaw != "[<DB0B127ADB050BC7FA6212CE9DDBFC70><246EA46DE075715E82A47F70DB5527D6>]" {
t.Error("Invalid original section parsing")
}
if len(pdfInf.XrefTable) != 1 {
t.Error("Invalid Xreftable parse")
}
if pdfInf.XrefTable[0].SectionStart != 16062 ||
len(pdfInf.XrefTable[0].ObjectSubsections) != 2 ||
pdfInf.XrefTable[0].ObjectSubsections[0].ObjectsCount != 6 ||
pdfInf.XrefTable[0].ObjectSubsections[11].ObjectsCount != 7 ||
pdfInf.XrefTable[0].ObjectSubsections[11].Id != 11 ||
pdfInf.XrefTable[0].ObjectSubsections[11].LastSubsectionObjectId != 17 {
t.Error("Invalid xref object parsing")
}
if pdfInf.Root.Type != "/Catalog" ||
pdfInf.Root.PageLabels != nil ||
pdfInf.Root.Lang != "" {
t.Error("Invalid root section parse")
}
if pdfInf.Root.Metadata.ObjectNumber != 12 {
t.Error("Invalid root section metadata link parse")
}
if pdfInf.Root.Pages.ObjectNumber != 2 {
t.Error("Invalid root section pages link parse")
}
if pdfInf.Metadata.Subtype != "XML" ||
pdfInf.Metadata.Length != 3175 ||
pdfInf.Metadata.Type != "Metadata" ||
pdfInf.Metadata.RdfMeta.Creator != testName {
t.Error("Invalid metadata section parse")
}
}
func TestGetTitle(t *testing.T) {
file, _ := filepath.Abs("./resources/test.pdf")
pdf, err := ParsePdf(file)
if err != nil {
t.Error(err)
}
if pdf.GetTitle() != pdf.Info.Title ||
pdf.GetTitle() != pdf.Metadata.RdfMeta.Title {
t.Error("Invalid GetTitle behaviour")
}
}
func TestGetAuthor(t *testing.T) {
file, _ := filepath.Abs("./resources/test.pdf")
pdf, err := ParsePdf(file)
if err != nil {
t.Error(err)
}
if pdf.GetAuthor() != pdf.Info.Author ||
pdf.GetAuthor() != pdf.Metadata.RdfMeta.Creator {
t.Error("Invalid getAuthor behaviour")
}
}
func TestSetLogger(t *testing.T) {
lg := logger.New()
lg.SetOutput(os.Stdout)
lg.SetReportCaller(true)
lg.SetFormatter(&logger.JSONFormatter{})
SetLogger(lg)
file, _ := filepath.Abs("./resources/test.pdf")
pdf, err := ParsePdf(file)
if err != nil {
t.Error(err)
}
if pdf.GetPagesCount() != 1 {
t.Error("Invalid amount of pages")
}
}
func TestNonExistedFileWithLof(t *testing.T) {
file, _ := filepath.Abs("./resources/test_non_existed.pdf")
_, err := ParsePdf(file)
if err == nil {
t.Error(err)
}
}
func TestWriteLogToFile(t *testing.T) {
f, err := os.OpenFile("test.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
t.Error(err)
}
defer f.Close()
lg := logger.New()
lg.SetOutput(f)
lg.SetFormatter(&logger.JSONFormatter{})
SetLogger(lg)
file, _ := filepath.Abs("./resources/test.pdf")
pdf, err := ParsePdf(file)
if err != nil {
t.Error(err)
}
if pdf.GetPagesCount() != 1 {
t.Error("Invalid amount of pages")
}
logFile, errF := os.Stat("test.log")
if errF != nil {
t.Error(errF)
}
if logFile.Size() == 0 {
t.Error("File should not be empty")
}
if os.IsNotExist(errF) {
t.Error(errF)
}
e := os.Remove("test.log")
if e != nil {
t.Error(err)
}
}