forked from nguyenthenguyen/docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docx_test.go
126 lines (99 loc) · 2.75 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
package docx
import (
"bytes"
"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, err := ioutil.ReadFile(file)
r, err := ReadDocxFromMemory(bytes.NewReader(data), int64(len(data)))
if err != nil {
panic(err)
}
return r.Editable()
}
//Tests that we are able to load a file from a memory array of bytes and do a quick replacement test
func TestReadDocxFromMemory(t *testing.T) {
d := loadFromMemory(testFile)
if d == nil {
t.Error("Doc should not be nill', 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) {
d := loadFile(testFile)
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)
}
if !strings.Contains(d.content, "line1<w:br/>line2") {
t.Error("Expected 'line1<w:br/>line2', got ", d.content)
}
}
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)
}
}