unioffice/creator_test.go

78 lines
2.6 KiB
Go
Raw Normal View History

// Copyright 2017 FoxyUtils ehf. All rights reserved.
2017-08-29 16:55:03 -05:00
//
// Use of this software package and source code is governed by the terms of the
// UniDoc End User License Agreement (EULA) that is available at:
// https://unidoc.io/eula/
// A trial license code for evaluation can be obtained at https://unidoc.io.
2017-08-29 16:55:03 -05:00
package unioffice_test
2017-08-29 16:55:03 -05:00
import (
"bytes"
"encoding/xml"
"fmt"
"os"
"strings"
"testing"
"github.com/unidoc/unioffice"
2017-09-01 19:29:06 -05:00
"github.com/unidoc/unioffice/schema/soo/wml"
"github.com/unidoc/unioffice/zippkg"
2017-08-29 16:55:03 -05:00
)
2017-09-01 19:29:06 -05:00
func TestCreatorUnknownType(t *testing.T) {
el, err := unioffice.CreateElement(xml.StartElement{Name: xml.Name{Local: "foo", Space: "bar"}})
2017-09-01 19:29:06 -05:00
if el == nil || err != nil {
t.Errorf("CreateElement should never return nil: %s", err)
}
if _, ok := el.(*unioffice.XSDAny); !ok {
2017-09-01 19:29:06 -05:00
t.Errorf("CreateElement should return XSDAny for unknown types")
}
}
func TestCreatorKnownType(t *testing.T) {
el, err := unioffice.CreateElement(xml.StartElement{Name: xml.Name{Local: "CT_Settings", Space: "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}})
2017-09-01 19:29:06 -05:00
if el == nil || err != nil {
t.Errorf("CreateElement should never return nil: %s", err)
}
if _, ok := el.(*wml.CT_Settings); !ok {
t.Errorf("CreateElement should return the element requested, got %T", el)
}
}
2017-08-29 16:55:03 -05:00
func TestRawEncode(t *testing.T) {
f, err := os.Open("testdata/settings.xml")
if err != nil {
t.Fatalf("error reading settings file")
}
dec := xml.NewDecoder(f)
var got *bytes.Buffer
// should round trip multiple times with no changes after
// the first encoding
for i := 0; i < 5; i++ {
2017-08-29 16:55:03 -05:00
stng := wml.NewSettings()
if err := dec.Decode(stng); err != nil {
t.Errorf("error decoding settings: %s", err)
}
got = &bytes.Buffer{}
fmt.Fprintf(got, zippkg.XMLHeader)
enc := xml.NewEncoder(zippkg.SelfClosingWriter{W: got})
if err := enc.Encode(stng); err != nil {
t.Errorf("error encoding settings: %s", err)
}
dec = xml.NewDecoder(bytes.NewReader(got.Bytes()))
}
xmlStr := got.String()
beg := strings.LastIndex(xmlStr, "<w:hdrShapeDefaults>")
end := strings.LastIndex(xmlStr, "</w:hdrShapeDefaults>")
2017-08-29 16:55:03 -05:00
gotRaw := xmlStr[beg+20 : end]
exp := "<o:shapedefaults xmlns=\"urn:schemas-microsoft-com:office:office\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:s=\"http://schemas.openxmlformats.org/officeDocument/2006/sharedTypes\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" spidmax=\"2049\" ext=\"edit\"/>"
2017-08-29 16:55:03 -05:00
if gotRaw != exp {
t.Errorf("expected\n%q\ngot\n%q\n", exp, gotRaw)
}
2017-08-29 16:55:03 -05:00
}