This repository was archived by the owner on Apr 6, 2026. It is now read-only.
forked from MhmoudGit/html2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2pdf_test.go
More file actions
137 lines (119 loc) · 2.36 KB
/
html2pdf_test.go
File metadata and controls
137 lines (119 loc) · 2.36 KB
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
package html2pdf
import (
"html/template"
"os"
"testing"
"time"
)
type Todo struct {
ID int
Item string
}
var (
tmpl = `
<!DOCTYPE html>
<html>
<head>
<title>Todo Information</title>
</head>
<body>
<h1>{{.ID}}</h1>
<p>{{.Item}}.</p>
</body>
</html>
`
// for single file test
singleTmpl = `
<!DOCTYPE html>
<html>
<head>
<title>Todo Information</title>
</head>
<body>
{{range .}}
<h1>{{.ID}}</h1>
<p>{{.Item}}.</p>
{{end}}
</body>
</html>
`
// Create a temporary directory for testing
tempDir = "files"
mergedPdf = "final.pdf"
g = Generator[Todo]{
OutputPath: tempDir,
FinalPdf: mergedPdf,
SingleHtmlFile: true, // test for single html file only
}
)
func TestGenerator(t *testing.T) {
expectedTodos := []Todo{
{ID: 1, Item: "First item"},
{ID: 2, Item: "Second item"},
{ID: 3, Item: "Third item"},
{ID: 4, Item: "Fourth item"},
}
if g.SingleHtmlFile {
template, err := template.New("todo").Parse(singleTmpl)
if err != nil {
panic(err)
}
g.Template = template
} else {
template, err := template.New("todo").Parse(tmpl)
if err != nil {
panic(err)
}
g.Template = template
}
g.Data = expectedTodos
result := g
// Test if the Data field matches the expectedTodos
if len(result.Data) != len(expectedTodos) {
t.Errorf("Expected Data length %d, got %d", len(expectedTodos), len(result.Data))
return
}
for i, expected := range expectedTodos {
if result.Data[i] != expected {
t.Errorf("Expected todo at index %d to be %v, got %v", i, expected, result.Data[i])
}
}
}
func TestCreatePdf(t *testing.T) {
todos := []Todo{
{ID: 1, Item: "First item"},
{ID: 2, Item: "Second item"},
{ID: 3, Item: "Third item"},
{ID: 4, Item: "Fourth item"},
}
if g.SingleHtmlFile {
template, err := template.New("todo").Parse(singleTmpl)
if err != nil {
panic(err)
}
g.Template = template
} else {
template, err := template.New("todo").Parse(tmpl)
if err != nil {
panic(err)
}
g.Template = template
}
g.Data = todos
start := time.Now()
// test 1
err := g.CreatePdf()
if err != nil {
t.Errorf("Error creating PDF: %v", err)
}
t.Log(time.Since(start))
}
func TestDeletingDataFolder(t *testing.T) {
err := g.DeleteFiles()
if err != nil {
t.Errorf("Error deleting data folder: %v", err)
}
if _, err := os.Stat(tempDir); !os.IsNotExist(err) {
t.Errorf("Error folder is not deleted")
}
}