package docxlib import ( "encoding/xml" "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) { doc := Document{ XMLW: XMLNS_W, XMLR: XMLNS_R, XMLWP: XMLNS_WP, 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 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 { tail := "-mock-inline-p" + string(rune('0'+i)) + "-c" + string(rune('0'+j)) 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 "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) } 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) } } } } } } func TestMarshalDrawingStructure(t *testing.T) { w := New() // add new paragraph para1 := w.AddParagraph() // add text para1.AddText("直接粘贴 inline") para2 := w.AddParagraph() para2.AddText("test font size and color").Size("44").Color("ff0000") para2.AddText("test font size and color").Size("44").Color("ff0000") para2.AddText("test font size and color").Size("44").Color("ff0000") nextPara := w.AddParagraph() nextPara.AddLink("google", `http://google.com`) f, err := os.Create("test.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 = New() err = xml.NewDecoder(f).Decode(&w.Document) if err != nil { t.Fatal(err) } f1, err := os.Create("test1.xml") if err != nil { t.Fatal(err) } defer f1.Close() _, err = marshaller{data: w.Document}.WriteTo(f1) if err != nil { t.Fatal(err) } t.Fail() }