/*
Copyright (c) 2020 gingfrederik
Copyright (c) 2021 Gonzalo Fernandez-Victorio
Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com)
Copyright (c) 2023 Fumiama Minamoto (源文雨)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package docx
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 TestUnmarshalPlainStructure(t *testing.T) {
testCases := []struct {
content string
numParagraphs int
}{
{decoded_doc_1, 6},
{decoded_doc_2, 15},
}
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.Items) != tc.numParagraphs {
t.Fatalf("We expected %d paragraphs, we got %d", tc.numParagraphs, len(doc.Body.Items))
}
for i, it := range doc.Body.Items {
switch v := it.(type) {
case *Paragraph:
if len(v.Children) == 0 {
t.Fatalf("We were not able to parse paragraph %d", i)
}
for _, child := range v.Children {
if child == nil {
t.Fatalf("There are Paragraph children with all fields nil")
}
if o, ok := child.(*Hyperlink); ok && o.ID == "" {
t.Fatalf("We have a link without ID")
}
}
case *SectPr:
if v.PgSz.W.Value != "11906" || v.PgSz.H.Value != "16838" {
t.Fatalf("We were not able to parse sectPr")
}
}
}
}
}