This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reftable_test.go
125 lines (110 loc) · 3.68 KB
/
reftable_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package xlsx
import (
"bytes"
"encoding/xml"
. "gopkg.in/check.v1"
)
type RefTableSuite struct {
SharedStringsXML *bytes.Buffer
}
var _ = Suite(&RefTableSuite{})
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"
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>`)
}
// We can add a new string to the RefTable
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 *RefTableSuite) TestCreateNewSharedStringRefTable(c *C) {
refTable := NewSharedStringRefTable()
refTable.AddString("Foo")
refTable.AddString("Bar")
c.Assert(refTable.ResolveSharedString(0), Equals, "Foo")
c.Assert(refTable.ResolveSharedString(1), Equals, "Bar")
}
// Test we can correctly convert a xlsxSST into a reference table
// using xlsx.MakeSharedStringRefTable().
func (s *RefTableSuite) TestMakeSharedStringRefTable(c *C) {
sst := new(xlsxSST)
err := xml.NewDecoder(s.SharedStringsXML).Decode(sst)
c.Assert(err, IsNil)
reftable := MakeSharedStringRefTable(sst)
c.Assert(reftable.Length(), Equals, 4)
c.Assert(reftable.ResolveSharedString(0), Equals, "Foo")
c.Assert(reftable.ResolveSharedString(1), Equals, "Bar")
}
// Test we can correctly resolve a numeric reference in the reference
// table to a string value using RefTable.ResolveSharedString().
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 create the xlsx.xlsxSST struct from a RefTable
func (s *RefTableSuite) TestMakeXLSXSST(c *C) {
refTable := NewSharedStringRefTable()
refTable.AddString("Foo")
refTable.AddString("Bar")
sst := refTable.makeXLSXSST()
c.Assert(sst, NotNil)
c.Assert(sst.Count, Equals, 2)
c.Assert(sst.UniqueCount, Equals, 2)
c.Assert(sst.SI, HasLen, 2)
si := sst.SI[0]
c.Assert(si.T, Equals, "Foo")
}
func (s *RefTableSuite) TestMarshalSST(c *C) {
refTable := NewSharedStringRefTable()
refTable.AddString("Foo")
sst := refTable.makeXLSXSST()
output := bytes.NewBufferString(xml.Header)
body, err := xml.Marshal(sst)
c.Assert(err, IsNil)
c.Assert(body, NotNil)
_, err = output.Write(body)
c.Assert(err, IsNil)
expectedXLSXSST := `<?xml version="1.0" encoding="UTF-8"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="1" uniqueCount="1"><si><t>Foo</t></si></sst>`
c.Assert(output.String(), Equals, expectedXLSXSST)
}
func (s *RefTableSuite) TestRefTableReadAddString(c *C) {
refTable := NewSharedStringRefTable()
refTable.isWrite = false
index1 := refTable.AddString("Foo")
index2 := refTable.AddString("Foo")
c.Assert(index1, Equals, 0)
c.Assert(index2, Equals, 1)
c.Assert(refTable.ResolveSharedString(0), Equals, "Foo")
c.Assert(refTable.ResolveSharedString(1), Equals, "Foo")
}
func (s *RefTableSuite) TestRefTableWriteAddString(c *C) {
refTable := NewSharedStringRefTable()
refTable.isWrite = true
index1 := refTable.AddString("Foo")
index2 := refTable.AddString("Foo")
c.Assert(index1, Equals, 0)
c.Assert(index2, Equals, 0)
c.Assert(refTable.ResolveSharedString(0), Equals, "Foo")
}