adjust_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  109. func TestSortCoordinates(t *testing.T) {
  110. assert.EqualError(t, sortCoordinates(make([]int, 3)), "coordinates length must be 4")
  111. }