cell_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package xlsx
  2. import (
  3. . "gopkg.in/check.v1"
  4. )
  5. type StyleSuite struct {}
  6. var _ = Suite(&StyleSuite{})
  7. func (s *StyleSuite) TestNewStyle(c *C){
  8. style := NewStyle()
  9. c.Assert(style, NotNil)
  10. }
  11. // Test that SetFont correctly updates the Font associated with a Style.
  12. func (s *StyleSuite) TestSetFont(c *C) {
  13. font := NewFont(12, "Calibra")
  14. style := Style{}
  15. style.SetFont(*font)
  16. c.Assert(style.Font.Size, Equals, 12)
  17. c.Assert(style.Font.Name, Equals, "Calibra")
  18. }
  19. type FontSuite struct {}
  20. var _ = Suite(&FontSuite{})
  21. func (s *FontSuite) TestNewFont(c *C) {
  22. font := NewFont(12, "Verdana")
  23. c.Assert(font, NotNil)
  24. c.Assert(font.Name, Equals, "Verdana")
  25. c.Assert(font.Size, Equals, 12)
  26. }
  27. type CellSuite struct {}
  28. var _ = Suite(&CellSuite{})
  29. // Test that we can set and get a Value from a Cell
  30. func (s *CellSuite) TestValueSet(c *C) {
  31. // Note, this test is fairly pointless, it serves mostly to
  32. // reinforce that this functionality is important, and should
  33. // the mechanics of this all change at some point, to remind
  34. // us not to lose this.
  35. cell := Cell{}
  36. cell.Value = "A string"
  37. c.Assert(cell.Value, Equals, "A string")
  38. }
  39. // Test that GetStyle correctly converts the xlsxStyle.Fonts.
  40. func (s *CellSuite) TestGetStyleWithFonts(c *C) {
  41. var cell *Cell
  42. var style *Style
  43. var xStyles *xlsxStyles
  44. var fonts []xlsxFont
  45. var cellXfs []xlsxXf
  46. fonts = make([]xlsxFont, 1)
  47. fonts[0] = xlsxFont{
  48. Sz: xlsxVal{Val: "10"},
  49. Name: xlsxVal{Val: "Calibra"}}
  50. cellXfs = make([]xlsxXf, 1)
  51. cellXfs[0] = xlsxXf{ApplyFont: true, FontId: 0}
  52. xStyles = &xlsxStyles{Fonts: fonts, CellXfs: cellXfs}
  53. cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
  54. style = cell.GetStyle()
  55. c.Assert(style, NotNil)
  56. c.Assert(style.Font.Size, Equals, 10)
  57. c.Assert(style.Font.Name, Equals, "Calibra")
  58. }
  59. // Test that SetStyle correctly updates the xlsxStyle.Fonts.
  60. func (s *CellSuite) TestSetStyleWithFonts(c *C) {
  61. file := NewFile()
  62. sheet := file.AddSheet("Test")
  63. row := sheet.AddRow()
  64. cell := row.AddCell()
  65. font := NewFont(12, "Calibra")
  66. style := NewStyle()
  67. style.SetFont(*font)
  68. cell.SetStyle(style)
  69. c.Assert(cell.styleIndex, Equals, 0)
  70. c.Assert(cell.styles.Fonts, HasLen, 1)
  71. xFont := cell.styles.Fonts[0]
  72. c.Assert(xFont.Sz.Val, Equals, "12")
  73. c.Assert(xFont.Name.Val, Equals, "Calibra")
  74. }
  75. // Test that GetStyle correctly converts the xlsxStyle.Fills.
  76. func (s *CellSuite) TestGetStyleWithFills(c *C) {
  77. var cell *Cell
  78. var style *Style
  79. var xStyles *xlsxStyles
  80. var fills []xlsxFill
  81. var cellXfs []xlsxXf
  82. fills = make([]xlsxFill, 1)
  83. fills[0] = xlsxFill{
  84. PatternFill: xlsxPatternFill{
  85. PatternType: "solid",
  86. FgColor: xlsxColor{RGB: "FF000000"},
  87. BgColor: xlsxColor{RGB: "00FF0000"}}}
  88. cellXfs = make([]xlsxXf, 1)
  89. cellXfs[0] = xlsxXf{ApplyFill: true, FillId: 0}
  90. xStyles = &xlsxStyles{Fills: fills, CellXfs: cellXfs}
  91. cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
  92. style = cell.GetStyle()
  93. fill := style.Fill
  94. c.Assert(fill.PatternType, Equals, "solid")
  95. c.Assert(fill.BgColor, Equals, "00FF0000")
  96. c.Assert(fill.FgColor, Equals, "FF000000")
  97. }
  98. // Test that SetStyle correctly updates xlsxStyle.Fills.
  99. func (s *CellSuite) TestSetStyleWithFills(c *C) {
  100. file := NewFile()
  101. sheet := file.AddSheet("Test")
  102. row := sheet.AddRow()
  103. cell := row.AddCell()
  104. fill := NewFill("solid", "00FF0000", "FF000000")
  105. style := NewStyle()
  106. style.SetFill(*fill)
  107. cell.SetStyle(style)
  108. c.Assert(cell.styleIndex, Equals, 0)
  109. c.Assert(cell.styles.Fills, HasLen, 1)
  110. xFill := cell.styles.Fills[0]
  111. xPatternFill := xFill.PatternFill
  112. c.Assert(xPatternFill.PatternType, Equals, "solid")
  113. c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
  114. c.Assert(xPatternFill.BgColor.RGB, Equals, "FF000000")
  115. }
  116. // Test that GetStyle correctly converts the xlsxStyle.Borders.
  117. func (s *CellSuite) TestGetStyleWithBorders(c *C) {
  118. var cell *Cell
  119. var style *Style
  120. var xStyles *xlsxStyles
  121. var borders []xlsxBorder
  122. var cellXfs []xlsxXf
  123. borders = make([]xlsxBorder, 1)
  124. borders[0] = xlsxBorder{
  125. Left: xlsxLine{Style: "thin"},
  126. Right: xlsxLine{Style: "thin"},
  127. Top: xlsxLine{Style: "thin"},
  128. Bottom: xlsxLine{Style: "thin"}}
  129. cellXfs = make([]xlsxXf, 1)
  130. cellXfs[0] = xlsxXf{ApplyBorder: true, BorderId: 0}
  131. xStyles = &xlsxStyles{Borders: borders, CellXfs: cellXfs}
  132. cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
  133. style = cell.GetStyle()
  134. border := style.Border
  135. c.Assert(border.Left, Equals, "thin")
  136. c.Assert(border.Right, Equals, "thin")
  137. c.Assert(border.Top, Equals, "thin")
  138. c.Assert(border.Bottom, Equals, "thin")
  139. }