lib_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package excelize
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. var validColumns = []struct {
  10. Name string
  11. Num int
  12. }{
  13. {Name: "A", Num: 1},
  14. {Name: "Z", Num: 26},
  15. {Name: "AA", Num: 26 + 1},
  16. {Name: "AK", Num: 26 + 11},
  17. {Name: "ak", Num: 26 + 11},
  18. {Name: "Ak", Num: 26 + 11},
  19. {Name: "aK", Num: 26 + 11},
  20. {Name: "AZ", Num: 26 + 26},
  21. {Name: "ZZ", Num: 26 + 26*26},
  22. {Name: "AAA", Num: 26 + 26*26 + 1},
  23. {Name: "ZZZ", Num: 26 + 26*26 + 26*26*26},
  24. }
  25. var invalidColumns = []struct {
  26. Name string
  27. Num int
  28. }{
  29. {Name: "", Num: -1},
  30. {Name: " ", Num: -1},
  31. {Name: "_", Num: -1},
  32. {Name: "__", Num: -1},
  33. {Name: "-1", Num: -1},
  34. {Name: "0", Num: -1},
  35. {Name: " A", Num: -1},
  36. {Name: "A ", Num: -1},
  37. {Name: "A1", Num: -1},
  38. {Name: "1A", Num: -1},
  39. {Name: " a", Num: -1},
  40. {Name: "a ", Num: -1},
  41. {Name: "a1", Num: -1},
  42. {Name: "1a", Num: -1},
  43. {Name: " _", Num: -1},
  44. {Name: "_ ", Num: -1},
  45. {Name: "_1", Num: -1},
  46. {Name: "1_", Num: -1},
  47. }
  48. var invalidCells = []string{"", "A", "AA", " A", "A ", "1A", "A1A", "A1 ", " A1", "1A1", "a-1", "A-1"}
  49. var invalidIndexes = []int{-100, -2, -1, 0}
  50. func TestColumnNameToNumber_OK(t *testing.T) {
  51. const msg = "Column %q"
  52. for _, col := range validColumns {
  53. out, err := ColumnNameToNumber(col.Name)
  54. if assert.NoErrorf(t, err, msg, col.Name) {
  55. assert.Equalf(t, col.Num, out, msg, col.Name)
  56. }
  57. }
  58. }
  59. func TestColumnNameToNumber_Error(t *testing.T) {
  60. const msg = "Column %q"
  61. for _, col := range invalidColumns {
  62. out, err := ColumnNameToNumber(col.Name)
  63. if assert.Errorf(t, err, msg, col.Name) {
  64. assert.Equalf(t, col.Num, out, msg, col.Name)
  65. }
  66. }
  67. }
  68. func TestColumnNumberToName_OK(t *testing.T) {
  69. const msg = "Column %q"
  70. for _, col := range validColumns {
  71. out, err := ColumnNumberToName(col.Num)
  72. if assert.NoErrorf(t, err, msg, col.Name) {
  73. assert.Equalf(t, strings.ToUpper(col.Name), out, msg, col.Name)
  74. }
  75. }
  76. }
  77. func TestColumnNumberToName_Error(t *testing.T) {
  78. out, err := ColumnNumberToName(-1)
  79. if assert.Error(t, err) {
  80. assert.Equal(t, "", out)
  81. }
  82. out, err = ColumnNumberToName(0)
  83. if assert.Error(t, err) {
  84. assert.Equal(t, "", out)
  85. }
  86. }
  87. func TestSplitCellName_OK(t *testing.T) {
  88. const msg = "Cell \"%s%d\""
  89. for i, col := range validColumns {
  90. row := i + 1
  91. c, r, err := SplitCellName(col.Name + strconv.Itoa(row))
  92. if assert.NoErrorf(t, err, msg, col.Name, row) {
  93. assert.Equalf(t, col.Name, c, msg, col.Name, row)
  94. assert.Equalf(t, row, r, msg, col.Name, row)
  95. }
  96. }
  97. }
  98. func TestSplitCellName_Error(t *testing.T) {
  99. const msg = "Cell %q"
  100. for _, cell := range invalidCells {
  101. c, r, err := SplitCellName(cell)
  102. if assert.Errorf(t, err, msg, cell) {
  103. assert.Equalf(t, "", c, msg, cell)
  104. assert.Equalf(t, -1, r, msg, cell)
  105. }
  106. }
  107. }
  108. func TestJoinCellName_OK(t *testing.T) {
  109. const msg = "Cell \"%s%d\""
  110. for i, col := range validColumns {
  111. row := i + 1
  112. cell, err := JoinCellName(col.Name, row)
  113. if assert.NoErrorf(t, err, msg, col.Name, row) {
  114. assert.Equalf(t, strings.ToUpper(fmt.Sprintf("%s%d", col.Name, row)), cell, msg, row)
  115. }
  116. }
  117. }
  118. func TestJoinCellName_Error(t *testing.T) {
  119. const msg = "Cell \"%s%d\""
  120. test := func(col string, row int) {
  121. cell, err := JoinCellName(col, row)
  122. if assert.Errorf(t, err, msg, col, row) {
  123. assert.Equalf(t, "", cell, msg, col, row)
  124. }
  125. }
  126. for _, col := range invalidColumns {
  127. test(col.Name, 1)
  128. for _, row := range invalidIndexes {
  129. test("A", row)
  130. test(col.Name, row)
  131. }
  132. }
  133. }
  134. func TestCellNameToCoordinates_OK(t *testing.T) {
  135. const msg = "Cell \"%s%d\""
  136. for i, col := range validColumns {
  137. row := i + 1
  138. c, r, err := CellNameToCoordinates(col.Name + strconv.Itoa(row))
  139. if assert.NoErrorf(t, err, msg, col.Name, row) {
  140. assert.Equalf(t, col.Num, c, msg, col.Name, row)
  141. assert.Equalf(t, i+1, r, msg, col.Name, row)
  142. }
  143. }
  144. }
  145. func TestCellNameToCoordinates_Error(t *testing.T) {
  146. const msg = "Cell %q"
  147. for _, cell := range invalidCells {
  148. c, r, err := CellNameToCoordinates(cell)
  149. if assert.Errorf(t, err, msg, cell) {
  150. assert.Equalf(t, -1, c, msg, cell)
  151. assert.Equalf(t, -1, r, msg, cell)
  152. }
  153. }
  154. }
  155. func TestCoordinatesToCellName_OK(t *testing.T) {
  156. const msg = "Coordinates [%d, %d]"
  157. for i, col := range validColumns {
  158. row := i + 1
  159. cell, err := CoordinatesToCellName(col.Num, row)
  160. if assert.NoErrorf(t, err, msg, col.Num, row) {
  161. assert.Equalf(t, strings.ToUpper(col.Name+strconv.Itoa(row)), cell, msg, col.Num, row)
  162. }
  163. }
  164. }
  165. func TestCoordinatesToCellName_Error(t *testing.T) {
  166. const msg = "Coordinates [%d, %d]"
  167. test := func(col, row int) {
  168. cell, err := CoordinatesToCellName(col, row)
  169. if assert.Errorf(t, err, msg, col, row) {
  170. assert.Equalf(t, "", cell, msg, col, row)
  171. }
  172. }
  173. for _, col := range invalidIndexes {
  174. test(col, 1)
  175. for _, row := range invalidIndexes {
  176. test(1, row)
  177. test(col, row)
  178. }
  179. }
  180. }
  181. func TestBytesReplace(t *testing.T) {
  182. s := []byte{0x01}
  183. assert.EqualValues(t, s, bytesReplace(s, []byte{}, []byte{}, 0))
  184. }