xmlStyle_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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="1" applyBorder="1" applyFont="1" applyFill="1" applyProtection="1" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="1" shrinkToFit="1" textRotation="0" vertical="middle" wrapText="0"/></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="1" applyBorder="1" applyFont="1" applyFill="1" applyProtection="1" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="1" shrinkToFit="1" textRotation="0" vertical="middle" wrapText="0"/></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. }
  141. func (x *XMLStyleSuite) TestFontEquals(c *C) {
  142. fontA := xlsxFont{Sz: xlsxVal{Val: "11"},
  143. Color: xlsxColor{RGB: "FFFF0000"},
  144. Name: xlsxVal{Val: "Calibri"},
  145. Family: xlsxVal{Val: "2"}}
  146. fontB := xlsxFont{Sz: xlsxVal{Val: "11"},
  147. Color: xlsxColor{RGB: "FFFF0000"},
  148. Name: xlsxVal{Val: "Calibri"},
  149. Family: xlsxVal{Val: "2"}}
  150. c.Assert(fontA.Equals(fontB), Equals, true)
  151. fontB.Sz.Val = "12"
  152. c.Assert(fontA.Equals(fontB), Equals, false)
  153. fontB.Sz.Val = "11"
  154. fontB.Color.RGB = "12345678"
  155. c.Assert(fontA.Equals(fontB), Equals, false)
  156. fontB.Color.RGB = "FFFF0000"
  157. fontB.Name.Val = "Arial"
  158. c.Assert(fontA.Equals(fontB), Equals, false)
  159. fontB.Name.Val = "Calibri"
  160. fontB.Family.Val = "1"
  161. c.Assert(fontA.Equals(fontB), Equals, false)
  162. fontB.Family.Val = "2"
  163. // For sanity
  164. c.Assert(fontA.Equals(fontB), Equals, true)
  165. }
  166. func (x *XMLStyleSuite) TestFillEquals(c *C) {
  167. fillA := xlsxFill{PatternFill: xlsxPatternFill{
  168. PatternType: "solid",
  169. FgColor: xlsxColor{RGB: "FFFF0000"},
  170. BgColor: xlsxColor{RGB: "0000FFFF"}}}
  171. fillB := xlsxFill{PatternFill: xlsxPatternFill{
  172. PatternType: "solid",
  173. FgColor: xlsxColor{RGB: "FFFF0000"},
  174. BgColor: xlsxColor{RGB: "0000FFFF"}}}
  175. c.Assert(fillA.Equals(fillB), Equals, true)
  176. fillB.PatternFill.PatternType = "gray125"
  177. c.Assert(fillA.Equals(fillB), Equals, false)
  178. fillB.PatternFill.PatternType = "solid"
  179. fillB.PatternFill.FgColor.RGB = "00FF00FF"
  180. c.Assert(fillA.Equals(fillB), Equals, false)
  181. fillB.PatternFill.FgColor.RGB = "FFFF0000"
  182. fillB.PatternFill.BgColor.RGB = "12456789"
  183. c.Assert(fillA.Equals(fillB), Equals, false)
  184. fillB.PatternFill.BgColor.RGB = "0000FFFF"
  185. // For sanity
  186. c.Assert(fillA.Equals(fillB), Equals, true)
  187. }
  188. func (x *XMLStyleSuite) TestBorderEquals(c *C) {
  189. borderA := xlsxBorder{Left: xlsxLine{Style: "none"},
  190. Right: xlsxLine{Style: "none"},
  191. Top: xlsxLine{Style: "none"},
  192. Bottom: xlsxLine{Style: "none"}}
  193. borderB := xlsxBorder{Left: xlsxLine{Style: "none"},
  194. Right: xlsxLine{Style: "none"},
  195. Top: xlsxLine{Style: "none"},
  196. Bottom: xlsxLine{Style: "none"}}
  197. c.Assert(borderA.Equals(borderB), Equals, true)
  198. borderB.Left.Style = "thin"
  199. c.Assert(borderA.Equals(borderB), Equals, false)
  200. borderB.Left.Style = "none"
  201. borderB.Right.Style = "thin"
  202. c.Assert(borderA.Equals(borderB), Equals, false)
  203. borderB.Right.Style = "none"
  204. borderB.Top.Style = "thin"
  205. c.Assert(borderA.Equals(borderB), Equals, false)
  206. borderB.Top.Style = "none"
  207. borderB.Bottom.Style = "thin"
  208. c.Assert(borderA.Equals(borderB), Equals, false)
  209. borderB.Bottom.Style = "none"
  210. // for sanity
  211. c.Assert(borderA.Equals(borderB), Equals, true)
  212. }
  213. func (x *XMLStyleSuite) TestXfEquals(c *C) {
  214. xfA := xlsxXf{
  215. ApplyAlignment: true,
  216. ApplyBorder: true,
  217. ApplyFont: true,
  218. ApplyFill: true,
  219. ApplyProtection: true,
  220. BorderId: 0,
  221. FillId: 0,
  222. FontId: 0,
  223. NumFmtId: 0}
  224. xfB := xlsxXf{
  225. ApplyAlignment: true,
  226. ApplyBorder: true,
  227. ApplyFont: true,
  228. ApplyFill: true,
  229. ApplyProtection: true,
  230. BorderId: 0,
  231. FillId: 0,
  232. FontId: 0,
  233. NumFmtId: 0}
  234. c.Assert(xfA.Equals(xfB), Equals, true)
  235. xfB.ApplyAlignment = false
  236. c.Assert(xfA.Equals(xfB), Equals, false)
  237. xfB.ApplyAlignment = true
  238. xfB.ApplyBorder = false
  239. c.Assert(xfA.Equals(xfB), Equals, false)
  240. xfB.ApplyBorder = true
  241. xfB.ApplyFont = false
  242. c.Assert(xfA.Equals(xfB), Equals, false)
  243. xfB.ApplyFont = true
  244. xfB.ApplyFill = false
  245. c.Assert(xfA.Equals(xfB), Equals, false)
  246. xfB.ApplyFill = true
  247. xfB.ApplyProtection = false
  248. c.Assert(xfA.Equals(xfB), Equals, false)
  249. xfB.ApplyProtection = true
  250. xfB.BorderId = 1
  251. c.Assert(xfA.Equals(xfB), Equals, false)
  252. xfB.BorderId = 0
  253. xfB.FillId = 1
  254. c.Assert(xfA.Equals(xfB), Equals, false)
  255. xfB.FillId = 0
  256. xfB.FontId = 1
  257. c.Assert(xfA.Equals(xfB), Equals, false)
  258. xfB.FontId = 0
  259. xfB.NumFmtId = 1
  260. c.Assert(xfA.Equals(xfB), Equals, false)
  261. xfB.NumFmtId = 0
  262. // for sanity
  263. c.Assert(xfA.Equals(xfB), Equals, true)
  264. }