package docxlib
import (
"encoding/xml"
"hash/crc64"
"io"
"os"
"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 TestUnmarshalPlainStructure(t *testing.T) {
testCases := []struct {
content string
numParagraphs int
}{
{decoded_doc_1, 5},
{decoded_doc_2, 14},
}
for _, tc := range testCases {
doc := Document{
XMLW: XMLNS_W,
XMLR: XMLNS_R,
XMLWP: XMLNS_WP,
XMLName: xml.Name{Space: XMLNS_W, Local: "document"}}
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 TestUnmarshalDrawingStructure(t *testing.T) {
doc := Document{
XMLW: XMLNS_W,
XMLR: XMLNS_R,
XMLWP: XMLNS_WP,
XMLWP14: XMLNS_WP14,
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 {
anchor := "mock-anchor-p" + string(rune('0'+i)) + "-c" + string(rune('0'+j))
edit := "mock-edit-p" + string(rune('0'+i)) + "-c" + string(rune('0'+j))
if anchor != child.Run.Drawing.Inline.AnchorID {
t.Fatal("expect", anchor, "but got", child.Run.Drawing.Inline.AnchorID)
}
if edit != child.Run.Drawing.Inline.EditID {
t.Fatal("expect", edit, "but got", child.Run.Drawing.Inline.EditID)
}
if child.Run.Drawing.Inline.Graphic != nil && child.Run.Drawing.Inline.Graphic.GraphicData != nil {
t.Log(child.Run.Drawing.Inline.Graphic.GraphicData.URI)
if child.Run.Drawing.Inline.Graphic.GraphicData.Pic != nil {
t.Log(child.Run.Drawing.Inline.Graphic.GraphicData.Pic.NonVisualPicProperties.NonVisualDrawingProperties.ID, child.Run.Drawing.Inline.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
}
}
}
}
}
}
}
func TestMarshalDrawingStructure(t *testing.T) {
w := NewA4()
// add new paragraph
para1 := w.AddParagraph()
// add text
para1.AddText("直接粘贴 inline")
para2 := w.AddParagraph()
para2.AddInlineDrawingFrom("testdata/fumiama.JPG")
para2.AddInlineDrawingFrom("testdata/fumiama2x.webp")
para3 := w.AddParagraph()
para3.AddInlineDrawingFrom("testdata/fumiamayoko.png")
f, err := os.Create("TestMarshalDrawingStructure_Marshal.xml")
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = marshaller{data: w.Document}.WriteTo(f)
if err != nil {
t.Fatal(err)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
t.Fatal(err)
}
w = NewA4()
err = xml.NewDecoder(f).Decode(&w.Document)
if err != nil {
t.Fatal(err)
}
f1, err := os.Create("TestMarshalDrawingStructure_Unmarshal.xml")
if err != nil {
t.Fatal(err)
}
defer f1.Close()
_, err = marshaller{data: w.Document}.WriteTo(f1)
if err != nil {
t.Fatal(err)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
t.Fatal(err)
}
_, err = f1.Seek(0, io.SeekStart)
if err != nil {
t.Fatal(err)
}
h := crc64.New(crc64.MakeTable(crc64.ECMA))
_, err = io.Copy(h, f)
if err != nil {
t.Fatal(err)
}
md51 := h.Sum64()
h.Reset()
_, err = io.Copy(h, f1)
if err != nil {
t.Fatal(err)
}
md52 := h.Sum64()
if md51 != md52 {
t.Fail()
} /* else {
_ = os.Remove("TestMarshalDrawingStructure_Marshal.xml")
_ = os.Remove("TestMarshalDrawingStructure_Unmarshal.xml")
}*/
}