-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
sheet_readstream_test.go
216 lines (187 loc) · 6.5 KB
/
sheet_readstream_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2017 Andrey Gayvoronsky <plandem@gmail.com>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx_test
import (
"github.com/plandem/xlsx"
"github.com/plandem/xlsx/types/options/sheet"
"github.com/stretchr/testify/require"
"testing"
)
//load content using multi phases or normal mode - must be same for both
func testSheetReadFull(t *testing.T, sheet xlsx.Sheet) {
cols, rows := sheet.Dimension()
require.Equal(t, 14, cols)
require.Equal(t, 28, rows)
emptyRow := []string{"", "", "", "", "", "", "", "", "", "", "", "", "", ""}
values := [][]string{
emptyRow,
{" with leading space", "", "merged rows", "", "merged cols", "merged cols", "merged cols", "", "", "", "", "", "", ""},
{"", "", "merged rows", "", "", "", "", "", "", "", "", "", "", ""},
{"", "", "merged rows", "", "merged rows+cols", "merged rows+cols", "merged rows+cols", "", "", "", "", "", "", ""},
{"with trailing space ", "", "merged rows", "", "merged rows+cols", "merged rows+cols", "merged rows+cols", "", "", "", "", "", "", ""},
emptyRow,
emptyRow,
emptyRow,
emptyRow,
{"", "", "", "1", "2", "3", "4", "5", "", "", "", "", "", ""},
{"", "", "", "6", "7", "8", "9", "10", "", "", "", "", "", ""},
{"", "", "", "11", "12", "13", "14", "15", "", "", "", "", "", ""},
{"", "", "", "16", "17", "18", "19", "20", "", "", "", "", "", ""},
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
{"", "", "", "", "", "", "", "", "", "", "", "", "", "last cell"},
}
for rowIdx, rv := range values {
require.Equal(t, rv, sheet.Row(rowIdx).Values())
}
}
//load content using single phase only - it will be without merged cells info - only first cell from range will be output
func testSheetReadLimited(t *testing.T, sheet xlsx.Sheet) {
cols, rows := sheet.Dimension()
require.Equal(t, 14, cols)
require.Equal(t, 28, rows)
emptyRow := []string{"", "", "", "", "", "", "", "", "", "", "", "", "", ""}
values := [][]string{
emptyRow,
{" with leading space", "", "merged rows", "", "merged cols", "", "", "", "", "", "", "", "", ""},
emptyRow,
{"", "", "", "", "merged rows+cols", "", "", "", "", "", "", "", "", ""},
{"with trailing space ", "", "", "", "", "", "", "", "", "", "", "", "", ""},
emptyRow,
emptyRow,
emptyRow,
emptyRow,
{"", "", "", "1", "2", "3", "4", "5", "", "", "", "", "", ""},
{"", "", "", "6", "7", "8", "9", "10", "", "", "", "", "", ""},
{"", "", "", "11", "12", "13", "14", "15", "", "", "", "", "", ""},
{"", "", "", "16", "17", "18", "19", "20", "", "", "", "", "", ""},
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
{"", "", "", "", "", "", "", "", "", "", "", "", "", "last cell"},
}
for rowIdx, rv := range values {
require.Equal(t, rv, sheet.Row(rowIdx).Values())
}
}
func TestSpreadsheet(t *testing.T) {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
panic(err)
}
defer xl.Close()
type beforeTestFn func(t *testing.T, xl *xlsx.Spreadsheet) xlsx.Sheet
type testFn func(t *testing.T, sheet xlsx.Sheet)
sheetTests := []struct {
name string
open beforeTestFn
callback testFn
}{
{
"SheetReadStream_SinglePhased", func(t *testing.T, xl *xlsx.Spreadsheet) xlsx.Sheet { return xl.Sheet(0, xlsx.SheetModeStream) }, testSheetReadLimited,
},
{
"SheetReadStream_MultiPhased", func(t *testing.T, xl *xlsx.Spreadsheet) xlsx.Sheet {
return xl.Sheet(0, xlsx.SheetModeStream, xlsx.SheetModeMultiPhase)
}, testSheetReadFull,
},
{
"SheetReadWrite", func(t *testing.T, xl *xlsx.Spreadsheet) xlsx.Sheet { return xl.Sheet(0) }, testSheetReadFull,
},
}
for _, info := range sheetTests {
t.Run(info.name, func(tt *testing.T) {
sheet := info.open(tt, xl)
info.callback(tt, sheet)
})
}
}
func TestSheetReadStream_notImplemented(t *testing.T) {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
panic(err)
}
defer xl.Close()
sheet := xl.Sheet(0, xlsx.SheetModeStream, xlsx.SheetModeMultiPhase)
defer sheet.Close()
require.Panics(t, func() { sheet.Col(0) })
require.Panics(t, func() { sheet.Cols() })
require.Panics(t, func() { sheet.InsertCol(0) })
require.Panics(t, func() { sheet.InsertRow(0) })
require.Panics(t, func() { sheet.DeleteRow(0) })
require.Panics(t, func() { sheet.DeleteCol(0) })
require.Panics(t, func() { sheet.SetDimension(100, 100) })
require.Panics(t, func() { sheet.SetActive() })
require.Panics(t, func() { sheet.SetOptions(options.New(options.Visibility(options.VisibilityVisible))) })
require.Panics(t, func() { sheet.SetName("aaa") })
}
func TestSheetReadStream_access(t *testing.T) {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
panic(err)
}
defer xl.Close()
sheet := xl.Sheet(0, xlsx.SheetModeStream, xlsx.SheetModeMultiPhase)
defer sheet.Close()
require.Equal(t, "8", sheet.CellByRef("F11").Value())
require.Equal(t, "", sheet.CellByRef("F10").Value())
require.Equal(t, "8", sheet.Cell(5, 10).Value())
require.Equal(t, []string{"", "", "", "", "", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"}, sheet.RangeByRef("D10:H13").Values())
}
func TestSheetReadStream_unsupported(t *testing.T) {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
panic(err)
}
defer xl.Close()
sheet := xl.Sheet(0, xlsx.SheetModeStream, xlsx.SheetModeMultiPhase)
defer sheet.Close()
//SetString must not work in read-only mode
require.Panics(t, func() { sheet.CellByRef("A1").SetText("a") })
//SetValueWithStyles must not work in read-only mode
require.Panics(t, func() { sheet.CellByRef("A1").SetValueWithFormat("a", "@") })
//CopyTo/CopyToRef must not work in read-only mode
require.Panics(t, func() { sheet.RangeByRef("A1:B1").CopyToRef("C2") })
}
func TestSheetReadStream_modes(t *testing.T) {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
panic(err)
}
//open as read-write and after as read-stream
_ = xl.Sheet(0)
require.Panics(t, func() { xl.Sheet(0, xlsx.SheetModeStream, xlsx.SheetModeMultiPhase) })
xl.Close()
xl, err = xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
panic(err)
}
defer xl.Close()
//open as read-stream and after as read-write
sheet := xl.Sheet(0, xlsx.SheetModeStream, xlsx.SheetModeMultiPhase)
defer sheet.Close()
}