forked from lukasjarosch/go-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_test.go
57 lines (48 loc) · 1.24 KB
/
replace_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
package docx
import (
"encoding/xml"
"os"
"testing"
)
func TestReplacer_Replace(t *testing.T) {
replaceMap := PlaceholderMap{
"key": "key",
"key-with-dash": "key-with-dash",
"key-with-dashes": "key-with-dashes",
"key with space": "key with space",
"key_with_underscore": "key_with_underscore",
"multiline": "multiline",
"key.with.dots": "key.with.dots",
"mixed-key.separator_styles#": "mixed-key.separator_styles#",
"yet-another_placeholder": "yet-another_placeholder",
"foo": "foo",
}
doc, err := Open("./test/template.docx")
if err != nil {
t.Error(err)
return
}
err = doc.ReplaceAll(replaceMap)
if err != nil {
t.Error("replacing failed", err)
return
}
err = doc.WriteToFile("./test/out.docx")
if err != nil {
t.Error("unable to write", err)
return
}
document, err := Open("./test/out.docx")
if err != nil {
t.Error("failed to open docx")
return
}
documentXml := document.files[DocumentXml]
err = xml.Unmarshal(documentXml, new(interface{}))
if err != nil {
t.Error("failed to unmarshal xml, replacing failed")
return
}
// cleanup
_ = os.Remove("./test/out.docx")
}