excelize/file_test.go

70 lines
1.5 KiB
Go
Raw Normal View History

package excelize
import (
2020-04-01 00:38:12 +08:00
"bufio"
"bytes"
"os"
2020-04-01 00:38:12 +08:00
"strings"
"testing"
2020-04-01 00:38:12 +08:00
"github.com/stretchr/testify/assert"
)
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 {
b.Error(err)
}
2020-08-16 11:48:11 +08:00
if err := f.SetCellValue("Sheet1", val, s); err != nil {
b.Error(err)
}
}
}
// Save spreadsheet by the given path.
err := f.SaveAs("./test.xlsx")
if err != nil {
b.Error(err)
}
}
}
2020-04-01 00:38:12 +08:00
func TestWriteTo(t *testing.T) {
// Test WriteToBuffer err
{
f := File{}
buf := bytes.Buffer{}
f.XLSX = make(map[string][]byte)
f.XLSX["/d/"] = []byte("s")
_, err := f.WriteTo(bufio.NewWriter(&buf))
assert.EqualError(t, err, "zip: write to directory")
delete(f.XLSX, "/d/")
}
2020-04-01 00:38:12 +08:00
// Test file path overflow
{
f := File{}
buf := bytes.Buffer{}
f.XLSX = make(map[string][]byte)
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")
}
// Test StreamsWriter err
{
f := File{}
buf := bytes.Buffer{}
f.XLSX = make(map[string][]byte)
f.XLSX["s"] = nil
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
}