2019-10-28 23:34:21 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2020-04-01 00:38:12 +08:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
2019-10-28 23:34:21 +08:00
|
|
|
"testing"
|
2020-04-01 00:38:12 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-28 23:34:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkWrite(b *testing.B) {
|
|
|
|
const s = "This is test data"
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
f := NewFile()
|
|
|
|
for row := 1; row <= 10000; row++ {
|
|
|
|
for col := 1; col <= 20; col++ {
|
|
|
|
val, err := CoordinatesToCellName(col, row)
|
|
|
|
if err != nil {
|
2020-01-03 23:57:25 +08:00
|
|
|
b.Error(err)
|
2019-10-28 23:34:21 +08:00
|
|
|
}
|
2020-08-16 11:48:11 +08:00
|
|
|
if err := f.SetCellValue("Sheet1", val, s); err != nil {
|
2020-01-03 23:57:25 +08:00
|
|
|
b.Error(err)
|
2019-12-24 01:09:28 +08:00
|
|
|
}
|
2019-10-28 23:34:21 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-10 23:48:09 +08:00
|
|
|
// Save spreadsheet by the given path.
|
2019-10-28 23:34:21 +08:00
|
|
|
err := f.SaveAs("./test.xlsx")
|
|
|
|
if err != nil {
|
2020-01-03 23:57:25 +08:00
|
|
|
b.Error(err)
|
2019-10-28 23:34:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 00:38:12 +08:00
|
|
|
|
|
|
|
func TestWriteTo(t *testing.T) {
|
|
|
|
f := File{}
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
f.XLSX = make(map[string][]byte, 0)
|
|
|
|
f.XLSX["/d/"] = []byte("s")
|
|
|
|
_, err := f.WriteTo(bufio.NewWriter(&buf))
|
|
|
|
assert.EqualError(t, err, "zip: write to directory")
|
|
|
|
delete(f.XLSX, "/d/")
|
|
|
|
// Test file path overflow
|
|
|
|
const maxUint16 = 1<<16 - 1
|
|
|
|
f.XLSX[strings.Repeat("s", maxUint16+1)] = nil
|
|
|
|
_, err = f.WriteTo(bufio.NewWriter(&buf))
|
|
|
|
assert.EqualError(t, err, "zip: FileHeader.Name too long")
|
|
|
|
}
|