file_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package excelize
  2. import (
  3. "bufio"
  4. "bytes"
  5. "os"
  6. "strings"
  7. "sync"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func BenchmarkWrite(b *testing.B) {
  12. const s = "This is test data"
  13. for i := 0; i < b.N; i++ {
  14. f := NewFile()
  15. for row := 1; row <= 10000; row++ {
  16. for col := 1; col <= 20; col++ {
  17. val, err := CoordinatesToCellName(col, row)
  18. if err != nil {
  19. b.Error(err)
  20. }
  21. if err := f.SetCellValue("Sheet1", val, s); err != nil {
  22. b.Error(err)
  23. }
  24. }
  25. }
  26. // Save spreadsheet by the given path.
  27. err := f.SaveAs("./test.xlsx")
  28. if err != nil {
  29. b.Error(err)
  30. }
  31. }
  32. }
  33. func TestWriteTo(t *testing.T) {
  34. // Test WriteToBuffer err
  35. {
  36. f := File{}
  37. buf := bytes.Buffer{}
  38. f.Pkg = sync.Map{}
  39. f.Pkg.Store("/d/", []byte("s"))
  40. _, err := f.WriteTo(bufio.NewWriter(&buf))
  41. assert.EqualError(t, err, "zip: write to directory")
  42. f.Pkg.Delete("/d/")
  43. }
  44. // Test file path overflow
  45. {
  46. f := File{}
  47. buf := bytes.Buffer{}
  48. f.Pkg = sync.Map{}
  49. const maxUint16 = 1<<16 - 1
  50. f.Pkg.Store(strings.Repeat("s", maxUint16+1), nil)
  51. _, err := f.WriteTo(bufio.NewWriter(&buf))
  52. assert.EqualError(t, err, "zip: FileHeader.Name too long")
  53. }
  54. // Test StreamsWriter err
  55. {
  56. f := File{}
  57. buf := bytes.Buffer{}
  58. f.Pkg = sync.Map{}
  59. f.Pkg.Store("s", nil)
  60. f.streams = make(map[string]*StreamWriter)
  61. file, _ := os.Open("123")
  62. f.streams["s"] = &StreamWriter{rawData: bufferedWriter{tmp: file}}
  63. _, err := f.WriteTo(bufio.NewWriter(&buf))
  64. assert.Nil(t, err)
  65. }
  66. }