2019-10-28 23:34:21 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2020-04-01 00:38:12 +08:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2021-06-22 14:06:08 +08:00
|
|
|
"os"
|
2020-04-01 00:38:12 +08:00
|
|
|
"strings"
|
2021-07-05 00:03:56 +08:00
|
|
|
"sync"
|
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) {
|
2021-06-22 14:06:08 +08:00
|
|
|
// Test WriteToBuffer err
|
|
|
|
{
|
|
|
|
f := File{}
|
|
|
|
buf := bytes.Buffer{}
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg = sync.Map{}
|
|
|
|
f.Pkg.Store("/d/", []byte("s"))
|
2021-06-22 14:06:08 +08:00
|
|
|
_, err := f.WriteTo(bufio.NewWriter(&buf))
|
|
|
|
assert.EqualError(t, err, "zip: write to directory")
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Delete("/d/")
|
2021-06-22 14:06:08 +08:00
|
|
|
}
|
2020-04-01 00:38:12 +08:00
|
|
|
// Test file path overflow
|
2021-06-22 14:06:08 +08:00
|
|
|
{
|
|
|
|
f := File{}
|
|
|
|
buf := bytes.Buffer{}
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg = sync.Map{}
|
2021-06-22 14:06:08 +08:00
|
|
|
const maxUint16 = 1<<16 - 1
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg.Store(strings.Repeat("s", maxUint16+1), nil)
|
2021-06-22 14:06:08 +08:00
|
|
|
_, err := f.WriteTo(bufio.NewWriter(&buf))
|
|
|
|
assert.EqualError(t, err, "zip: FileHeader.Name too long")
|
|
|
|
}
|
|
|
|
// Test StreamsWriter err
|
|
|
|
{
|
|
|
|
f := File{}
|
|
|
|
buf := bytes.Buffer{}
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Pkg = sync.Map{}
|
|
|
|
f.Pkg.Store("s", nil)
|
2021-06-22 14:06:08 +08:00
|
|
|
f.streams = make(map[string]*StreamWriter)
|
|
|
|
file, _ := os.Open("123")
|
|
|
|
f.streams["s"] = &StreamWriter{rawData: bufferedWriter{tmp: file}}
|
|
|
|
_, err := f.WriteTo(bufio.NewWriter(&buf))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
2020-04-01 00:38:12 +08:00
|
|
|
}
|