lib_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. assert.Panicsf(t, func() {
  67. MustColumnNameToNumber(col.Name)
  68. }, msg, col.Name)
  69. }
  70. }
  71. func TestColumnNumberToName_OK(t *testing.T) {
  72. const msg = "Column %q"
  73. for _, col := range validColumns {
  74. out, err := ColumnNumberToName(col.Num)
  75. if assert.NoErrorf(t, err, msg, col.Name) {
  76. assert.Equalf(t, strings.ToUpper(col.Name), out, msg, col.Name)
  77. }
  78. }
  79. }
  80. func TestColumnNumberToName_Error(t *testing.T) {
  81. out, err := ColumnNumberToName(-1)
  82. if assert.Error(t, err) {
  83. assert.Equal(t, "", out)
  84. }
  85. out, err = ColumnNumberToName(0)
  86. if assert.Error(t, err) {
  87. assert.Equal(t, "", out)
  88. }
  89. }
  90. func TestSplitCellName_OK(t *testing.T) {
  91. const msg = "Cell \"%s%d\""
  92. for i, col := range validColumns {
  93. row := i + 1
  94. c, r, err := SplitCellName(col.Name + strconv.Itoa(row))
  95. if assert.NoErrorf(t, err, msg, col.Name, row) {
  96. assert.Equalf(t, col.Name, c, msg, col.Name, row)
  97. assert.Equalf(t, row, r, msg, col.Name, row)
  98. }
  99. }
  100. }
  101. func TestSplitCellName_Error(t *testing.T) {
  102. const msg = "Cell %q"
  103. for _, cell := range invalidCells {
  104. c, r, err := SplitCellName(cell)
  105. if assert.Errorf(t, err, msg, cell) {
  106. assert.Equalf(t, "", c, msg, cell)
  107. assert.Equalf(t, -1, r, msg, cell)
  108. }
  109. }
  110. }
  111. func TestJoinCellName_OK(t *testing.T) {
  112. const msg = "Cell \"%s%d\""
  113. for i, col := range validColumns {
  114. row := i + 1
  115. cell, err := JoinCellName(col.Name, row)
  116. if assert.NoErrorf(t, err, msg, col.Name, row) {
  117. assert.Equalf(t, strings.ToUpper(fmt.Sprintf("%s%d", col.Name, row)), cell, msg, row)
  118. }
  119. }
  120. }
  121. func TestJoinCellName_Error(t *testing.T) {
  122. const msg = "Cell \"%s%d\""
  123. test := func(col string, row int) {
  124. cell, err := JoinCellName(col, row)
  125. if assert.Errorf(t, err, msg, col, row) {
  126. assert.Equalf(t, "", cell, msg, col, row)
  127. }
  128. }
  129. for _, col := range invalidColumns {
  130. test(col.Name, 1)
  131. for _, row := range invalidIndexes {
  132. test("A", row)
  133. test(col.Name, row)
  134. }
  135. }
  136. }
  137. func TestCellNameToCoordinates_OK(t *testing.T) {
  138. const msg = "Cell \"%s%d\""
  139. for i, col := range validColumns {
  140. row := i + 1
  141. c, r, err := CellNameToCoordinates(col.Name + strconv.Itoa(row))
  142. if assert.NoErrorf(t, err, msg, col.Name, row) {
  143. assert.Equalf(t, col.Num, c, msg, col.Name, row)
  144. assert.Equalf(t, i+1, r, msg, col.Name, row)
  145. }
  146. }
  147. }
  148. func TestCellNameToCoordinates_Error(t *testing.T) {
  149. const msg = "Cell %q"
  150. for _, cell := range invalidCells {
  151. c, r, err := CellNameToCoordinates(cell)
  152. if assert.Errorf(t, err, msg, cell) {
  153. assert.Equalf(t, -1, c, msg, cell)
  154. assert.Equalf(t, -1, r, msg, cell)
  155. }
  156. assert.Panicsf(t, func() {
  157. MustCellNameToCoordinates(cell)
  158. }, msg, cell)
  159. }
  160. }
  161. func TestCoordinatesToCellName_OK(t *testing.T) {
  162. const msg = "Coordinates [%d, %d]"
  163. for i, col := range validColumns {
  164. row := i + 1
  165. cell, err := CoordinatesToCellName(col.Num, row)
  166. if assert.NoErrorf(t, err, msg, col.Num, row) {
  167. assert.Equalf(t, strings.ToUpper(col.Name+strconv.Itoa(row)), cell, msg, col.Num, row)
  168. }
  169. }
  170. }
  171. func TestCoordinatesToCellName_Error(t *testing.T) {
  172. const msg = "Coordinates [%d, %d]"
  173. test := func(col, row int) {
  174. cell, err := CoordinatesToCellName(col, row)
  175. if assert.Errorf(t, err, msg, col, row) {
  176. assert.Equalf(t, "", cell, msg, col, row)
  177. }
  178. assert.Panicsf(t, func() {
  179. MustCoordinatesToCellName(col, row)
  180. }, msg, col, row)
  181. }
  182. for _, col := range invalidIndexes {
  183. test(col, 1)
  184. for _, row := range invalidIndexes {
  185. test(1, row)
  186. test(col, row)
  187. }
  188. }
  189. }