package docxlib
import (
"encoding/xml"
"testing"
)
const decoded_doc_1 = `testtest font sizetest colorNew style 1New style 2test font size and colorgoogle`
const decoded_doc_2 = `Table of Contents TOC \h \z \t "Heading 1,2,S6,1,S0,1,S1,1,S2,1,S3,1,S4,1,S5,1" Holy Grail [xref:bRJduW6hNR] PAGEREF _Toc420414504 \h 21.What is your name? [xref:TH7u7QDqhD] PAGEREF _Toc420414505 \h 22.What is your quest? [xref:bC62HkFATC] PAGEREF _Toc420414506 \h 23.What is your favourite colour? [xref:I3TphuHX6N] PAGEREF _Toc420414507 \h 2Holy Grail [ FORMTEXT xref:bRJduW6hNR]What is your name? [ FORMTEXT xref:TH7u7QDqhD]My name is Sir Launcelot of Camelot.What is your quest? [ FORMTEXT xref:bC62HkFATC]To seek the Holy Grail[or a grail shaped beacon]. What is your favourite colour? [ FORMTEXT xref:I3TphuHX6N]Blue.How many paragraphs here then?`
const NUM_PARAGRAPHS = 5
func TestStructure(t *testing.T) {
doc := Document{
XMLW: XMLNS_W,
XMLR: XMLNS_R,
XMLName: xml.Name{Space: XMLNS_W, Local: "document"}}
testCases := []struct {
content string
numParagraphs int
}{
{decoded_doc_1, 5},
{decoded_doc_2, 19},
}
for _, tc := range testCases {
err := xml.Unmarshal([]byte(tc.content), &doc)
if err != nil {
t.Errorf("We expected to be able to decode %s but we didn't",
tc.content)
}
if len(doc.Body.Paragraphs) != tc.numParagraphs {
t.Errorf("We expected %d paragraphs, we got %d",
NUM_PARAGRAPHS, len(doc.Body.Paragraphs))
}
for _, p := range doc.Body.Paragraphs {
if len(p.Children()) == 0 {
t.Errorf("We were not able to parse paragraph %v",
p)
}
for _, child := range p.Children() {
if child.Link == nil && child.Properties == nil && child.Run == nil {
t.Errorf("There are Paragraph children with all fields nil")
}
if child.Run != nil && child.Run.Text == nil && child.Run.InstrText == "" {
t.Errorf("We have a run with no text")
}
if child.Link != nil && child.Link.ID == "" {
t.Errorf("We have a link without ID")
}
}
}
}
}