write_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package xlsx
  2. import (
  3. . "gopkg.in/check.v1"
  4. )
  5. type WriteSuite struct{}
  6. var _ = Suite(&WriteSuite{})
  7. // Test if we can write a struct to a row
  8. func (r *RowSuite) TestWriteStruct(c *C) {
  9. var f *File
  10. f = NewFile()
  11. sheet := f.AddSheet("Test1")
  12. row := sheet.AddRow()
  13. type e struct {
  14. FirstName string
  15. Age int
  16. GPA float64
  17. LikesPHP bool
  18. }
  19. testStruct := e{
  20. "Eric",
  21. 20,
  22. 3.94,
  23. false,
  24. }
  25. row.WriteStruct(&testStruct, -1)
  26. c.Assert(row, NotNil)
  27. c0 := row.Cells[0].String()
  28. c1, e1 := row.Cells[1].Int()
  29. c2, e2 := row.Cells[2].Float()
  30. c3 := row.Cells[3].Bool()
  31. c.Assert(c0, Equals, "Eric")
  32. c.Assert(c1, Equals, 20)
  33. c.Assert(c2, Equals, 3.94)
  34. c.Assert(c3, Equals, false)
  35. c.Assert(e1, Equals, nil)
  36. c.Assert(e2, Equals, nil)
  37. }
  38. // Test if we can write a slice to a row
  39. func (r *RowSuite) TestWriteSlice(c *C) {
  40. var f *File
  41. f = NewFile()
  42. sheet := f.AddSheet("Test1")
  43. type strA []string
  44. type intA []int
  45. type floatA []float64
  46. type boolA []bool
  47. type interfaceA []interface{}
  48. s0 := strA{"Eric"}
  49. row0 := sheet.AddRow()
  50. row0.WriteSlice(&s0, -1)
  51. c.Assert(row0, NotNil)
  52. c0 := row0.Cells[0].String()
  53. c.Assert(c0, Equals, "Eric")
  54. s1 := intA{10}
  55. row1 := sheet.AddRow()
  56. row1.WriteSlice(&s1, -1)
  57. c.Assert(row1, NotNil)
  58. c1, e1 := row1.Cells[0].Int()
  59. c.Assert(e1, Equals, nil)
  60. c.Assert(c1, Equals, 10)
  61. s2 := floatA{3.94}
  62. row2 := sheet.AddRow()
  63. row2.WriteSlice(&s2, -1)
  64. c.Assert(row2, NotNil)
  65. c2, e2 := row2.Cells[0].Float()
  66. c.Assert(e2, Equals, nil)
  67. c.Assert(c2, Equals, 3.94)
  68. s3 := boolA{true}
  69. row3 := sheet.AddRow()
  70. row3.WriteSlice(&s3, -1)
  71. c.Assert(row3, NotNil)
  72. c3 := row3.Cells[0].Bool()
  73. c.Assert(c3, Equals, true)
  74. s4 := interfaceA{"Eric", 10, 3.94, true}
  75. row4 := sheet.AddRow()
  76. row4.WriteSlice(&s4, -1)
  77. c.Assert(row4, NotNil)
  78. c40 := row4.Cells[0].String()
  79. c.Assert(c40, Equals, "Eric")
  80. c41, e41 := row4.Cells[1].Int()
  81. c.Assert(e41, Equals, nil)
  82. c.Assert(c41, Equals, 10)
  83. c42, e42 := row4.Cells[2].Float()
  84. c.Assert(e42, Equals, nil)
  85. c.Assert(c42, Equals, 3.94)
  86. c43 := row4.Cells[3].Bool()
  87. c.Assert(c43, Equals, true)
  88. }