mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-25 13:48:53 +08:00
test: add more tests for xsdany and optional
- XSDAny - filename generation - optional
This commit is contained in:
parent
f0de9225cf
commit
20dce01067
80
filenames_test.go
Normal file
80
filenames_test.go
Normal file
@ -0,0 +1,80 @@
|
||||
// Copyright 2017 Baliance. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by the terms of the Affero GNU General
|
||||
// Public License version 3.0 as published by the Free Software Foundation and
|
||||
// appearing in the file LICENSE included in the packaging of this file. A
|
||||
// commercial license can be purchased by contacting sales@baliance.com.
|
||||
|
||||
package gooxml_test
|
||||
|
||||
import "testing"
|
||||
import "baliance.com/gooxml"
|
||||
|
||||
func TestWMLFilenames(t *testing.T) {
|
||||
td := []struct {
|
||||
Idx int
|
||||
Type string
|
||||
ExpRel string
|
||||
ExpAbs string
|
||||
}{
|
||||
{0, gooxml.CorePropertiesType, "docProps/core.xml", "docProps/core.xml"},
|
||||
{0, gooxml.ExtendedPropertiesType, "docProps/app.xml", "docProps/app.xml"},
|
||||
{0, gooxml.ThumbnailType, "docProps/thumbnail.jpeg", "docProps/thumbnail.jpeg"},
|
||||
{0, gooxml.StylesType, "styles.xml", "word/styles.xml"},
|
||||
|
||||
{0, gooxml.OfficeDocumentType, "word/document.xml", "word/document.xml"},
|
||||
{0, gooxml.FontTableType, "fontTable.xml", "word/fontTable.xml"},
|
||||
{0, gooxml.EndNotesType, "endnotes.xml", "word/endnotes.xml"},
|
||||
{0, gooxml.FootNotesType, "footnotes.xml", "word/footnotes.xml"},
|
||||
{0, gooxml.NumberingType, "numbering.xml", "word/numbering.xml"},
|
||||
{0, gooxml.WebSettingsType, "webSettings.xml", "word/webSettings.xml"},
|
||||
{0, gooxml.SettingsType, "settings.xml", "word/settings.xml"},
|
||||
{23, gooxml.HeaderType, "header23.xml", "word/header23.xml"},
|
||||
{15, gooxml.FooterType, "footer15.xml", "word/footer15.xml"},
|
||||
{1, gooxml.ThemeType, "theme/theme1.xml", "word/theme/theme1.xml"},
|
||||
}
|
||||
for _, tc := range td {
|
||||
rel := gooxml.RelativeFilename(gooxml.DocTypeDocument, tc.Type, tc.Idx)
|
||||
abs := gooxml.AbsoluteFilename(gooxml.DocTypeDocument, tc.Type, tc.Idx)
|
||||
if rel != tc.ExpRel {
|
||||
t.Errorf("expected relative filename of %s for document %s/%d, got %s", tc.ExpRel, tc.Type, tc.Idx, rel)
|
||||
}
|
||||
if abs != tc.ExpAbs {
|
||||
t.Errorf("expected absolute filename of %s for document %s/%d, got %s", tc.ExpAbs, tc.Type, tc.Idx, abs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSMLFilenames(t *testing.T) {
|
||||
td := []struct {
|
||||
Idx int
|
||||
Type string
|
||||
ExpRel string
|
||||
ExpAbs string
|
||||
}{
|
||||
{0, gooxml.CorePropertiesType, "docProps/core.xml", "docProps/core.xml"},
|
||||
{0, gooxml.ExtendedPropertiesType, "docProps/app.xml", "docProps/app.xml"},
|
||||
{0, gooxml.ThumbnailType, "docProps/thumbnail.jpeg", "docProps/thumbnail.jpeg"},
|
||||
{0, gooxml.StylesType, "styles.xml", "xl/styles.xml"},
|
||||
|
||||
{0, gooxml.OfficeDocumentType, "xl/workbook.xml", "xl/workbook.xml"},
|
||||
{15, gooxml.ChartType, "../charts/chart15.xml", "xl/charts/chart15.xml"},
|
||||
{12, gooxml.DrawingType, "../drawings/drawing12.xml", "xl/drawings/drawing12.xml"},
|
||||
{13, gooxml.TableType, "../tables/table13.xml", "xl/tables/table13.xml"},
|
||||
{2, gooxml.CommentsType, "../comments2.xml", "xl/comments2.xml"},
|
||||
{15, gooxml.WorksheetType, "worksheets/sheet15.xml", "xl/worksheets/sheet15.xml"},
|
||||
{2, gooxml.VMLDrawingType, "../drawings/vmlDrawing2.vml", "xl/drawings/vmlDrawing2.vml"},
|
||||
{0, gooxml.SharedStingsType, "sharedStrings.xml", "xl/sharedStrings.xml"},
|
||||
{1, gooxml.ThemeType, "theme/theme1.xml", "xl/theme/theme1.xml"},
|
||||
}
|
||||
for _, tc := range td {
|
||||
rel := gooxml.RelativeFilename(gooxml.DocTypeSpreadsheet, tc.Type, tc.Idx)
|
||||
abs := gooxml.AbsoluteFilename(gooxml.DocTypeSpreadsheet, tc.Type, tc.Idx)
|
||||
if rel != tc.ExpRel {
|
||||
t.Errorf("expected relative filename of %s for document %s/%d, got %s", tc.ExpRel, tc.Type, tc.Idx, rel)
|
||||
}
|
||||
if abs != tc.ExpAbs {
|
||||
t.Errorf("expected absolute filename of %s for document %s/%d, got %s", tc.ExpAbs, tc.Type, tc.Idx, abs)
|
||||
}
|
||||
}
|
||||
}
|
4
mingo.go
4
mingo.go
@ -7,4 +7,8 @@
|
||||
|
||||
package gooxml
|
||||
|
||||
// MinGoVersion is used to cause a compile time error if gooxml is compiled with
|
||||
// an older version of go. Specifically it requires a feature in go 1.8
|
||||
// regarding collecting all attributes from arbitrary xml used in decode
|
||||
// gooxml.XSDAny.
|
||||
const MinGoVersion = requires_go_18
|
||||
|
@ -75,7 +75,8 @@ func String(v string) *string {
|
||||
return &x
|
||||
}
|
||||
|
||||
// String returns a copy of v as a pointer.
|
||||
// Stringf formats according to a format specifier and returns a pointer to the
|
||||
// resulting string.
|
||||
func Stringf(f string, args ...interface{}) *string {
|
||||
x := fmt.Sprintf(f, args...)
|
||||
return &x
|
||||
|
@ -13,6 +13,14 @@ import (
|
||||
"baliance.com/gooxml"
|
||||
)
|
||||
|
||||
func TestFloat32(t *testing.T) {
|
||||
exp := float32(1.234)
|
||||
got := gooxml.Float32(exp)
|
||||
if *got != exp {
|
||||
t.Errorf("expected %f, got %f", exp, *got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64(t *testing.T) {
|
||||
exp := 1.234
|
||||
got := gooxml.Float64(exp)
|
||||
@ -53,6 +61,14 @@ func TestInt32(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt8(t *testing.T) {
|
||||
exp := int8(123)
|
||||
got := gooxml.Int8(exp)
|
||||
if *got != exp {
|
||||
t.Errorf("expected %d, got %d", exp, *got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBool(t *testing.T) {
|
||||
exp := bool(true)
|
||||
got := gooxml.Bool(exp)
|
||||
@ -61,10 +77,34 @@ func TestBool(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSTring(t *testing.T) {
|
||||
func TestString(t *testing.T) {
|
||||
exp := "foo"
|
||||
got := gooxml.String(exp)
|
||||
if *got != exp {
|
||||
t.Errorf("expected %s, got %s", exp, *got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint8(t *testing.T) {
|
||||
exp := uint8(123)
|
||||
got := gooxml.Uint8(exp)
|
||||
if *got != exp {
|
||||
t.Errorf("expected %d, got %d", exp, *got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUint16(t *testing.T) {
|
||||
exp := uint16(123)
|
||||
got := gooxml.Uint16(exp)
|
||||
if *got != exp {
|
||||
t.Errorf("expected %d, got %d", exp, *got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringf(t *testing.T) {
|
||||
exp := "foobar123"
|
||||
got := gooxml.Stringf("foo%s%d", "bar", 123)
|
||||
if *got != exp {
|
||||
t.Errorf("expected %s, got %s", exp, *got)
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,10 @@
|
||||
// Copyright 2017 Baliance. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by the terms of the Affero GNU General
|
||||
// Public License version 3.0 as published by the Free Software Foundation and
|
||||
// appearing in the file LICENSE included in the packaging of this file. A
|
||||
// commercial license can be purchased by contacting sales@baliance.com.
|
||||
|
||||
package gooxml
|
||||
|
||||
import (
|
||||
|
37
xsdany_test.go
Normal file
37
xsdany_test.go
Normal file
@ -0,0 +1,37 @@
|
||||
// Copyright 2017 Baliance. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by the terms of the Affero GNU General
|
||||
// Public License version 3.0 as published by the Free Software Foundation and
|
||||
// appearing in the file LICENSE included in the packaging of this file. A
|
||||
// commercial license can be purchased by contacting sales@baliance.com.
|
||||
|
||||
package gooxml_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"baliance.com/gooxml"
|
||||
)
|
||||
|
||||
func TestXSDAny(t *testing.T) {
|
||||
any := gooxml.XSDAny{}
|
||||
anyXml := `<w:settings xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" mc:Ignorable="w14 w15 w16se">
|
||||
<w:hdrShapeDefaults><o:shapedefaults v:ext="edit" spidmax="2049"><o:idmap v:ext="edit" data="1"/></o:shapedefaults></w:hdrShapeDefaults></w:settings>`
|
||||
|
||||
exp := `<m:settings ma:Ignorable="w14 w15 w16se" xmlns:m="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:ma="http://schemas.openxmlformats.org/markup-compatibility" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml">
	<m:hdrShapeDefaults><o:shapedefaults v:ext="edit" spidmax="2049"><o:idmap v:ext="edit" data="1"></o:idmap></o:shapedefaults></m:hdrShapeDefaults></m:settings>`
|
||||
dec := xml.NewDecoder(strings.NewReader(anyXml))
|
||||
if err := dec.Decode(&any); err != nil {
|
||||
t.Errorf("error decoding XSDAny: %s", err)
|
||||
}
|
||||
buf := bytes.Buffer{}
|
||||
enc := xml.NewEncoder(&buf)
|
||||
if err := enc.Encode(&any); err != nil {
|
||||
t.Errorf("error encoding XSDAny: %s", err)
|
||||
}
|
||||
if buf.String() != exp {
|
||||
t.Errorf("expected %s, got %s", exp, buf.String())
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user