write_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. c0 := row.Cells[0].String()
  38. c1, e1 := row.Cells[1].Int()
  39. c2, e2 := row.Cells[2].Float()
  40. c3 := row.Cells[3].Bool()
  41. c4 := row.Cells[4].String()
  42. c5 := row.Cells[5].String()
  43. c.Assert(c0, Equals, "Eric")
  44. c.Assert(c1, Equals, 20)
  45. c.Assert(c2, Equals, 3.94)
  46. c.Assert(c3, Equals, false)
  47. c.Assert(c4, Equals, "Stringer")
  48. c.Assert(c5, Equals, "Pointer to Stringer")
  49. c.Assert(e1, Equals, nil)
  50. c.Assert(e2, Equals, nil)
  51. }
  52. // Test if we can write a slice to a row
  53. func (r *RowSuite) TestWriteSlice(c *C) {
  54. var f *File
  55. f = NewFile()
  56. sheet, _ := f.AddSheet("Test1")
  57. type strA []string
  58. type intA []int
  59. type floatA []float64
  60. type boolA []bool
  61. type interfaceA []interface{}
  62. type stringerA []testStringerImpl
  63. type stringerPtrA []*testStringerImpl
  64. s0 := strA{"Eric"}
  65. row0 := sheet.AddRow()
  66. row0.WriteSlice(&s0, -1)
  67. c.Assert(row0, NotNil)
  68. c0 := row0.Cells[0].String()
  69. c.Assert(c0, Equals, "Eric")
  70. s1 := intA{10}
  71. row1 := sheet.AddRow()
  72. row1.WriteSlice(&s1, -1)
  73. c.Assert(row1, NotNil)
  74. c1, e1 := row1.Cells[0].Int()
  75. c.Assert(e1, Equals, nil)
  76. c.Assert(c1, Equals, 10)
  77. s2 := floatA{3.94}
  78. row2 := sheet.AddRow()
  79. row2.WriteSlice(&s2, -1)
  80. c.Assert(row2, NotNil)
  81. c2, e2 := row2.Cells[0].Float()
  82. c.Assert(e2, Equals, nil)
  83. c.Assert(c2, Equals, 3.94)
  84. s3 := boolA{true}
  85. row3 := sheet.AddRow()
  86. row3.WriteSlice(&s3, -1)
  87. c.Assert(row3, NotNil)
  88. c3 := row3.Cells[0].Bool()
  89. c.Assert(c3, Equals, true)
  90. s4 := interfaceA{"Eric", 10, 3.94, true}
  91. row4 := sheet.AddRow()
  92. row4.WriteSlice(&s4, -1)
  93. c.Assert(row4, NotNil)
  94. c40 := row4.Cells[0].String()
  95. c.Assert(c40, Equals, "Eric")
  96. c41, e41 := row4.Cells[1].Int()
  97. c.Assert(e41, Equals, nil)
  98. c.Assert(c41, Equals, 10)
  99. c42, e42 := row4.Cells[2].Float()
  100. c.Assert(e42, Equals, nil)
  101. c.Assert(c42, Equals, 3.94)
  102. c43 := row4.Cells[3].Bool()
  103. c.Assert(c43, Equals, true)
  104. s5 := stringerA{testStringerImpl{"Stringer"}}
  105. row5 := sheet.AddRow()
  106. row5.WriteSlice(&s5, -1)
  107. c.Assert(row5, NotNil)
  108. c5 := row5.Cells[0].String()
  109. c.Assert(c5, Equals, "Stringer")
  110. s6 := stringerPtrA{&testStringerImpl{"Pointer to Stringer"}}
  111. row6 := sheet.AddRow()
  112. row6.WriteSlice(&s6, -1)
  113. c.Assert(row6, NotNil)
  114. c6 := row6.Cells[0].String()
  115. c.Assert(c6, Equals, "Pointer to Stringer")
  116. }