adjust_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. assert.NoError(t, f.adjustAutoFilter(&xlsxWorksheet{
  49. SheetData: xlsxSheetData{
  50. Row: []xlsxRow{{Hidden: true, R: 2}},
  51. },
  52. AutoFilter: &xlsxAutoFilter{
  53. Ref: "A1:A3",
  54. },
  55. }, rows, 1, -1))
  56. // testing adjustAutoFilter with illegal cell coordinates.
  57. assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
  58. AutoFilter: &xlsxAutoFilter{
  59. Ref: "A:B1",
  60. },
  61. }, rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  62. assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
  63. AutoFilter: &xlsxAutoFilter{
  64. Ref: "A1:B",
  65. },
  66. }, rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
  67. }
  68. func TestAdjustHelper(t *testing.T) {
  69. f := NewFile()
  70. f.NewSheet("Sheet2")
  71. f.Sheet.Store("xl/worksheets/sheet1.xml", &xlsxWorksheet{
  72. MergeCells: &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:B1"}}}})
  73. f.Sheet.Store("xl/worksheets/sheet2.xml", &xlsxWorksheet{
  74. AutoFilter: &xlsxAutoFilter{Ref: "A1:B"}})
  75. // testing adjustHelper with illegal cell coordinates.
  76. assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  77. assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
  78. // testing adjustHelper on not exists worksheet.
  79. assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN is not exist")
  80. }
  81. func TestAdjustCalcChain(t *testing.T) {
  82. f := NewFile()
  83. f.CalcChain = &xlsxCalcChain{
  84. C: []xlsxCalcChainC{
  85. {R: "B2", I: 2}, {R: "B2", I: 1},
  86. },
  87. }
  88. assert.NoError(t, f.InsertCol("Sheet1", "A"))
  89. assert.NoError(t, f.InsertRow("Sheet1", 1))
  90. f.CalcChain.C[1].R = "invalid coordinates"
  91. assert.EqualError(t, f.InsertCol("Sheet1", "A"), `cannot convert cell "invalid coordinates" to coordinates: invalid cell name "invalid coordinates"`)
  92. f.CalcChain = nil
  93. assert.NoError(t, f.InsertCol("Sheet1", "A"))
  94. }
  95. func TestCoordinatesToAreaRef(t *testing.T) {
  96. f := NewFile()
  97. _, err := f.coordinatesToAreaRef([]int{})
  98. assert.EqualError(t, err, ErrCoordinates.Error())
  99. _, err = f.coordinatesToAreaRef([]int{1, -1, 1, 1})
  100. assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
  101. _, err = f.coordinatesToAreaRef([]int{1, 1, 1, -1})
  102. assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
  103. ref, err := f.coordinatesToAreaRef([]int{1, 1, 1, 1})
  104. assert.NoError(t, err)
  105. assert.EqualValues(t, ref, "A1:A1")
  106. }
  107. func TestSortCoordinates(t *testing.T) {
  108. assert.EqualError(t, sortCoordinates(make([]int, 3)), ErrCoordinates.Error())
  109. }