reftable_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package xlsx
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. . "gopkg.in/check.v1"
  6. )
  7. type RefTableSuite struct {
  8. SharedStringsXML *bytes.Buffer
  9. }
  10. var _ = Suite(&RefTableSuite{})
  11. func (s *RefTableSuite) SetUpTest(c *C) {
  12. s.SharedStringsXML = bytes.NewBufferString(
  13. `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  14. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  15. count="4"
  16. uniqueCount="4">
  17. <si>
  18. <t>Foo</t>
  19. </si>
  20. <si>
  21. <t>Bar</t>
  22. </si>
  23. <si>
  24. <t xml:space="preserve">Baz </t>
  25. </si>
  26. <si>
  27. <t>Quuk</t>
  28. </si>
  29. </sst>`)
  30. }
  31. // We can add a new string to the RefTable
  32. func (s *RefTableSuite) TestRefTableAddString(c *C) {
  33. refTable := NewSharedStringRefTable()
  34. index := refTable.AddString("Foo")
  35. c.Assert(index, Equals, 0)
  36. c.Assert(refTable.ResolveSharedString(0), Equals, "Foo")
  37. }
  38. func (s *RefTableSuite) TestCreateNewSharedStringRefTable(c *C) {
  39. refTable := NewSharedStringRefTable()
  40. refTable.AddString("Foo")
  41. refTable.AddString("Bar")
  42. c.Assert(refTable.ResolveSharedString(0), Equals, "Foo")
  43. c.Assert(refTable.ResolveSharedString(1), Equals, "Bar")
  44. }
  45. // Test we can correctly convert a xlsxSST into a reference table
  46. // using xlsx.MakeSharedStringRefTable().
  47. func (s *RefTableSuite) TestMakeSharedStringRefTable(c *C) {
  48. sst := new(xlsxSST)
  49. err := xml.NewDecoder(s.SharedStringsXML).Decode(sst)
  50. c.Assert(err, IsNil)
  51. reftable := MakeSharedStringRefTable(sst)
  52. c.Assert(reftable.Length(), Equals, 4)
  53. c.Assert(reftable.ResolveSharedString(0), Equals, "Foo")
  54. c.Assert(reftable.ResolveSharedString(1), Equals, "Bar")
  55. }
  56. // Test we can correctly resolve a numeric reference in the reference
  57. // table to a string value using RefTable.ResolveSharedString().
  58. func (s *RefTableSuite) TestResolveSharedString(c *C) {
  59. sst := new(xlsxSST)
  60. err := xml.NewDecoder(s.SharedStringsXML).Decode(sst)
  61. c.Assert(err, IsNil)
  62. reftable := MakeSharedStringRefTable(sst)
  63. c.Assert(reftable.ResolveSharedString(0), Equals, "Foo")
  64. }
  65. // Test we can correctly create the xlsx.xlsxSST struct from a RefTable
  66. func (s *RefTableSuite) TestMakeXLSXSST(c *C) {
  67. refTable := NewSharedStringRefTable()
  68. refTable.AddString("Foo")
  69. refTable.AddString("Bar")
  70. sst := refTable.makeXLSXSST()
  71. c.Assert(sst, NotNil)
  72. c.Assert(sst.Count, Equals, 2)
  73. c.Assert(sst.UniqueCount, Equals, 2)
  74. c.Assert(sst.SI, HasLen, 2)
  75. si := sst.SI[0]
  76. c.Assert(si.T, Equals, "Foo")
  77. }
  78. func (s *RefTableSuite) TestMarshalSST(c *C) {
  79. refTable := NewSharedStringRefTable()
  80. refTable.AddString("Foo")
  81. sst := refTable.makeXLSXSST()
  82. output := bytes.NewBufferString(xml.Header)
  83. body, err := xml.Marshal(sst)
  84. c.Assert(err, IsNil)
  85. c.Assert(body, NotNil)
  86. _, err = output.Write(body)
  87. c.Assert(err, IsNil)
  88. expectedXLSXSST := `<?xml version="1.0" encoding="UTF-8"?>
  89. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="1" uniqueCount="1"><si><t>Foo</t></si></sst>`
  90. c.Assert(output.String(), Equals, expectedXLSXSST)
  91. }
  92. func (s *RefTableSuite) TestRefTableReadAddString(c *C) {
  93. refTable := NewSharedStringRefTable()
  94. refTable.isWrite = false
  95. index1 := refTable.AddString("Foo")
  96. index2 := refTable.AddString("Foo")
  97. c.Assert(index1, Equals, 0)
  98. c.Assert(index2, Equals, 1)
  99. c.Assert(refTable.ResolveSharedString(0), Equals, "Foo")
  100. c.Assert(refTable.ResolveSharedString(1), Equals, "Foo")
  101. }
  102. func (s *RefTableSuite) TestRefTableWriteAddString(c *C) {
  103. refTable := NewSharedStringRefTable()
  104. refTable.isWrite = true
  105. index1 := refTable.AddString("Foo")
  106. index2 := refTable.AddString("Foo")
  107. c.Assert(index1, Equals, 0)
  108. c.Assert(index2, Equals, 0)
  109. c.Assert(refTable.ResolveSharedString(0), Equals, "Foo")
  110. }