lib_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }
  24. var invalidColumns = []struct {
  25. Name string
  26. Num int
  27. }{
  28. {Name: "", Num: -1},
  29. {Name: " ", Num: -1},
  30. {Name: "_", Num: -1},
  31. {Name: "__", Num: -1},
  32. {Name: "-1", Num: -1},
  33. {Name: "0", Num: -1},
  34. {Name: " A", Num: -1},
  35. {Name: "A ", Num: -1},
  36. {Name: "A1", Num: -1},
  37. {Name: "1A", Num: -1},
  38. {Name: " a", Num: -1},
  39. {Name: "a ", Num: -1},
  40. {Name: "a1", Num: -1},
  41. {Name: "1a", Num: -1},
  42. {Name: " _", Num: -1},
  43. {Name: "_ ", Num: -1},
  44. {Name: "_1", Num: -1},
  45. {Name: "1_", Num: -1},
  46. }
  47. var invalidCells = []string{"", "A", "AA", " A", "A ", "1A", "A1A", "A1 ", " A1", "1A1", "a-1", "A-1"}
  48. var invalidIndexes = []int{-100, -2, -1, 0}
  49. func TestColumnNameToNumber_OK(t *testing.T) {
  50. const msg = "Column %q"
  51. for _, col := range validColumns {
  52. out, err := ColumnNameToNumber(col.Name)
  53. if assert.NoErrorf(t, err, msg, col.Name) {
  54. assert.Equalf(t, col.Num, out, msg, col.Name)
  55. }
  56. }
  57. }
  58. func TestColumnNameToNumber_Error(t *testing.T) {
  59. const msg = "Column %q"
  60. for _, col := range invalidColumns {
  61. out, err := ColumnNameToNumber(col.Name)
  62. if assert.Errorf(t, err, msg, col.Name) {
  63. assert.Equalf(t, col.Num, out, msg, col.Name)
  64. }
  65. }
  66. _, err := ColumnNameToNumber("XFE")
  67. assert.EqualError(t, err, "column number exceeds maximum limit")
  68. }
  69. func TestColumnNumberToName_OK(t *testing.T) {
  70. const msg = "Column %q"
  71. for _, col := range validColumns {
  72. out, err := ColumnNumberToName(col.Num)
  73. if assert.NoErrorf(t, err, msg, col.Name) {
  74. assert.Equalf(t, strings.ToUpper(col.Name), out, msg, col.Name)
  75. }
  76. }
  77. }
  78. func TestColumnNumberToName_Error(t *testing.T) {
  79. out, err := ColumnNumberToName(-1)
  80. if assert.Error(t, err) {
  81. assert.Equal(t, "", out)
  82. }
  83. out, err = ColumnNumberToName(0)
  84. if assert.Error(t, err) {
  85. assert.Equal(t, "", out)
  86. }
  87. }
  88. func TestSplitCellName_OK(t *testing.T) {
  89. const msg = "Cell \"%s%d\""
  90. for i, col := range validColumns {
  91. row := i + 1
  92. c, r, err := SplitCellName(col.Name + strconv.Itoa(row))
  93. if assert.NoErrorf(t, err, msg, col.Name, row) {
  94. assert.Equalf(t, col.Name, c, msg, col.Name, row)
  95. assert.Equalf(t, row, r, msg, col.Name, row)
  96. }
  97. }
  98. }
  99. func TestSplitCellName_Error(t *testing.T) {
  100. const msg = "Cell %q"
  101. for _, cell := range invalidCells {
  102. c, r, err := SplitCellName(cell)
  103. if assert.Errorf(t, err, msg, cell) {
  104. assert.Equalf(t, "", c, msg, cell)
  105. assert.Equalf(t, -1, r, msg, cell)
  106. }
  107. }
  108. }
  109. func TestJoinCellName_OK(t *testing.T) {
  110. const msg = "Cell \"%s%d\""
  111. for i, col := range validColumns {
  112. row := i + 1
  113. cell, err := JoinCellName(col.Name, row)
  114. if assert.NoErrorf(t, err, msg, col.Name, row) {
  115. assert.Equalf(t, strings.ToUpper(fmt.Sprintf("%s%d", col.Name, row)), cell, msg, row)
  116. }
  117. }
  118. }
  119. func TestJoinCellName_Error(t *testing.T) {
  120. const msg = "Cell \"%s%d\""
  121. test := func(col string, row int) {
  122. cell, err := JoinCellName(col, row)
  123. if assert.Errorf(t, err, msg, col, row) {
  124. assert.Equalf(t, "", cell, msg, col, row)
  125. }
  126. }
  127. for _, col := range invalidColumns {
  128. test(col.Name, 1)
  129. for _, row := range invalidIndexes {
  130. test("A", row)
  131. test(col.Name, row)
  132. }
  133. }
  134. }
  135. func TestCellNameToCoordinates_OK(t *testing.T) {
  136. const msg = "Cell \"%s%d\""
  137. for i, col := range validColumns {
  138. row := i + 1
  139. c, r, err := CellNameToCoordinates(col.Name + strconv.Itoa(row))
  140. if assert.NoErrorf(t, err, msg, col.Name, row) {
  141. assert.Equalf(t, col.Num, c, msg, col.Name, row)
  142. assert.Equalf(t, i+1, r, msg, col.Name, row)
  143. }
  144. }
  145. }
  146. func TestCellNameToCoordinates_Error(t *testing.T) {
  147. const msg = "Cell %q"
  148. for _, cell := range invalidCells {
  149. c, r, err := CellNameToCoordinates(cell)
  150. if assert.Errorf(t, err, msg, cell) {
  151. assert.Equalf(t, -1, c, msg, cell)
  152. assert.Equalf(t, -1, r, msg, cell)
  153. }
  154. }
  155. _, _, err := CellNameToCoordinates("A1048577")
  156. assert.EqualError(t, err, "row number exceeds maximum limit")
  157. }
  158. func TestCoordinatesToCellName_OK(t *testing.T) {
  159. const msg = "Coordinates [%d, %d]"
  160. for i, col := range validColumns {
  161. row := i + 1
  162. cell, err := CoordinatesToCellName(col.Num, row)
  163. if assert.NoErrorf(t, err, msg, col.Num, row) {
  164. assert.Equalf(t, strings.ToUpper(col.Name+strconv.Itoa(row)), cell, msg, col.Num, row)
  165. }
  166. }
  167. }
  168. func TestCoordinatesToCellName_Error(t *testing.T) {
  169. const msg = "Coordinates [%d, %d]"
  170. test := func(col, row int) {
  171. cell, err := CoordinatesToCellName(col, row)
  172. if assert.Errorf(t, err, msg, col, row) {
  173. assert.Equalf(t, "", cell, msg, col, row)
  174. }
  175. }
  176. for _, col := range invalidIndexes {
  177. test(col, 1)
  178. for _, row := range invalidIndexes {
  179. test(1, row)
  180. test(col, row)
  181. }
  182. }
  183. }
  184. func TestBytesReplace(t *testing.T) {
  185. s := []byte{0x01}
  186. assert.EqualValues(t, s, bytesReplace(s, []byte{}, []byte{}, 0))
  187. }
  188. func TestStack(t *testing.T) {
  189. s := NewStack()
  190. assert.Equal(t, s.Peek(), nil)
  191. assert.Equal(t, s.Pop(), nil)
  192. }