-
Notifications
You must be signed in to change notification settings - Fork 136
/
docx_test.go
202 lines (163 loc) · 4.68 KB
/
docx_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
package docx
import (
"bytes"
"embed"
"fmt"
"io/ioutil"
"strings"
"testing"
)
const testFile = "./TestDocument.docx"
const testFileResult = "./TestDocumentResult.docx"
func loadFile(file string) *Docx {
r, err := ReadDocxFile(file)
if err != nil {
panic(err)
}
return r.Editable()
}
func loadFromMemory(file string) *Docx {
data, _ := ioutil.ReadFile(file)
r, err := ReadDocxFromMemory(bytes.NewReader(data), int64(len(data)))
if err != nil {
panic(err)
}
return r.Editable()
}
//go:embed TestDocument.docx
var testFS embed.FS
func loadFromFs(file string) *Docx {
r, err := ReadDocxFromFS(strings.TrimPrefix(file, "./"), testFS)
if err != nil {
panic(err)
}
return r.Editable()
}
//Tests that we are able to load a file from a filesystem and do a quick replacement test
func TestReadDocxFromFS(t *testing.T) {
d := loadFromFs(testFile)
simpleReplacementTest(d, t)
}
//Tests that we are able to load a file from a memory array of bytes
func TestReadDocxFromMemory(t *testing.T) {
d := loadFromMemory(testFile)
simpleReplacementTest(d, t)
}
func simpleReplacementTest(d *Docx, t *testing.T) {
if d == nil {
t.Error("Doc should not be nil', got ", d)
}
d.Replace("document.", "line1\r\nline2", 1)
d.WriteToFile(testFileResult)
d = loadFile(testFileResult)
if strings.Contains(d.content, "This is a word document") {
t.Error("Missing 'This is a word document.', got ", d.content)
}
}
func TestReplace(t *testing.T) {
tests := []struct {
name string
replaceWith string
expect string
}{
{"Windows line breaks", "line1\r\nline2", "line1<w:br/>line2"},
{"Mac line breaks", "line1\rline2", "line1<w:br/>line2"},
{"Linux line breaks", "line1\nline2", "line1<w:br/>line2"},
{"Tabs", "line1\tline2", "line1</w:t><w:tab/><w:t>line2"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := loadFile(testFile)
previous := d.content
d.Replace("document.", tt.replaceWith, 1)
d.WriteToFile(testFileResult)
d = loadFile(testFileResult)
if strings.Contains(d.content, "This is a word document") {
t.Error("Should not contain 'This is a word document' after replacement, got ", d.content)
}
// These are arbitrary start and end points that should include the part that's being replaced, plus a bit extra
start := 1200
end := 1450
if !strings.Contains(d.content, tt.expect) {
t.Errorf("Expected '%s'\n previous: \n%s\n got: \n%s", tt.expect, extractMiddle(start, end, previous), extractMiddle(start, end, d.content))
}
})
}
}
// To make test failure messages easier to read, trim off the beginning and end of the document to focus on the part expected
// to have changed
func extractMiddle(start, end int, content string) string {
return fmt.Sprint(content[start:end])
}
func TestReplaceLink(t *testing.T) {
d := loadFile(testFile)
d.ReplaceLink("http://example.com/", "https://github.com/nguyenthenguyen/docx", -1)
d.WriteToFile(testFileResult)
d = loadFile(testFileResult)
if strings.Contains(d.links, "http://example.com") {
t.Error("Missing 'http://example.com', got ", d.links)
}
if !strings.Contains(d.links, "https://github.com/nguyenthenguyen/docx") {
t.Error("Expected 'word', got ", d.links)
}
}
func TestReplaceHeader(t *testing.T) {
d := loadFile(testFile)
d.ReplaceHeader("This is a header.", "newHeader")
d.WriteToFile(testFileResult)
d = loadFile(testFileResult)
headers := d.headers
found := false
for _, v := range headers {
if strings.Contains(v, "This is a header.") {
t.Error("Missing 'This is a header.', got ", d.content)
}
if strings.Contains(v, "newHeader") {
found = true
}
}
if !found {
t.Error("Expected 'newHeader', got ", d.headers)
}
}
func TestReplaceFooter(t *testing.T) {
d := loadFile(testFile)
d.ReplaceFooter("This is a footer.", "newFooter")
d.WriteToFile(testFileResult)
d = loadFile(testFileResult)
footers := d.footers
found := false
for _, v := range footers {
if strings.Contains(v, "This is a footer.") {
t.Error("Missing 'This is a footer.', got ", d.content)
}
if strings.Contains(v, "newFooter") {
found = true
}
}
if !found {
t.Error("Expected 'newFooter', got ", d.headers)
}
}
func TestReplaceImage(t *testing.T) {
d := loadFile(testFile)
d.ReplaceImage("word/media/image1.png", "./new.png")
d.WriteToFile(testFileResult)
d = loadFile(testFileResult)
images := d.images
found := false
for k := range images {
if k == "word/media/image1.png" {
found = true
}
}
if !found {
t.Error("Expected an 'word/media/image1.png', got something else")
}
}
func TestDocx_ImagesLen(t *testing.T) {
d := loadFile(testFile)
if d.ImagesLen() != 1 {
t.Error("Expected the sum of images is 1, got something else")
}
}