Browse Source

Split sharedstrings.go and renamed to reftable.go and xmlSharedStrings.go

Geoffrey J. Teale 11 years ago
parent
commit
e593ccca0c
4 changed files with 92 additions and 55 deletions
  1. 0 31
      reftable.go
  2. 12 24
      reftable_test.go
  3. 32 0
      xmlSharedStrings.go
  4. 48 0
      xmlSharedStrings_test.go

+ 0 - 31
sharedstrings.go → reftable.go

@@ -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

+ 12 - 24
sharedstrings_test.go → reftable_test.go

@@ -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"
@@ -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")
@@ -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)
@@ -63,7 +64,7 @@ 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)
@@ -71,21 +72,8 @@ func (s *SharedStringsSuite) TestResolveSharedString(c *C) {
 	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")
@@ -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()
@@ -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")
@@ -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")

+ 32 - 0
xmlSharedStrings.go

@@ -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 - 0
xmlSharedStrings_test.go

@@ -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")
+}