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"
|
2022-11-13 00:40:04 +08:00
|
|
|
"path/filepath"
|
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"
|
2021-09-18 23:20:24 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
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
|
|
|
|
{
|
2021-08-15 00:06:40 +08:00
|
|
|
f, buf := File{Pkg: sync.Map{}}, bytes.Buffer{}
|
2021-07-05 00:03:56 +08:00
|
|
|
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
|
|
|
{
|
2021-08-15 00:06:40 +08:00
|
|
|
f, buf := File{Pkg: sync.Map{}}, bytes.Buffer{}
|
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
|
|
|
|
{
|
2021-08-15 00:06:40 +08:00
|
|
|
f, buf := File{Pkg: sync.Map{}}, bytes.Buffer{}
|
2021-07-05 00:03:56 +08:00
|
|
|
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)
|
|
|
|
}
|
2021-12-01 00:10:31 +08:00
|
|
|
// Test write with temporary file
|
|
|
|
{
|
|
|
|
f, buf := File{tempFiles: sync.Map{}}, bytes.Buffer{}
|
|
|
|
const maxUint16 = 1<<16 - 1
|
|
|
|
f.tempFiles.Store("s", "")
|
|
|
|
f.tempFiles.Store(strings.Repeat("s", maxUint16+1), "")
|
|
|
|
_, err := f.WriteTo(bufio.NewWriter(&buf))
|
|
|
|
assert.EqualError(t, err, "zip: FileHeader.Name too long")
|
|
|
|
}
|
2022-09-08 22:20:21 +08:00
|
|
|
// Test write with unsupported workbook file format
|
|
|
|
{
|
|
|
|
f, buf := File{Pkg: sync.Map{}}, bytes.Buffer{}
|
|
|
|
f.Pkg.Store("/d", []byte("s"))
|
|
|
|
f.Path = "Book1.xls"
|
|
|
|
_, err := f.WriteTo(bufio.NewWriter(&buf))
|
|
|
|
assert.EqualError(t, err, ErrWorkbookFileFormat.Error())
|
|
|
|
}
|
2022-11-13 00:40:04 +08:00
|
|
|
// Test write with unsupported charset content types.
|
|
|
|
{
|
|
|
|
f, buf := NewFile(), bytes.Buffer{}
|
|
|
|
f.ContentTypes, f.Path = nil, filepath.Join("test", "TestWriteTo.xlsx")
|
|
|
|
f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)
|
|
|
|
_, err := f.WriteTo(bufio.NewWriter(&buf))
|
|
|
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
|
|
|
}
|
2020-04-01 00:38:12 +08:00
|
|
|
}
|
2021-09-18 23:20:24 +08:00
|
|
|
|
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
f.tempFiles.Store("/d/", "/d/")
|
|
|
|
require.Error(t, f.Close())
|
|
|
|
}
|