sharedstrings_test.go 2.2 KB

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