forked from nguyenthenguyen/docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocx_test.go
82 lines (64 loc) · 1.6 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
package docx
import (
"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 TestReplace(t *testing.T) {
d := loadFile(testFile)
d.Replace("This is a word document.", "word", 1)
d.WriteToFile(testFileResult)
d = loadFile(testFileResult)
if strings.Contains(d.content, "This is a word document") {
t.Error("Missing 'This is a word doucument.', got ", d.content)
}
if !strings.Contains(d.content, "word") {
t.Error("Expected 'word', got ", d.content)
}
}
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)
}
}