Skip to content

Commit

Permalink
Merge pull request #7 from qmuntal/encode-indent
Browse files Browse the repository at this point in the history
Encode using 4 spaces
  • Loading branch information
qmuntal committed Jun 30, 2020
2 parents 6b00854 + 7cb15d2 commit 1daf83d
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 234 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ language: go
go_import_path: github.com/qmuntal/opc

go:
- 1.10.x
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x

env:
- GO111MODULE=on
Expand Down
12 changes: 8 additions & 4 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ func (p *pkg) checkPrefixCollision(uri string) bool {
}

func (p *pkg) encodeContentTypes(w io.Writer) error {
w.Write(([]byte)(`<?xml version="1.0" encoding="UTF-8"?>`))
return xml.NewEncoder(w).Encode(p.contentTypes.toXML())
w.Write(([]byte)(xml.Header))
enc := xml.NewEncoder(w)
enc.Indent("", " ")
return enc.Encode(p.contentTypes.toXML())
}

func (p *pkg) checkStringsPrefixCollision(s1, s2 string) bool {
Expand Down Expand Up @@ -259,8 +261,10 @@ type CoreProperties struct {
}

func (c *CoreProperties) encode(w io.Writer) error {
w.Write(([]byte)(`<?xml version="1.0" encoding="UTF-8"?>`))
return xml.NewEncoder(w).Encode(&corePropertiesXMLMarshal{
w.Write(([]byte)(xml.Header))
enc := xml.NewEncoder(w)
enc.Indent("", " ")
return enc.Encode(&corePropertiesXMLMarshal{
xml.Name{Local: "coreProperties"},
"http:https://schemas.openxmlformats.org/package/2006/metadata/core-properties",
"http:https://purl.org/dc/terms/",
Expand Down
267 changes: 143 additions & 124 deletions package_test.go
Original file line number Diff line number Diff line change
@@ -1,124 +1,143 @@
package opc

import (
"bytes"
"reflect"
"strings"
"testing"
)

func createFakePackage(m ...string) *pkg {
parts := make(map[string]*Part, len(m))
for _, s := range m {
parts[strings.ToUpper(s)] = new(Part)
}
return &pkg{
parts: parts,
}
}

func Test_newPackage(t *testing.T) {
tests := []struct {
name string
want *pkg
}{
{"base", &pkg{
parts: make(map[string]*Part, 0),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newPackage(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newPackage() = %v, want %v", got, tt.want)
}
})
}
}

func TestPackage_deletePart(t *testing.T) {
type args struct {
uri string
}
tests := []struct {
name string
p *pkg
args args
}{
{"empty", newPackage(), args{"/a.xml"}},
{"existing", createFakePackage("/a.xml"), args{"/a.xml"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.p.deletePart(tt.args.uri)
if _, ok := tt.p.parts[strings.ToUpper(tt.args.uri)]; ok {
t.Error("pkg.deletePart() should have deleted the part")
}
})
}
}

func TestPackage_add(t *testing.T) {
type args struct {
part *Part
}
tests := []struct {
name string
p *pkg
args args
wantContentTypes contentTypes
wantErr bool
}{
{"base", createFakePackage("/b.xml"), args{&Part{"/A.xml", "a/b", nil}}, contentTypes{map[string]string{"xml": "a/b"}, nil}, false},
{"emptyContentType", createFakePackage(), args{&Part{"/A.xml", "", nil}}, contentTypes{}, true},
{"noExtension", createFakePackage(), args{&Part{"/A", "a/b", nil}}, contentTypes{nil, map[string]string{"/A": "a/b"}}, false},
{"duplicated", createFakePackage("/a.xml"), args{&Part{"/A.xml", "a/b", nil}}, contentTypes{}, true},
{"collision1", createFakePackage("/abc.xml", "/xyz/PQR/A.JPG"), args{&Part{"/abc.xml/b.xml", "a/b", nil}}, contentTypes{}, true},
{"collision2", createFakePackage("/abc.xml", "/xyz/PQR/A.JPG"), args{&Part{"/xyz/pqr", "a/b", nil}}, contentTypes{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.p.add(tt.args.part); (err != nil) != tt.wantErr {
t.Errorf("pkg.add() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !reflect.DeepEqual(tt.p.contentTypes, tt.wantContentTypes) {
t.Errorf("pkg.add() = %v, want %v", tt.p.contentTypes, tt.wantContentTypes)
}
})
}
}

func buildCoreString(content string) string {
s := `<?xml version="1.0" encoding="UTF-8"?>`
s += `<coreProperties xmlns="http:https://schemas.openxmlformats.org/package/2006/metadata/core-properties"`
s += ` xmlns:dcterms="http:https://purl.org/dc/terms/" xmlns:dc="http:https://purl.org/dc/elements/1.1/">`
return s + content + "</coreProperties>"
}

func TestCoreProperties_encode(t *testing.T) {
tests := []struct {
name string
c *CoreProperties
wantW string
wantErr bool
}{
{"empty", &CoreProperties{}, buildCoreString(""), false},
{"some", &CoreProperties{Category: "A", LastPrinted: "b"}, buildCoreString("<category>A</category><lastPrinted>b</lastPrinted>"), false},
{"all", &CoreProperties{"partName", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"},
buildCoreString("<category>a</category><contentStatus>b</contentStatus><dcterms:created>c</dcterms:created><dc:creator>d</dc:creator><dc:description>e</dc:description><dc:identifier>f</dc:identifier><keywords>g</keywords><dc:language>h</dc:language><lastModifiedBy>i</lastModifiedBy><lastPrinted>j</lastPrinted><dcterms:modified>k</dcterms:modified><revision>l</revision><dc:subject>m</dc:subject><dc:title>n</dc:title><version>o</version>"),
false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := tt.c.encode(w); (err != nil) != tt.wantErr {
t.Errorf("CoreProperties.encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("CoreProperties.encode() = %v, want %v", gotW, tt.wantW)
}
})
}
}
package opc

import (
"bytes"
"reflect"
"strings"
"testing"
)

func createFakePackage(m ...string) *pkg {
parts := make(map[string]*Part, len(m))
for _, s := range m {
parts[strings.ToUpper(s)] = new(Part)
}
return &pkg{
parts: parts,
}
}

func Test_newPackage(t *testing.T) {
tests := []struct {
name string
want *pkg
}{
{"base", &pkg{
parts: make(map[string]*Part, 0),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newPackage(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("newPackage() = %v, want %v", got, tt.want)
}
})
}
}

func TestPackage_deletePart(t *testing.T) {
type args struct {
uri string
}
tests := []struct {
name string
p *pkg
args args
}{
{"empty", newPackage(), args{"/a.xml"}},
{"existing", createFakePackage("/a.xml"), args{"/a.xml"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.p.deletePart(tt.args.uri)
if _, ok := tt.p.parts[strings.ToUpper(tt.args.uri)]; ok {
t.Error("pkg.deletePart() should have deleted the part")
}
})
}
}

func TestPackage_add(t *testing.T) {
type args struct {
part *Part
}
tests := []struct {
name string
p *pkg
args args
wantContentTypes contentTypes
wantErr bool
}{
{"base", createFakePackage("/b.xml"), args{&Part{"/A.xml", "a/b", nil}}, contentTypes{map[string]string{"xml": "a/b"}, nil}, false},
{"emptyContentType", createFakePackage(), args{&Part{"/A.xml", "", nil}}, contentTypes{}, true},
{"noExtension", createFakePackage(), args{&Part{"/A", "a/b", nil}}, contentTypes{nil, map[string]string{"/A": "a/b"}}, false},
{"duplicated", createFakePackage("/a.xml"), args{&Part{"/A.xml", "a/b", nil}}, contentTypes{}, true},
{"collision1", createFakePackage("/abc.xml", "/xyz/PQR/A.JPG"), args{&Part{"/abc.xml/b.xml", "a/b", nil}}, contentTypes{}, true},
{"collision2", createFakePackage("/abc.xml", "/xyz/PQR/A.JPG"), args{&Part{"/xyz/pqr", "a/b", nil}}, contentTypes{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.p.add(tt.args.part); (err != nil) != tt.wantErr {
t.Errorf("pkg.add() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !reflect.DeepEqual(tt.p.contentTypes, tt.wantContentTypes) {
t.Errorf("pkg.add() = %v, want %v", tt.p.contentTypes, tt.wantContentTypes)
}
})
}
}

func buildCoreString(content string) string {
s := `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
s += `<coreProperties xmlns="http:https://schemas.openxmlformats.org/package/2006/metadata/core-properties"`
s += ` xmlns:dcterms="http:https://purl.org/dc/terms/" xmlns:dc="http:https://purl.org/dc/elements/1.1/">`
return s + content + "</coreProperties>"
}

func TestCoreProperties_encode(t *testing.T) {
tests := []struct {
name string
c *CoreProperties
wantW string
wantErr bool
}{
{"empty", &CoreProperties{}, buildCoreString(""), false},
{"some", &CoreProperties{Category: "A", LastPrinted: "b"}, buildCoreString(`
<category>A</category>
<lastPrinted>b</lastPrinted>
`), false},
{"all", &CoreProperties{"partName", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o"},
buildCoreString(`
<category>a</category>
<contentStatus>b</contentStatus>
<dcterms:created>c</dcterms:created>
<dc:creator>d</dc:creator>
<dc:description>e</dc:description>
<dc:identifier>f</dc:identifier>
<keywords>g</keywords>
<dc:language>h</dc:language>
<lastModifiedBy>i</lastModifiedBy>
<lastPrinted>j</lastPrinted>
<dcterms:modified>k</dcterms:modified>
<revision>l</revision>
<dc:subject>m</dc:subject>
<dc:title>n</dc:title>
<version>o</version>
`),
false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := tt.c.encode(w); (err != nil) != tt.wantErr {
t.Errorf("CoreProperties.encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
t.Errorf("CoreProperties.encode() = %v, want %v", gotW, tt.wantW)
}
})
}
}
6 changes: 3 additions & 3 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ func Test_newReader_File(t *testing.T) {
func Test_newReader_ContentType(t *testing.T) {
invalidType := `<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http:https://schemas.openxmlformats.org/package/2006/content-types">
<Fake Extension="" ContentType=""/>
<Fake Extension="" ContentType=""/>
</Types>`

incorrectOverrideXML := `<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http:https://schemas.openxmlformats.org/package/2006/content-types">
<Override ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" PartName="/docProps/app.xml">
<Override ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" PartName="/docProps/app.xml">
</Types>`

incorrectDefaultXML := `<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http:https://schemas.openxmlformats.org/package/2006/content-types">
<Default ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" PartName="/docProps/app.xml">
<Default ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" PartName="/docProps/app.xml">
</Types>`

p := newPackage()
Expand Down
6 changes: 4 additions & 2 deletions relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ func validateRelationships(sourceURI string, rs []*Relationship) error {
}

func encodeRelationships(w io.Writer, rs []*Relationship) error {
w.Write(([]byte)(`<?xml version="1.0" encoding="UTF-8"?>`))
re := &relationshipsXML{XML: "http:https://schemas.openxmlformats.org/package/2006/relationships"}
for _, r := range rs {
re.RelsXML = append(re.RelsXML, r.toXML())
}
return xml.NewEncoder(w).Encode(re)
w.Write(([]byte)(xml.Header))
enc := xml.NewEncoder(w)
enc.Indent("", " ")
return enc.Encode(re)
}

func decodeRelationships(r io.Reader, partName string) ([]*Relationship, error) {
Expand Down
Loading

0 comments on commit 1daf83d

Please sign in to comment.