lib_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package excelize
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. var validColumns = []struct {
  11. Name string
  12. Num int
  13. }{
  14. {Name: "A", Num: 1},
  15. {Name: "Z", Num: 26},
  16. {Name: "AA", Num: 26 + 1},
  17. {Name: "AK", Num: 26 + 11},
  18. {Name: "ak", Num: 26 + 11},
  19. {Name: "Ak", Num: 26 + 11},
  20. {Name: "aK", Num: 26 + 11},
  21. {Name: "AZ", Num: 26 + 26},
  22. {Name: "ZZ", Num: 26 + 26*26},
  23. {Name: "AAA", Num: 26 + 26*26 + 1},
  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. _, err := ColumnNameToNumber("XFE")
  68. assert.EqualError(t, err, "column number exceeds maximum limit")
  69. }
  70. func TestColumnNumberToName_OK(t *testing.T) {
  71. const msg = "Column %q"
  72. for _, col := range validColumns {
  73. out, err := ColumnNumberToName(col.Num)
  74. if assert.NoErrorf(t, err, msg, col.Name) {
  75. assert.Equalf(t, strings.ToUpper(col.Name), out, msg, col.Name)
  76. }
  77. }
  78. }
  79. func TestColumnNumberToName_Error(t *testing.T) {
  80. out, err := ColumnNumberToName(-1)
  81. if assert.Error(t, err) {
  82. assert.Equal(t, "", out)
  83. }
  84. out, err = ColumnNumberToName(0)
  85. if assert.Error(t, err) {
  86. assert.Equal(t, "", out)
  87. }
  88. _, err = ColumnNumberToName(TotalColumns + 1)
  89. assert.EqualError(t, err, "column number exceeds maximum limit")
  90. }
  91. func TestSplitCellName_OK(t *testing.T) {
  92. const msg = "Cell \"%s%d\""
  93. for i, col := range validColumns {
  94. row := i + 1
  95. c, r, err := SplitCellName(col.Name + strconv.Itoa(row))
  96. if assert.NoErrorf(t, err, msg, col.Name, row) {
  97. assert.Equalf(t, col.Name, c, msg, col.Name, row)
  98. assert.Equalf(t, row, r, msg, col.Name, row)
  99. }
  100. }
  101. }
  102. func TestSplitCellName_Error(t *testing.T) {
  103. const msg = "Cell %q"
  104. for _, cell := range invalidCells {
  105. c, r, err := SplitCellName(cell)
  106. if assert.Errorf(t, err, msg, cell) {
  107. assert.Equalf(t, "", c, msg, cell)
  108. assert.Equalf(t, -1, r, msg, cell)
  109. }
  110. }
  111. }
  112. func TestJoinCellName_OK(t *testing.T) {
  113. const msg = "Cell \"%s%d\""
  114. for i, col := range validColumns {
  115. row := i + 1
  116. cell, err := JoinCellName(col.Name, row)
  117. if assert.NoErrorf(t, err, msg, col.Name, row) {
  118. assert.Equalf(t, strings.ToUpper(fmt.Sprintf("%s%d", col.Name, row)), cell, msg, row)
  119. }
  120. }
  121. }
  122. func TestJoinCellName_Error(t *testing.T) {
  123. const msg = "Cell \"%s%d\""
  124. test := func(col string, row int) {
  125. cell, err := JoinCellName(col, row)
  126. if assert.Errorf(t, err, msg, col, row) {
  127. assert.Equalf(t, "", cell, msg, col, row)
  128. }
  129. }
  130. for _, col := range invalidColumns {
  131. test(col.Name, 1)
  132. for _, row := range invalidIndexes {
  133. test("A", row)
  134. test(col.Name, row)
  135. }
  136. }
  137. }
  138. func TestCellNameToCoordinates_OK(t *testing.T) {
  139. const msg = "Cell \"%s%d\""
  140. for i, col := range validColumns {
  141. row := i + 1
  142. c, r, err := CellNameToCoordinates(col.Name + strconv.Itoa(row))
  143. if assert.NoErrorf(t, err, msg, col.Name, row) {
  144. assert.Equalf(t, col.Num, c, msg, col.Name, row)
  145. assert.Equalf(t, i+1, r, msg, col.Name, row)
  146. }
  147. }
  148. }
  149. func TestCellNameToCoordinates_Error(t *testing.T) {
  150. const msg = "Cell %q"
  151. for _, cell := range invalidCells {
  152. c, r, err := CellNameToCoordinates(cell)
  153. if assert.Errorf(t, err, msg, cell) {
  154. assert.Equalf(t, -1, c, msg, cell)
  155. assert.Equalf(t, -1, r, msg, cell)
  156. }
  157. }
  158. _, _, err := CellNameToCoordinates("A1048577")
  159. assert.EqualError(t, err, "row number exceeds maximum limit")
  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. }
  179. for _, col := range invalidIndexes {
  180. test(col, 1)
  181. for _, row := range invalidIndexes {
  182. test(1, row)
  183. test(col, row)
  184. }
  185. }
  186. }
  187. func TestBytesReplace(t *testing.T) {
  188. s := []byte{0x01}
  189. assert.EqualValues(t, s, bytesReplace(s, []byte{}, []byte{}, 0))
  190. }
  191. func TestSetIgnorableNameSpace(t *testing.T) {
  192. f := NewFile()
  193. f.xmlAttr["xml_path"] = []xml.Attr{{}}
  194. f.setIgnorableNameSpace("xml_path", 0, xml.Attr{Name: xml.Name{Local: "c14"}})
  195. assert.EqualValues(t, "c14", f.xmlAttr["xml_path"][0].Value)
  196. }
  197. func TestStack(t *testing.T) {
  198. s := NewStack()
  199. assert.Equal(t, s.Peek(), nil)
  200. assert.Equal(t, s.Pop(), nil)
  201. }