table_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package excelize
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestAddTable(t *testing.T) {
  9. f, err := prepareTestBook1()
  10. if !assert.NoError(t, err) {
  11. t.FailNow()
  12. }
  13. err = f.AddTable("Sheet1", "B26", "A21", `{}`)
  14. if !assert.NoError(t, err) {
  15. t.FailNow()
  16. }
  17. err = f.AddTable("Sheet2", "A2", "B5", `{"table_name":"table","table_style":"TableStyleMedium2", "show_first_column":true,"show_last_column":true,"show_row_stripes":false,"show_column_stripes":true}`)
  18. if !assert.NoError(t, err) {
  19. t.FailNow()
  20. }
  21. err = f.AddTable("Sheet2", "F1", "F1", `{"table_style":"TableStyleMedium8"}`)
  22. if !assert.NoError(t, err) {
  23. t.FailNow()
  24. }
  25. // Test add table in not exist worksheet.
  26. assert.EqualError(t, f.AddTable("SheetN", "B26", "A21", `{}`), "sheet SheetN is not exist")
  27. // Test add table with illegal formatset.
  28. assert.EqualError(t, f.AddTable("Sheet1", "B26", "A21", `{x}`), "invalid character 'x' looking for beginning of object key string")
  29. // Test add table with illegal cell coordinates.
  30. assert.EqualError(t, f.AddTable("Sheet1", "A", "B1", `{}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  31. assert.EqualError(t, f.AddTable("Sheet1", "A1", "B", `{}`), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
  32. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddTable.xlsx")))
  33. // Test addTable with illegal cell coordinates.
  34. f = NewFile()
  35. assert.EqualError(t, f.addTable("sheet1", "", 0, 0, 0, 0, 0, nil), "invalid cell coordinates [0, 0]")
  36. assert.EqualError(t, f.addTable("sheet1", "", 1, 1, 0, 0, 0, nil), "invalid cell coordinates [0, 0]")
  37. }
  38. func TestAutoFilter(t *testing.T) {
  39. outFile := filepath.Join("test", "TestAutoFilter%d.xlsx")
  40. f, err := prepareTestBook1()
  41. if !assert.NoError(t, err) {
  42. t.FailNow()
  43. }
  44. formats := []string{
  45. ``,
  46. `{"column":"B","expression":"x != blanks"}`,
  47. `{"column":"B","expression":"x == blanks"}`,
  48. `{"column":"B","expression":"x != nonblanks"}`,
  49. `{"column":"B","expression":"x == nonblanks"}`,
  50. `{"column":"B","expression":"x <= 1 and x >= 2"}`,
  51. `{"column":"B","expression":"x == 1 or x == 2"}`,
  52. `{"column":"B","expression":"x == 1 or x == 2*"}`,
  53. }
  54. for i, format := range formats {
  55. t.Run(fmt.Sprintf("Expression%d", i+1), func(t *testing.T) {
  56. err = f.AutoFilter("Sheet1", "D4", "B1", format)
  57. assert.NoError(t, err)
  58. assert.NoError(t, f.SaveAs(fmt.Sprintf(outFile, i+1)))
  59. })
  60. }
  61. // testing AutoFilter with illegal cell coordinates.
  62. assert.EqualError(t, f.AutoFilter("Sheet1", "A", "B1", ""), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  63. assert.EqualError(t, f.AutoFilter("Sheet1", "A1", "B", ""), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
  64. }
  65. func TestAutoFilterError(t *testing.T) {
  66. outFile := filepath.Join("test", "TestAutoFilterError%d.xlsx")
  67. f, err := prepareTestBook1()
  68. if !assert.NoError(t, err) {
  69. t.FailNow()
  70. }
  71. formats := []string{
  72. `{"column":"B","expression":"x <= 1 and x >= blanks"}`,
  73. `{"column":"B","expression":"x -- y or x == *2*"}`,
  74. `{"column":"B","expression":"x != y or x ? *2"}`,
  75. `{"column":"B","expression":"x -- y o r x == *2"}`,
  76. `{"column":"B","expression":"x -- y"}`,
  77. `{"column":"A","expression":"x -- y"}`,
  78. }
  79. for i, format := range formats {
  80. t.Run(fmt.Sprintf("Expression%d", i+1), func(t *testing.T) {
  81. err = f.AutoFilter("Sheet2", "D4", "B1", format)
  82. if assert.Error(t, err) {
  83. assert.NoError(t, f.SaveAs(fmt.Sprintf(outFile, i+1)))
  84. }
  85. })
  86. }
  87. assert.EqualError(t, f.autoFilter("SheetN", "A1", 1, 1, &formatAutoFilter{
  88. Column: "A",
  89. Expression: "",
  90. }), "sheet SheetN is not exist")
  91. assert.EqualError(t, f.autoFilter("Sheet1", "A1", 1, 1, &formatAutoFilter{
  92. Column: "-",
  93. Expression: "-",
  94. }), `invalid column name "-"`)
  95. assert.EqualError(t, f.autoFilter("Sheet1", "A1", 1, 100, &formatAutoFilter{
  96. Column: "A",
  97. Expression: "-",
  98. }), `incorrect index of column 'A'`)
  99. assert.EqualError(t, f.autoFilter("Sheet1", "A1", 1, 1, &formatAutoFilter{
  100. Column: "A",
  101. Expression: "-",
  102. }), `incorrect number of tokens in criteria '-'`)
  103. }
  104. func TestParseFilterTokens(t *testing.T) {
  105. f := NewFile()
  106. // Test with unknown operator.
  107. _, _, err := f.parseFilterTokens("", []string{"", "!"})
  108. assert.EqualError(t, err, "unknown operator: !")
  109. // Test invalid operator in context.
  110. _, _, err = f.parseFilterTokens("", []string{"", "<", "x != blanks"})
  111. assert.EqualError(t, err, "the operator '<' in expression '' is not valid in relation to Blanks/NonBlanks'")
  112. }