write_test.go 3.3 KB

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