sharedstrings_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package xlsx
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "testing"
  6. )
  7. // Test we can correctly convert a XLSXSST into a reference table using xlsx.MakeSharedStringRefTable().
  8. func TestMakeSharedStringRefTable(t *testing.T) {
  9. var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  10. <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>`)
  11. sst := new(XLSXSST)
  12. error := xml.NewDecoder(sharedstringsXML).Decode(sst)
  13. if error != nil {
  14. t.Error(error.String())
  15. return
  16. }
  17. reftable := MakeSharedStringRefTable(sst)
  18. if len(reftable) == 0 {
  19. t.Error("Reftable is zero length.")
  20. return
  21. }
  22. if reftable[0] != "Foo" {
  23. t.Error("RefTable lookup failed, expected reftable[0] == 'Foo'")
  24. }
  25. if reftable[1] != "Bar" {
  26. t.Error("RefTable lookup failed, expected reftable[1] == 'Bar'")
  27. }
  28. }
  29. // Test we can correctly resolve a numeric reference in the reference table to a string value using xlsx.ResolveSharedString().
  30. func TestResolveSharedString(t *testing.T) {
  31. var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  32. <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>`)
  33. sst := new(XLSXSST)
  34. error := xml.NewDecoder(sharedstringsXML).Decode(sst)
  35. if error != nil {
  36. t.Error(error.String())
  37. return
  38. }
  39. reftable := MakeSharedStringRefTable(sst)
  40. if ResolveSharedString(reftable, 0) != "Foo" {
  41. t.Error("Expected ResolveSharedString(reftable, 0) == 'Foo'")
  42. }
  43. }
  44. // Test we can correctly unmarshal an the sharedstrings.xml file into
  45. // an xlsx.XLSXSST struct and it's associated children.
  46. func TestUnmarshallSharedStrings(t *testing.T) {
  47. var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  48. <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>`)
  49. sst := new(XLSXSST)
  50. error := xml.NewDecoder(sharedstringsXML).Decode(sst)
  51. if error != nil {
  52. t.Error(error.String())
  53. return
  54. }
  55. if sst.Count != "4" {
  56. t.Error(`sst.Count != "4"`)
  57. }
  58. if sst.UniqueCount != "4" {
  59. t.Error(`sst.UniqueCount != 4`)
  60. }
  61. if len(sst.SI) == 0 {
  62. t.Error("Expected 4 sst.SI but found none")
  63. }
  64. si := sst.SI[0]
  65. if si.T.Data != "Foo" {
  66. t.Error("Expected s.T.Data == 'Foo'")
  67. }
  68. }