forked from lukasjarosch/go-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
97 lines (83 loc) · 1.96 KB
/
parse_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
package docx
import (
"os"
"testing"
)
var (
testFile = "./test/test.xml"
totalRunCount = 7
emptyRunCount = 2
expectedTexts = []string{"TEXT0", "TEXT1", "TEXT2", "TEXT3", "TEXT4"}
)
func TestRunParser_FindRuns(t *testing.T) {
docBytes := readFile(t, testFile)
sut := NewRunParser(docBytes)
err := sut.findRuns()
if err != nil {
t.Errorf("parser.findRuns failed: %s", err)
}
is := len(sut.Runs())
if is != totalRunCount {
t.Errorf("parser returned %d runs, expected %d", is, totalRunCount)
}
t.Logf("parser returned %d runs, expected %d", is, totalRunCount)
}
func TestRunParser_FindTextRuns(t *testing.T) {
docBytes := readFile(t, testFile)
sut := NewRunParser(docBytes)
err := sut.findRuns()
if err != nil {
t.Errorf("parser.findRuns failed: %s", err)
}
err = sut.findTextRuns()
if err != nil {
t.Errorf("parser.findTextRuns failed: %s", err)
}
}
func TestRun_GetText(t *testing.T) {
docBytes := readFile(t, testFile)
sut := NewRunParser(docBytes)
err := sut.Execute()
if err != nil {
t.Errorf("parser.Execute failed: %s", err)
}
for _, expectedText := range expectedTexts {
found := false
for _, run := range sut.Runs().WithText() {
text := run.GetText(docBytes)
if text == expectedText {
found = true
t.Logf("found expected text %s", expectedText)
continue
}
}
if !found {
t.Errorf("did not find expected text %s", expectedText)
}
}
}
func TestRun_WithText(t *testing.T) {
docBytes := readFile(t, testFile)
sut := NewRunParser(docBytes)
err := sut.Execute()
if err != nil {
t.Errorf("parser.findRuns failed: %s", err)
}
is := len(sut.Runs().WithText())
exp := totalRunCount - emptyRunCount
if is != exp {
t.Errorf("parser returned %d runs with text, expected %d", is, exp)
}
}
func readFile(t testing.TB, path string) []byte {
f, err := os.Open(path)
if err != nil {
t.Error(err)
}
b := readBytes(f)
n := len(b)
if n == 0 {
t.Errorf("nothing was read from test file %s", path)
}
return b
}