You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.3KB

  1. package excelize
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestAdjustMergeCells(t *testing.T) {
  7. f := NewFile()
  8. // testing adjustAutoFilter with illegal cell coordinates.
  9. assert.EqualError(t, f.adjustMergeCells(&xlsxWorksheet{
  10. MergeCells: &xlsxMergeCells{
  11. Cells: []*xlsxMergeCell{
  12. {
  13. Ref: "A:B1",
  14. },
  15. },
  16. },
  17. }, rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  18. assert.EqualError(t, f.adjustMergeCells(&xlsxWorksheet{
  19. MergeCells: &xlsxMergeCells{
  20. Cells: []*xlsxMergeCell{
  21. {
  22. Ref: "A1:B",
  23. },
  24. },
  25. },
  26. }, rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
  27. assert.NoError(t, f.adjustMergeCells(&xlsxWorksheet{
  28. MergeCells: &xlsxMergeCells{
  29. Cells: []*xlsxMergeCell{
  30. {
  31. Ref: "A1:B1",
  32. },
  33. },
  34. },
  35. }, rows, 1, -1))
  36. assert.NoError(t, f.adjustMergeCells(&xlsxWorksheet{
  37. MergeCells: &xlsxMergeCells{
  38. Cells: []*xlsxMergeCell{
  39. {
  40. Ref: "A1:A2",
  41. },
  42. },
  43. },
  44. }, columns, 1, -1))
  45. }
  46. func TestAdjustAutoFilter(t *testing.T) {
  47. f := NewFile()
  48. // testing adjustAutoFilter with illegal cell coordinates.
  49. assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
  50. AutoFilter: &xlsxAutoFilter{
  51. Ref: "A:B1",
  52. },
  53. }, rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  54. assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
  55. AutoFilter: &xlsxAutoFilter{
  56. Ref: "A1:B",
  57. },
  58. }, rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
  59. }
  60. func TestAdjustHelper(t *testing.T) {
  61. f := NewFile()
  62. f.NewSheet("Sheet2")
  63. f.Sheet["xl/worksheets/sheet1.xml"] = &xlsxWorksheet{
  64. MergeCells: &xlsxMergeCells{
  65. Cells: []*xlsxMergeCell{
  66. {
  67. Ref: "A:B1",
  68. },
  69. },
  70. },
  71. }
  72. f.Sheet["xl/worksheets/sheet2.xml"] = &xlsxWorksheet{
  73. AutoFilter: &xlsxAutoFilter{
  74. Ref: "A1:B",
  75. },
  76. }
  77. // testing adjustHelper with illegal cell coordinates.
  78. assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  79. assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
  80. // testing adjustHelper on not exists worksheet.
  81. assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN is not exist")
  82. }
  83. func TestAdjustCalcChain(t *testing.T) {
  84. f := NewFile()
  85. f.CalcChain = &xlsxCalcChain{
  86. C: []xlsxCalcChainC{
  87. {R: "B2"},
  88. },
  89. }
  90. assert.NoError(t, f.InsertCol("Sheet1", "A"))
  91. assert.NoError(t, f.InsertRow("Sheet1", 1))
  92. f.CalcChain.C[0].R = "invalid coordinates"
  93. assert.EqualError(t, f.InsertCol("Sheet1", "A"), `cannot convert cell "invalid coordinates" to coordinates: invalid cell name "invalid coordinates"`)
  94. f.CalcChain = nil
  95. assert.NoError(t, f.InsertCol("Sheet1", "A"))
  96. }
  97. func TestCoordinatesToAreaRef(t *testing.T) {
  98. f := NewFile()
  99. _, err := f.coordinatesToAreaRef([]int{})
  100. assert.EqualError(t, err, "coordinates length must be 4")
  101. _, err = f.coordinatesToAreaRef([]int{1, -1, 1, 1})
  102. assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
  103. _, err = f.coordinatesToAreaRef([]int{1, 1, 1, -1})
  104. assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
  105. ref, err := f.coordinatesToAreaRef([]int{1, 1, 1, 1})
  106. assert.NoError(t, err)
  107. assert.EqualValues(t, ref, "A1:A1")
  108. }