-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdf_test.go
216 lines (201 loc) · 5.82 KB
/
pdf_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
package lazypress
import (
"os"
"strings"
"testing"
)
type mockWriter struct {
written []byte
}
func (m *mockWriter) Write(p []byte) (n int, err error) {
m.written = p
return len(p), nil
}
type mockCloser struct {
count uint8
}
func (m *mockCloser) Close() error {
m.count++
return nil
}
func TestShouldErrorWhenExporterIsNotDefined(t *testing.T) {
var p PDF
err := p.Export()
if err == nil {
t.Error("Expected error when exporter is not defined")
}
}
func TestShouldWriteWithExporter(t *testing.T) {
var p PDF
m := &mockWriter{}
p.Exporter = m
content := []byte("<html><body>Hello World</body></html>")
p.Content = content
if err := p.Export(); err != nil {
t.Error("Expected no error when exporting")
}
if string(m.written) != string(content) {
t.Error("Expected written content to be the same as the HTML content")
}
}
func TestShouldCreatePDFWithLazypressInFilename(t *testing.T) {
var p PDF
f, err := p.createFile("")
if err != nil {
t.Error("Expected no error when creating file")
}
filePath := f.(*os.File).Name()
defer os.Remove(filePath)
if !strings.Contains(filePath, "lazypress") {
t.Error("Expected filePath to contain lazypress")
}
if !strings.HasSuffix(filePath, ".pdf") {
t.Error("Expected filePath to have .pdf suffix")
}
if p.filePath != filePath {
t.Error("Expected filePath to be the same as the created file")
}
}
func TestShouldCreatePDFNotWithLazypressInFilename(t *testing.T) {
var p PDF
fileName := "test"
f, err := p.createFile(fileName)
if err != nil {
t.Error("Expected no error when creating file")
}
filePath := f.(*os.File).Name()
defer os.Remove(filePath)
if !strings.Contains(filePath, "test") {
t.Error("Expected filePath to contain test")
}
if !strings.HasSuffix(filePath, ".pdf") {
t.Error("Expected filePath to have .pdf suffix")
}
if p.filePath != filePath {
t.Error("Expected filePath to be the same as the created file")
}
}
func TestShouldLoadOutputToFileSetting(t *testing.T) {
var p PDF
params := map[string]string{
"output": "file",
}
p.LoadSettings(params, nil, nil)
if p.Exporter.(*os.File) == nil {
t.Error("Expected Exporter to be a file")
}
if p.Closer.(*os.File) == nil {
t.Error("Expected Closer to be a file")
}
if p.filePath == "" {
t.Error("Expected filePath to be set")
}
defer os.Remove(p.filePath)
}
func TestShouldLoadOutputToDownloadSetting(t *testing.T) {
var p PDF
w := &mockWriter{}
params := map[string]string{
"output": "download",
}
p.LoadSettings(params, w, nil)
if p.Exporter.(*mockWriter) == nil {
t.Error("Expected Exporter to be a mockWriter")
}
if p.Exporter != w {
t.Error("Expected Exporter to be the same as the mockWriter")
}
if p.filePath != "" {
t.Error("Expected filePath to be empty")
}
}
func TestShouldLoadPassedWriterAsDefaultExporter(t *testing.T) {
var p PDF
w := &mockWriter{}
c := &mockCloser{}
p.LoadSettings(map[string]string{}, w, c)
if p.Exporter.(*mockWriter) != w {
t.Error("Expected Exporter to be the same as the mockWriter")
}
if p.Closer.(*mockCloser) != c {
t.Error("Expected Closer to be the same as the mockCloser")
}
if p.filePath != "" {
t.Error("Expected filePath to be empty")
}
}
func TestShouldSetStdoutAsWriterWhenNoWriterPassedAndNoOutputParamPassed(t *testing.T) {
var p PDF
p.LoadSettings(map[string]string{}, nil, nil)
if p.Exporter.(*os.File) == nil {
t.Error("Expected Exporter to be a file")
}
if p.Closer.(*os.File) == nil {
t.Error("Expected Closer to be a file")
}
if p.filePath != "" {
t.Error("Expected filePath to be empty")
}
}
func TestShouldLoadPassedSettings(t *testing.T) {
var p PDF
params := map[string]string{
"landscape": "true",
"displayHeaderFooter": "true",
"printBackground": "true",
"scale": "2",
"paperWidth": "8.5",
"paperHeight": "11",
"marginTop": "1",
"marginBottom": "1",
"marginLeft": "1",
"marginRight": "1",
"pageRanges": "1-5, 8, 11-13",
"headerTemplate": "<span class=title></span>",
"footerTemplate": "<span class=date></span>",
"preferCSSPageSize": "true",
}
p.LoadSettings(params, nil, nil)
if p.Settings.Landscape != true {
t.Error("Expected landscape to be true. Got: ", p.Settings.Landscape)
}
if p.Settings.DisplayHeaderFooter != true {
t.Error("Expected displayHeaderFooter to be true. Got: ", p.Settings.DisplayHeaderFooter)
}
if p.Settings.PrintBackground != true {
t.Error("Expected printBackground to be true. Got: ", p.Settings.PrintBackground)
}
if p.Settings.Scale != 2 {
t.Error("Expected scale to be 2. Got: ", p.Settings.Scale)
}
if p.Settings.PaperWidth != 8.5 {
t.Error("Expected paperWidth to be 8.5. Got: ", p.Settings.PaperWidth)
}
if p.Settings.PaperHeight != 11 {
t.Error("Expected paperHeight to be 11. Got: ", p.Settings.PaperHeight)
}
if p.Settings.MarginTop != 1 {
t.Error("Expected marginTop to be 1. Got: ", p.Settings.MarginTop)
}
if p.Settings.MarginBottom != 1 {
t.Error("Expected marginBottom to be 1. Got: ", p.Settings.MarginBottom)
}
if p.Settings.MarginLeft != 1 {
t.Error("Expected marginLeft to be 1. Got: ", p.Settings.MarginLeft)
}
if p.Settings.MarginRight != 1 {
t.Error("Expected marginRight to be 1. Got: ", p.Settings.MarginRight)
}
if p.Settings.PageRanges != "1-5, 8, 11-13" {
t.Error("Expected pageRanges to be 1-5, 8, 11-13. Got: ", p.Settings.PageRanges)
}
if p.Settings.HeaderTemplate != "<span class=title></span>" {
t.Error("Expected headerTemplate to be <span class=title></span>. Got: ", p.Settings.HeaderTemplate)
}
if p.Settings.FooterTemplate != "<span class=date></span>" {
t.Error("Expected footerTemplate to be <span class=date></span>. Got: ", p.Settings.FooterTemplate)
}
if p.Settings.PreferCSSPageSize != true {
t.Error("Expected preferCSSPageSize to be true. Got: ", p.Settings.PreferCSSPageSize)
}
}