file_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package excelize
  2. import (
  3. "bufio"
  4. "bytes"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func BenchmarkWrite(b *testing.B) {
  10. const s = "This is test data"
  11. for i := 0; i < b.N; i++ {
  12. f := NewFile()
  13. for row := 1; row <= 10000; row++ {
  14. for col := 1; col <= 20; col++ {
  15. val, err := CoordinatesToCellName(col, row)
  16. if err != nil {
  17. b.Error(err)
  18. }
  19. if err := f.SetCellValue("Sheet1", val, s); err != nil {
  20. b.Error(err)
  21. }
  22. }
  23. }
  24. // Save spreadsheet by the given path.
  25. err := f.SaveAs("./test.xlsx")
  26. if err != nil {
  27. b.Error(err)
  28. }
  29. }
  30. }
  31. func TestWriteTo(t *testing.T) {
  32. f := File{}
  33. buf := bytes.Buffer{}
  34. f.XLSX = make(map[string][]byte)
  35. f.XLSX["/d/"] = []byte("s")
  36. _, err := f.WriteTo(bufio.NewWriter(&buf))
  37. assert.EqualError(t, err, "zip: write to directory")
  38. delete(f.XLSX, "/d/")
  39. // Test file path overflow
  40. const maxUint16 = 1<<16 - 1
  41. f.XLSX[strings.Repeat("s", maxUint16+1)] = nil
  42. _, err = f.WriteTo(bufio.NewWriter(&buf))
  43. assert.EqualError(t, err, "zip: FileHeader.Name too long")
  44. }