Skip to content

Commit

Permalink
Split sharedstrings.go and renamed to reftable.go and xmlSharedString…
Browse files Browse the repository at this point in the history
…s.go
  • Loading branch information
tealeg committed Nov 1, 2014
1 parent bb71ed2 commit f8e6fa2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 55 deletions.
31 changes: 0 additions & 31 deletions sharedstrings.go → reftable.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,5 @@
package xlsx

import (
"encoding/xml"
)

// xlsxSST directly maps the sst element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main currently
// I have not checked this for completeness - it does as much as I need.
type xlsxSST struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main sst"`
Count int `xml:"count,attr"`
UniqueCount int `xml:"uniqueCount,attr"`
SI []xlsxSI `xml:"si"`
}

// xlsxSI directly maps the si element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked this for completeness - it does as
// much as I need.
type xlsxSI struct {
T string `xml:"t"`
R []xlsxR `xml:"r"`
}

// xlsxR directly maps the r element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked this for completeness - it does as
// much as I need.
type xlsxR struct {
T string `xml:"t"`
}

type RefTable struct {
indexedStrings []string
knownStrings map[string]int
Expand Down
36 changes: 12 additions & 24 deletions sharedstrings_test.go → reftable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package xlsx
import (
"bytes"
"encoding/xml"

. "gopkg.in/check.v1"
)

type SharedStringsSuite struct {
type RefTableSuite struct {
SharedStringsXML *bytes.Buffer
}

var _ = Suite(&SharedStringsSuite{})
var _ = Suite(&RefTableSuite{})

func (s *SharedStringsSuite) SetUpTest(c *C) {
func (s *RefTableSuite) SetUpTest(c *C) {
s.SharedStringsXML = bytes.NewBufferString(
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
Expand All @@ -34,14 +35,14 @@ func (s *SharedStringsSuite) SetUpTest(c *C) {
}

// We can add a new string to the RefTable
func (s *SharedStringsSuite) TestRefTableAddString(c *C) {
func (s *RefTableSuite) TestRefTableAddString(c *C) {
refTable := NewSharedStringRefTable()
index := refTable.AddString("Foo")
c.Assert(index, Equals, 0)
c.Assert(refTable.ResolveSharedString(0), Equals, "Foo")
}

func (s *SharedStringsSuite) TestCreateNewSharedStringRefTable(c *C) {
func (s *RefTableSuite) TestCreateNewSharedStringRefTable(c *C) {
refTable := NewSharedStringRefTable()
refTable.AddString("Foo")
refTable.AddString("Bar")
Expand All @@ -51,7 +52,7 @@ func (s *SharedStringsSuite) TestCreateNewSharedStringRefTable(c *C) {

// Test we can correctly convert a xlsxSST into a reference table
// using xlsx.MakeSharedStringRefTable().
func (s *SharedStringsSuite) TestMakeSharedStringRefTable(c *C) {
func (s *RefTableSuite) TestMakeSharedStringRefTable(c *C) {
sst := new(xlsxSST)
err := xml.NewDecoder(s.SharedStringsXML).Decode(sst)
c.Assert(err, IsNil)
Expand All @@ -63,29 +64,16 @@ func (s *SharedStringsSuite) TestMakeSharedStringRefTable(c *C) {

// Test we can correctly resolve a numeric reference in the reference
// table to a string value using RefTable.ResolveSharedString().
func (s *SharedStringsSuite) TestResolveSharedString(c *C) {
func (s *RefTableSuite) TestResolveSharedString(c *C) {
sst := new(xlsxSST)
err := xml.NewDecoder(s.SharedStringsXML).Decode(sst)
c.Assert(err, IsNil)
reftable := MakeSharedStringRefTable(sst)
c.Assert(reftable.ResolveSharedString(0), Equals, "Foo")
}

// Test we can correctly unmarshal an the sharedstrings.xml file into
// an xlsx.xlsxSST struct and it's associated children.
func (s *SharedStringsSuite) TestUnmarshallSharedStrings(c *C) {
sst := new(xlsxSST)
err := xml.NewDecoder(s.SharedStringsXML).Decode(sst)
c.Assert(err, IsNil)
c.Assert(sst.Count, Equals, 4)
c.Assert(sst.UniqueCount, Equals, 4)
c.Assert(sst.SI, HasLen, 4)
si := sst.SI[0]
c.Assert(si.T, Equals, "Foo")
}

// Test we can correctly create the xlsx.xlsxSST struct from a RefTable
func (s *SharedStringsSuite) TestMakeXLSXSST(c *C) {
func (s *RefTableSuite) TestMakeXLSXSST(c *C) {
refTable := NewSharedStringRefTable()
refTable.AddString("Foo")
refTable.AddString("Bar")
Expand All @@ -98,7 +86,7 @@ func (s *SharedStringsSuite) TestMakeXLSXSST(c *C) {
c.Assert(si.T, Equals, "Foo")
}

func (s *SharedStringsSuite) TestMarshalSST(c *C) {
func (s *RefTableSuite) TestMarshalSST(c *C) {
refTable := NewSharedStringRefTable()
refTable.AddString("Foo")
sst := refTable.makeXLSXSST()
Expand All @@ -119,7 +107,7 @@ func (s *SharedStringsSuite) TestMarshalSST(c *C) {
c.Assert(output.String(), Equals, expectedXLSXSST)
}

func (s *SharedStringsSuite) TestRefTableReadAddString(c *C) {
func (s *RefTableSuite) TestRefTableReadAddString(c *C) {
refTable := NewSharedStringRefTable()
refTable.isWrite = false
index1 := refTable.AddString("Foo")
Expand All @@ -130,7 +118,7 @@ func (s *SharedStringsSuite) TestRefTableReadAddString(c *C) {
c.Assert(refTable.ResolveSharedString(1), Equals, "Foo")
}

func (s *SharedStringsSuite) TestRefTableWriteAddString(c *C) {
func (s *RefTableSuite) TestRefTableWriteAddString(c *C) {
refTable := NewSharedStringRefTable()
refTable.isWrite = true
index1 := refTable.AddString("Foo")
Expand Down
32 changes: 32 additions & 0 deletions xmlSharedStrings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package xlsx

import (
"encoding/xml"
)

// xlsxSST directly maps the sst element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main currently
// I have not checked this for completeness - it does as much as I need.
type xlsxSST struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main sst"`
Count int `xml:"count,attr"`
UniqueCount int `xml:"uniqueCount,attr"`
SI []xlsxSI `xml:"si"`
}

// xlsxSI directly maps the si element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked this for completeness - it does as
// much as I need.
type xlsxSI struct {
T string `xml:"t"`
R []xlsxR `xml:"r"`
}

// xlsxR directly maps the r element from the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked this for completeness - it does as
// much as I need.
type xlsxR struct {
T string `xml:"t"`
}
48 changes: 48 additions & 0 deletions xmlSharedStrings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package xlsx

import (
"bytes"
"encoding/xml"

. "gopkg.in/check.v1"
)

type SharedStringsSuite struct {
SharedStringsXML *bytes.Buffer
}

var _ = Suite(&SharedStringsSuite{})

func (s *SharedStringsSuite) SetUpTest(c *C) {
s.SharedStringsXML = bytes.NewBufferString(
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
count="4"
uniqueCount="4">
<si>
<t>Foo</t>
</si>
<si>
<t>Bar</t>
</si>
<si>
<t xml:space="preserve">Baz </t>
</si>
<si>
<t>Quuk</t>
</si>
</sst>`)
}

// Test we can correctly unmarshal an the sharedstrings.xml file into
// an xlsx.xlsxSST struct and it's associated children.
func (s *SharedStringsSuite) TestUnmarshallSharedStrings(c *C) {
sst := new(xlsxSST)
err := xml.NewDecoder(s.SharedStringsXML).Decode(sst)
c.Assert(err, IsNil)
c.Assert(sst.Count, Equals, 4)
c.Assert(sst.UniqueCount, Equals, 4)
c.Assert(sst.SI, HasLen, 4)
si := sst.SI[0]
c.Assert(si.T, Equals, "Foo")
}

0 comments on commit f8e6fa2

Please sign in to comment.