xmlStyle_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package xlsx
  2. import (
  3. . "gopkg.in/check.v1"
  4. )
  5. type XMLStyleSuite struct{}
  6. var _ = Suite(&XMLStyleSuite{})
  7. // Test we produce valid output for an empty style file.
  8. func (x *XMLStyleSuite) TestMarshalEmptyXlsxStyleSheet(c *C) {
  9. styles := &xlsxStyleSheet{}
  10. result, err := styles.Marshal()
  11. c.Assert(err, IsNil)
  12. c.Assert(string(result), Equals, `<?xml version="1.0" encoding="UTF-8"?>
  13. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"></styleSheet>`)
  14. }
  15. // Test we produce valid output for a style file with one font definition.
  16. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithAFont(c *C) {
  17. styles := &xlsxStyleSheet{}
  18. styles.Fonts = xlsxFonts{}
  19. styles.Fonts.Count = 1
  20. styles.Fonts.Font = make([]xlsxFont, 1)
  21. font := xlsxFont{}
  22. font.Sz.Val = "10"
  23. font.Name.Val = "Andale Mono"
  24. styles.Fonts.Font[0] = font
  25. expected := `<?xml version="1.0" encoding="UTF-8"?>
  26. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="10"/><name val="Andale Mono"/></font></fonts></styleSheet>`
  27. result, err := styles.Marshal()
  28. c.Assert(err, IsNil)
  29. c.Assert(string(result), Equals, expected)
  30. }
  31. // Test we produce valid output for a style file with one fill definition.
  32. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithAFill(c *C) {
  33. styles := &xlsxStyleSheet{}
  34. styles.Fills = xlsxFills{}
  35. styles.Fills.Count = 1
  36. styles.Fills.Fill = make([]xlsxFill, 1)
  37. fill := xlsxFill{}
  38. patternFill := xlsxPatternFill{
  39. PatternType: "solid",
  40. FgColor: xlsxColor{RGB: "#FFFFFF"},
  41. BgColor: xlsxColor{RGB: "#000000"}}
  42. fill.PatternFill = patternFill
  43. styles.Fills.Fill[0] = fill
  44. expected := `<?xml version="1.0" encoding="UTF-8"?>
  45. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fills count="1"><fill><patternFill patternType="solid"><fgColor rgb="#FFFFFF"/><bgColor rgb="#000000"/></patternFill></fill></fills></styleSheet>`
  46. result, err := styles.Marshal()
  47. c.Assert(err, IsNil)
  48. c.Assert(string(result), Equals, expected)
  49. }
  50. // Test we produce valid output for a style file with one border definition.
  51. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithABorder(c *C) {
  52. styles := &xlsxStyleSheet{}
  53. styles.Borders = xlsxBorders{}
  54. styles.Borders.Count = 1
  55. styles.Borders.Border = make([]xlsxBorder, 1)
  56. border := xlsxBorder{}
  57. border.Left.Style = "solid"
  58. border.Top.Style = "none"
  59. styles.Borders.Border[0] = border
  60. expected := `<?xml version="1.0" encoding="UTF-8"?>
  61. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><borders count="1"><border><left style="solid"/><top style="none"/></border></borders></styleSheet>`
  62. result, err := styles.Marshal()
  63. c.Assert(err, IsNil)
  64. c.Assert(string(result), Equals, expected)
  65. }
  66. // Test we produce valid output for a style file with one cellStyleXf definition.
  67. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellStyleXf(c *C) {
  68. styles := &xlsxStyleSheet{}
  69. styles.CellStyleXfs = xlsxCellStyleXfs{}
  70. styles.CellStyleXfs.Count = 1
  71. styles.CellStyleXfs.Xf = make([]xlsxXf, 1)
  72. xf := xlsxXf{}
  73. xf.ApplyAlignment = true
  74. xf.ApplyBorder = true
  75. xf.ApplyFont = true
  76. xf.ApplyFill = true
  77. xf.ApplyProtection = true
  78. xf.BorderId = 0
  79. xf.FillId = 0
  80. xf.FontId = 0
  81. xf.NumFmtId = 0
  82. xf.alignment = xlsxAlignment{
  83. Horizontal: "left",
  84. Indent: 1,
  85. ShrinkToFit: true,
  86. TextRotation: 0,
  87. Vertical: "middle",
  88. WrapText: false}
  89. styles.CellStyleXfs.Xf[0] = xf
  90. expected := `<?xml version="1.0" encoding="UTF-8"?>
  91. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><cellStyleXfs count="1"><xf applyAlignment="true" applyBorder="true" applyFont="true" applyFill="true" applyProtection="true" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="1" shrinkToFit="true" textRotation="0" vertical="middle" wrapText="false"/></xf></cellStyleXfs></styleSheet>`
  92. result, err := styles.Marshal()
  93. c.Assert(err, IsNil)
  94. c.Assert(string(result), Equals, expected)
  95. }
  96. // Test we produce valid output for a style file with one cellXf
  97. // definition.
  98. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellXf(c *C) {
  99. styles := &xlsxStyleSheet{}
  100. styles.CellXfs = xlsxCellXfs{}
  101. styles.CellXfs.Count = 1
  102. styles.CellXfs.Xf = make([]xlsxXf, 1)
  103. xf := xlsxXf{}
  104. xf.ApplyAlignment = true
  105. xf.ApplyBorder = true
  106. xf.ApplyFont = true
  107. xf.ApplyFill = true
  108. xf.ApplyProtection = true
  109. xf.BorderId = 0
  110. xf.FillId = 0
  111. xf.FontId = 0
  112. xf.NumFmtId = 0
  113. xf.alignment = xlsxAlignment{
  114. Horizontal: "left",
  115. Indent: 1,
  116. ShrinkToFit: true,
  117. TextRotation: 0,
  118. Vertical: "middle",
  119. WrapText: false}
  120. styles.CellXfs.Xf[0] = xf
  121. expected := `<?xml version="1.0" encoding="UTF-8"?>
  122. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><cellXfs count="1"><xf applyAlignment="true" applyBorder="true" applyFont="true" applyFill="true" applyProtection="true" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="1" shrinkToFit="true" textRotation="0" vertical="middle" wrapText="false"/></xf></cellXfs></styleSheet>`
  123. result, err := styles.Marshal()
  124. c.Assert(err, IsNil)
  125. c.Assert(string(result), Equals, expected)
  126. }
  127. // Test we produce valid output for a style file with one NumFmt
  128. // definition.
  129. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithANumFmt(c *C) {
  130. styles := &xlsxStyleSheet{}
  131. styles.NumFmts = xlsxNumFmts{}
  132. styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
  133. numFmt := xlsxNumFmt{NumFmtId: 164, FormatCode: "GENERAL"}
  134. styles.addNumFmt(numFmt)
  135. expected := `<?xml version="1.0" encoding="UTF-8"?>
  136. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><numFmts count="1"><numFmt numFmtId="164" formatCode="GENERAL"/></numFmts></styleSheet>`
  137. result, err := styles.Marshal()
  138. c.Assert(err, IsNil)
  139. c.Assert(string(result), Equals, expected)
  140. }