write_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package xlsx
  2. import (
  3. "math"
  4. "time"
  5. . "gopkg.in/check.v1"
  6. )
  7. type WriteSuite struct{}
  8. var _ = Suite(&WriteSuite{})
  9. type testStringerImpl struct {
  10. Value string
  11. }
  12. func (this testStringerImpl) String() string {
  13. return this.Value
  14. }
  15. // Test if we can write a struct to a row
  16. func (r *RowSuite) TestWriteStruct(c *C) {
  17. var f *File
  18. f = NewFile()
  19. sheet, _ := f.AddSheet("Test1")
  20. row := sheet.AddRow()
  21. type e struct {
  22. FirstName string
  23. Age int
  24. GPA float64
  25. LikesPHP bool
  26. Stringer testStringerImpl
  27. StringerPtr *testStringerImpl
  28. Time time.Time
  29. }
  30. testStruct := e{
  31. "Eric",
  32. 20,
  33. 3.94,
  34. false,
  35. testStringerImpl{"Stringer"},
  36. &testStringerImpl{"Pointer to Stringer"},
  37. time.Unix(0, 0),
  38. }
  39. cnt := row.WriteStruct(&testStruct, -1)
  40. c.Assert(cnt, Equals, 7)
  41. c.Assert(row, NotNil)
  42. var (
  43. c0, c4, c5 string
  44. err error
  45. c6 float64
  46. )
  47. if c0, err = row.Cells[0].FormattedValue(); err != nil {
  48. c.Error(err)
  49. }
  50. c1, e1 := row.Cells[1].Int()
  51. c2, e2 := row.Cells[2].Float()
  52. c3 := row.Cells[3].Bool()
  53. if c4, err = row.Cells[4].FormattedValue(); err != nil {
  54. c.Error(err)
  55. }
  56. if c5, err = row.Cells[5].FormattedValue(); err != nil {
  57. c.Error(err)
  58. }
  59. if c6, err = row.Cells[6].Float(); err != nil {
  60. c.Error(err)
  61. }
  62. c.Assert(c0, Equals, "Eric")
  63. c.Assert(c1, Equals, 20)
  64. c.Assert(c2, Equals, 3.94)
  65. c.Assert(c3, Equals, false)
  66. c.Assert(c4, Equals, "Stringer")
  67. c.Assert(c5, Equals, "Pointer to Stringer")
  68. c.Assert(math.Floor(c6), Equals, 25569.0)
  69. c.Assert(e1, Equals, nil)
  70. c.Assert(e2, Equals, nil)
  71. }
  72. // Test if we can write a slice to a row
  73. func (r *RowSuite) TestWriteSlice(c *C) {
  74. var f *File
  75. f = NewFile()
  76. sheet, _ := f.AddSheet("Test1")
  77. type strA []string
  78. type intA []int
  79. type floatA []float64
  80. type boolA []bool
  81. type interfaceA []interface{}
  82. type stringerA []testStringerImpl
  83. type stringerPtrA []*testStringerImpl
  84. s0 := strA{"Eric"}
  85. row0 := sheet.AddRow()
  86. row0.WriteSlice(&s0, -1)
  87. c.Assert(row0, NotNil)
  88. if val, err := row0.Cells[0].FormattedValue(); err != nil {
  89. c.Error(err)
  90. } else {
  91. c.Assert(val, Equals, "Eric")
  92. }
  93. s1 := intA{10}
  94. row1 := sheet.AddRow()
  95. row1.WriteSlice(&s1, -1)
  96. c.Assert(row1, NotNil)
  97. c1, e1 := row1.Cells[0].Int()
  98. c.Assert(e1, Equals, nil)
  99. c.Assert(c1, Equals, 10)
  100. s2 := floatA{3.94}
  101. row2 := sheet.AddRow()
  102. row2.WriteSlice(&s2, -1)
  103. c.Assert(row2, NotNil)
  104. c2, e2 := row2.Cells[0].Float()
  105. c.Assert(e2, Equals, nil)
  106. c.Assert(c2, Equals, 3.94)
  107. s3 := boolA{true}
  108. row3 := sheet.AddRow()
  109. row3.WriteSlice(&s3, -1)
  110. c.Assert(row3, NotNil)
  111. c3 := row3.Cells[0].Bool()
  112. c.Assert(c3, Equals, true)
  113. s4 := interfaceA{"Eric", 10, 3.94, true, time.Unix(0, 0)}
  114. row4 := sheet.AddRow()
  115. row4.WriteSlice(&s4, -1)
  116. c.Assert(row4, NotNil)
  117. if val, err := row4.Cells[0].FormattedValue(); err != nil {
  118. c.Error(err)
  119. } else {
  120. c.Assert(val, Equals, "Eric")
  121. }
  122. c41, e41 := row4.Cells[1].Int()
  123. c.Assert(e41, Equals, nil)
  124. c.Assert(c41, Equals, 10)
  125. c42, e42 := row4.Cells[2].Float()
  126. c.Assert(e42, Equals, nil)
  127. c.Assert(c42, Equals, 3.94)
  128. c43 := row4.Cells[3].Bool()
  129. c.Assert(c43, Equals, true)
  130. c44, e44 := row4.Cells[4].Float()
  131. c.Assert(e44, Equals, nil)
  132. c.Assert(math.Floor(c44), Equals, 25569.0)
  133. s5 := stringerA{testStringerImpl{"Stringer"}}
  134. row5 := sheet.AddRow()
  135. row5.WriteSlice(&s5, -1)
  136. c.Assert(row5, NotNil)
  137. if val, err := row5.Cells[0].FormattedValue(); err != nil {
  138. c.Error(err)
  139. } else {
  140. c.Assert(val, Equals, "Stringer")
  141. }
  142. s6 := stringerPtrA{&testStringerImpl{"Pointer to Stringer"}}
  143. row6 := sheet.AddRow()
  144. row6.WriteSlice(&s6, -1)
  145. c.Assert(row6, NotNil)
  146. if val, err := row6.Cells[0].FormattedValue(); err != nil {
  147. c.Error(err)
  148. } else {
  149. c.Assert(val, Equals, "Pointer to Stringer")
  150. }
  151. s7 := "expects -1 on non pointer to slice"
  152. row7 := sheet.AddRow()
  153. c.Assert(row7, NotNil)
  154. s7_ret := row7.WriteSlice(s7, -1)
  155. c.Assert(s7_ret, Equals, -1)
  156. s7_ret = row7.WriteSlice(&s7, -1)
  157. c.Assert(s7_ret, Equals, -1)
  158. s7_ret = row7.WriteSlice([]string{s7}, -1)
  159. c.Assert(s7_ret, Equals, -1)
  160. }