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?`
func TestPlainStructure(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(StringToBytes(tc.content), &doc)
if err != nil {
t.Fatal(err)
}
if len(doc.Body.Paragraphs) != tc.numParagraphs {
t.Fatalf("We expected %d paragraphs, we got %d", tc.numParagraphs, len(doc.Body.Paragraphs))
}
for i, p := range doc.Body.Paragraphs {
if len(p.Children) == 0 {
t.Fatalf("We were not able to parse paragraph %d", i)
}
for _, child := range p.Children {
if child.Link == nil && child.Properties == nil && child.Run == nil {
t.Fatalf("There are Paragraph children with all fields nil")
}
if child.Run != nil && child.Run.Text == nil && child.Run.InstrText == "" {
t.Fatalf("We have a run with no text")
}
if child.Link != nil && child.Link.ID == "" {
t.Fatalf("We have a link without ID")
}
}
}
}
}
const drawing_doc = `
直接粘贴
inline
一行2个
inline
一行2个组合
inline
一个 浮于上方
右侧对齐
左
11.32cm
顶
23.73cm
2935605
97790
0
0
`
func TestDrawingStructure(t *testing.T) {
doc := Document{
XMLW: XMLNS_W,
XMLR: XMLNS_R,
XMLName: xml.Name{Space: XMLNS_W, Local: "document"}}
err := xml.Unmarshal(StringToBytes(drawing_doc), &doc)
if err != nil {
t.Fatal(err)
}
if len(doc.Body.Paragraphs) != 8 {
t.Fatalf("We expected %d paragraphs, we got %d", 8, len(doc.Body.Paragraphs))
}
for i, p := range doc.Body.Paragraphs {
if len(p.Children) == 0 {
t.Fatalf("We were not able to parse paragraph %d", i)
}
for j, child := range p.Children {
if child.Link == nil && child.Properties == nil && child.Run == nil {
t.Fatalf("There are Paragraph children with all fields nil")
}
if child.Run != nil && child.Run.Text == nil && child.Run.InstrText == "" && child.Run.Drawing == nil {
t.Fatalf("We have a run with no text and drawing")
}
if child.Link != nil && child.Link.ID == "" {
t.Fatalf("We have a link without ID")
}
if child.Run != nil && child.Run.Drawing != nil {
t.Log("fild drawing at aragraph", i, ", child", j)
if child.Run.Drawing.Inline != nil {
tail := "-mock-inline-p" + string(rune('0'+i)) + "-c" + string(rune('0'+j))
if "T"+tail != child.Run.Drawing.Inline.DistT {
t.Fatal("expect", "T"+tail, "but got", child.Run.Drawing.Inline.DistT)
}
if "B"+tail != child.Run.Drawing.Inline.DistB {
t.Fatal("expect", "B"+tail, "but got", child.Run.Drawing.Inline.DistB)
}
if "L"+tail != child.Run.Drawing.Inline.DistL {
t.Fatal("expect", "L"+tail, "but got", child.Run.Drawing.Inline.DistL)
}
if "R"+tail != child.Run.Drawing.Inline.DistR {
t.Fatal("expect", "R"+tail, "but got", child.Run.Drawing.Inline.DistR)
}
}
}
}
}
}