adjust_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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["xl/worksheets/sheet1.xml"] = &xlsxWorksheet{
  72. MergeCells: &xlsxMergeCells{
  73. Cells: []*xlsxMergeCell{
  74. {
  75. Ref: "A:B1",
  76. },
  77. },
  78. },
  79. }
  80. f.Sheet["xl/worksheets/sheet2.xml"] = &xlsxWorksheet{
  81. AutoFilter: &xlsxAutoFilter{
  82. Ref: "A1:B",
  83. },
  84. }
  85. // testing adjustHelper with illegal cell coordinates.
  86. assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  87. assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
  88. // testing adjustHelper on not exists worksheet.
  89. assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN is not exist")
  90. }
  91. func TestAdjustCalcChain(t *testing.T) {
  92. f := NewFile()
  93. f.CalcChain = &xlsxCalcChain{
  94. C: []xlsxCalcChainC{
  95. {R: "B2", I: 2}, {R: "B2", I: 1},
  96. },
  97. }
  98. assert.NoError(t, f.InsertCol("Sheet1", "A"))
  99. assert.NoError(t, f.InsertRow("Sheet1", 1))
  100. f.CalcChain.C[1].R = "invalid coordinates"
  101. assert.EqualError(t, f.InsertCol("Sheet1", "A"), `cannot convert cell "invalid coordinates" to coordinates: invalid cell name "invalid coordinates"`)
  102. f.CalcChain = nil
  103. assert.NoError(t, f.InsertCol("Sheet1", "A"))
  104. }
  105. func TestCoordinatesToAreaRef(t *testing.T) {
  106. f := NewFile()
  107. _, err := f.coordinatesToAreaRef([]int{})
  108. assert.EqualError(t, err, "coordinates length must be 4")
  109. _, err = f.coordinatesToAreaRef([]int{1, -1, 1, 1})
  110. assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
  111. _, err = f.coordinatesToAreaRef([]int{1, 1, 1, -1})
  112. assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
  113. ref, err := f.coordinatesToAreaRef([]int{1, 1, 1, 1})
  114. assert.NoError(t, err)
  115. assert.EqualValues(t, ref, "A1:A1")
  116. }
  117. func TestSortCoordinates(t *testing.T) {
  118. assert.EqualError(t, sortCoordinates(make([]int, 3)), "coordinates length must be 4")
  119. }