xmlStyle_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 := newXlsxStyleSheet(nil)
  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 := newXlsxStyleSheet(nil)
  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. font.B = &xlsxVal{}
  25. font.I = &xlsxVal{}
  26. font.U = &xlsxVal{}
  27. styles.Fonts.Font[0] = font
  28. expected := `<?xml version="1.0" encoding="UTF-8"?>
  29. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="10"/><name val="Andale Mono"/><b/><i/><u/></font></fonts></styleSheet>`
  30. result, err := styles.Marshal()
  31. c.Assert(err, IsNil)
  32. c.Assert(string(result), Equals, expected)
  33. }
  34. // Test we produce valid output for a style file with one fill definition.
  35. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithAFill(c *C) {
  36. styles := newXlsxStyleSheet(nil)
  37. styles.Fills = xlsxFills{}
  38. styles.Fills.Count = 1
  39. styles.Fills.Fill = make([]xlsxFill, 1)
  40. fill := xlsxFill{}
  41. patternFill := xlsxPatternFill{
  42. PatternType: "solid",
  43. FgColor: xlsxColor{RGB: "#FFFFFF"},
  44. BgColor: xlsxColor{RGB: "#000000"}}
  45. fill.PatternFill = patternFill
  46. styles.Fills.Fill[0] = fill
  47. expected := `<?xml version="1.0" encoding="UTF-8"?>
  48. <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>`
  49. result, err := styles.Marshal()
  50. c.Assert(err, IsNil)
  51. c.Assert(string(result), Equals, expected)
  52. }
  53. // Test we produce valid output for a style file with one border definition.
  54. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithABorder(c *C) {
  55. styles := newXlsxStyleSheet(nil)
  56. styles.Borders = xlsxBorders{}
  57. styles.Borders.Count = 1
  58. styles.Borders.Border = make([]xlsxBorder, 1)
  59. border := xlsxBorder{}
  60. border.Left.Style = "solid"
  61. border.Top.Style = "none"
  62. styles.Borders.Border[0] = border
  63. expected := `<?xml version="1.0" encoding="UTF-8"?>
  64. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><borders count="1"><border><left style="solid"/><top style="none"/></border></borders></styleSheet>`
  65. result, err := styles.Marshal()
  66. c.Assert(err, IsNil)
  67. c.Assert(string(result), Equals, expected)
  68. }
  69. // Test we produce valid output for a style file with one cellStyleXf definition.
  70. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellStyleXf(c *C) {
  71. styles := newXlsxStyleSheet(nil)
  72. styles.CellStyleXfs = xlsxCellStyleXfs{}
  73. styles.CellStyleXfs.Count = 1
  74. styles.CellStyleXfs.Xf = make([]xlsxXf, 1)
  75. xf := xlsxXf{}
  76. xf.ApplyAlignment = true
  77. xf.ApplyBorder = true
  78. xf.ApplyFont = true
  79. xf.ApplyFill = true
  80. xf.ApplyProtection = true
  81. xf.BorderId = 0
  82. xf.FillId = 0
  83. xf.FontId = 0
  84. xf.NumFmtId = 0
  85. xf.Alignment = xlsxAlignment{
  86. Horizontal: "left",
  87. Indent: 1,
  88. ShrinkToFit: true,
  89. TextRotation: 0,
  90. Vertical: "middle",
  91. WrapText: false}
  92. styles.CellStyleXfs.Xf[0] = xf
  93. expected := `<?xml version="1.0" encoding="UTF-8"?>
  94. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><cellStyleXfs count="1"><xf applyAlignment="1" applyBorder="1" applyFont="1" applyFill="1" applyNumberFormat="0" 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>`
  95. result, err := styles.Marshal()
  96. c.Assert(err, IsNil)
  97. c.Assert(string(result), Equals, expected)
  98. }
  99. // Test we produce valid output for a style file with one cellXf
  100. // definition.
  101. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellXf(c *C) {
  102. styles := newXlsxStyleSheet(nil)
  103. styles.CellXfs = xlsxCellXfs{}
  104. styles.CellXfs.Count = 1
  105. styles.CellXfs.Xf = make([]xlsxXf, 1)
  106. xf := xlsxXf{}
  107. xf.ApplyAlignment = true
  108. xf.ApplyBorder = true
  109. xf.ApplyFont = true
  110. xf.ApplyFill = true
  111. xf.ApplyNumberFormat = true
  112. xf.ApplyProtection = true
  113. xf.BorderId = 0
  114. xf.FillId = 0
  115. xf.FontId = 0
  116. xf.NumFmtId = 0
  117. xf.Alignment = xlsxAlignment{
  118. Horizontal: "left",
  119. Indent: 1,
  120. ShrinkToFit: true,
  121. TextRotation: 0,
  122. Vertical: "middle",
  123. WrapText: false}
  124. styles.CellXfs.Xf[0] = xf
  125. expected := `<?xml version="1.0" encoding="UTF-8"?>
  126. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><cellXfs count="1"><xf applyAlignment="1" applyBorder="1" applyFont="1" applyFill="1" applyNumberFormat="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>`
  127. result, err := styles.Marshal()
  128. c.Assert(err, IsNil)
  129. c.Assert(string(result), Equals, expected)
  130. }
  131. // Test we produce valid output for a style file with one NumFmt
  132. // definition.
  133. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithANumFmt(c *C) {
  134. styles := &xlsxStyleSheet{}
  135. styles.NumFmts = xlsxNumFmts{}
  136. styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
  137. numFmt := xlsxNumFmt{NumFmtId: 164, FormatCode: "GENERAL"}
  138. styles.addNumFmt(numFmt)
  139. expected := `<?xml version="1.0" encoding="UTF-8"?>
  140. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><numFmts count="1"><numFmt numFmtId="164" formatCode="GENERAL"/></numFmts></styleSheet>`
  141. result, err := styles.Marshal()
  142. c.Assert(err, IsNil)
  143. c.Assert(string(result), Equals, expected)
  144. }
  145. func (x *XMLStyleSuite) TestFontEquals(c *C) {
  146. fontA := xlsxFont{Sz: xlsxVal{Val: "11"},
  147. Color: xlsxColor{RGB: "FFFF0000"},
  148. Name: xlsxVal{Val: "Calibri"},
  149. Family: xlsxVal{Val: "2"},
  150. B: &xlsxVal{},
  151. I: &xlsxVal{},
  152. U: &xlsxVal{}}
  153. fontB := xlsxFont{Sz: xlsxVal{Val: "11"},
  154. Color: xlsxColor{RGB: "FFFF0000"},
  155. Name: xlsxVal{Val: "Calibri"},
  156. Family: xlsxVal{Val: "2"},
  157. B: &xlsxVal{},
  158. I: &xlsxVal{},
  159. U: &xlsxVal{}}
  160. c.Assert(fontA.Equals(fontB), Equals, true)
  161. fontB.Sz.Val = "12"
  162. c.Assert(fontA.Equals(fontB), Equals, false)
  163. fontB.Sz.Val = "11"
  164. fontB.Color.RGB = "12345678"
  165. c.Assert(fontA.Equals(fontB), Equals, false)
  166. fontB.Color.RGB = "FFFF0000"
  167. fontB.Name.Val = "Arial"
  168. c.Assert(fontA.Equals(fontB), Equals, false)
  169. fontB.Name.Val = "Calibri"
  170. fontB.Family.Val = "1"
  171. c.Assert(fontA.Equals(fontB), Equals, false)
  172. fontB.Family.Val = "2"
  173. fontB.B = nil
  174. c.Assert(fontA.Equals(fontB), Equals, false)
  175. fontB.B = &xlsxVal{}
  176. fontB.I = nil
  177. c.Assert(fontA.Equals(fontB), Equals, false)
  178. fontB.I = &xlsxVal{}
  179. fontB.U = nil
  180. c.Assert(fontA.Equals(fontB), Equals, false)
  181. fontB.U = &xlsxVal{}
  182. // For sanity
  183. c.Assert(fontA.Equals(fontB), Equals, true)
  184. }
  185. func (x *XMLStyleSuite) TestFillEquals(c *C) {
  186. fillA := xlsxFill{PatternFill: xlsxPatternFill{
  187. PatternType: "solid",
  188. FgColor: xlsxColor{RGB: "FFFF0000"},
  189. BgColor: xlsxColor{RGB: "0000FFFF"}}}
  190. fillB := xlsxFill{PatternFill: xlsxPatternFill{
  191. PatternType: "solid",
  192. FgColor: xlsxColor{RGB: "FFFF0000"},
  193. BgColor: xlsxColor{RGB: "0000FFFF"}}}
  194. c.Assert(fillA.Equals(fillB), Equals, true)
  195. fillB.PatternFill.PatternType = "gray125"
  196. c.Assert(fillA.Equals(fillB), Equals, false)
  197. fillB.PatternFill.PatternType = "solid"
  198. fillB.PatternFill.FgColor.RGB = "00FF00FF"
  199. c.Assert(fillA.Equals(fillB), Equals, false)
  200. fillB.PatternFill.FgColor.RGB = "FFFF0000"
  201. fillB.PatternFill.BgColor.RGB = "12456789"
  202. c.Assert(fillA.Equals(fillB), Equals, false)
  203. fillB.PatternFill.BgColor.RGB = "0000FFFF"
  204. // For sanity
  205. c.Assert(fillA.Equals(fillB), Equals, true)
  206. }
  207. func (x *XMLStyleSuite) TestBorderEquals(c *C) {
  208. borderA := xlsxBorder{Left: xlsxLine{Style: "none"},
  209. Right: xlsxLine{Style: "none"},
  210. Top: xlsxLine{Style: "none"},
  211. Bottom: xlsxLine{Style: "none"}}
  212. borderB := xlsxBorder{Left: xlsxLine{Style: "none"},
  213. Right: xlsxLine{Style: "none"},
  214. Top: xlsxLine{Style: "none"},
  215. Bottom: xlsxLine{Style: "none"}}
  216. c.Assert(borderA.Equals(borderB), Equals, true)
  217. borderB.Left.Style = "thin"
  218. c.Assert(borderA.Equals(borderB), Equals, false)
  219. borderB.Left.Style = "none"
  220. borderB.Right.Style = "thin"
  221. c.Assert(borderA.Equals(borderB), Equals, false)
  222. borderB.Right.Style = "none"
  223. borderB.Top.Style = "thin"
  224. c.Assert(borderA.Equals(borderB), Equals, false)
  225. borderB.Top.Style = "none"
  226. borderB.Bottom.Style = "thin"
  227. c.Assert(borderA.Equals(borderB), Equals, false)
  228. borderB.Bottom.Style = "none"
  229. // for sanity
  230. c.Assert(borderA.Equals(borderB), Equals, true)
  231. }
  232. func (x *XMLStyleSuite) TestXfEquals(c *C) {
  233. xfA := xlsxXf{
  234. ApplyAlignment: true,
  235. ApplyBorder: true,
  236. ApplyFont: true,
  237. ApplyFill: true,
  238. ApplyProtection: true,
  239. BorderId: 0,
  240. FillId: 0,
  241. FontId: 0,
  242. NumFmtId: 0}
  243. xfB := xlsxXf{
  244. ApplyAlignment: true,
  245. ApplyBorder: true,
  246. ApplyFont: true,
  247. ApplyFill: true,
  248. ApplyProtection: true,
  249. BorderId: 0,
  250. FillId: 0,
  251. FontId: 0,
  252. NumFmtId: 0}
  253. c.Assert(xfA.Equals(xfB), Equals, true)
  254. xfB.ApplyAlignment = false
  255. c.Assert(xfA.Equals(xfB), Equals, false)
  256. xfB.ApplyAlignment = true
  257. xfB.ApplyBorder = false
  258. c.Assert(xfA.Equals(xfB), Equals, false)
  259. xfB.ApplyBorder = true
  260. xfB.ApplyFont = false
  261. c.Assert(xfA.Equals(xfB), Equals, false)
  262. xfB.ApplyFont = true
  263. xfB.ApplyFill = false
  264. c.Assert(xfA.Equals(xfB), Equals, false)
  265. xfB.ApplyFill = true
  266. xfB.ApplyProtection = false
  267. c.Assert(xfA.Equals(xfB), Equals, false)
  268. xfB.ApplyProtection = true
  269. xfB.BorderId = 1
  270. c.Assert(xfA.Equals(xfB), Equals, false)
  271. xfB.BorderId = 0
  272. xfB.FillId = 1
  273. c.Assert(xfA.Equals(xfB), Equals, false)
  274. xfB.FillId = 0
  275. xfB.FontId = 1
  276. c.Assert(xfA.Equals(xfB), Equals, false)
  277. xfB.FontId = 0
  278. xfB.NumFmtId = 1
  279. c.Assert(xfA.Equals(xfB), Equals, false)
  280. xfB.NumFmtId = 0
  281. // for sanity
  282. c.Assert(xfA.Equals(xfB), Equals, true)
  283. }
  284. func (s *CellSuite) TestNewNumFmt(c *C) {
  285. styles := newXlsxStyleSheet(nil)
  286. styles.NumFmts = xlsxNumFmts{}
  287. styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
  288. c.Assert(styles.newNumFmt("0"), DeepEquals, xlsxNumFmt{1, "0"})
  289. c.Assert(styles.newNumFmt("mm-dd-yy"), DeepEquals, xlsxNumFmt{14, "mm-dd-yy"})
  290. c.Assert(styles.newNumFmt("hh:mm:ss"), DeepEquals, xlsxNumFmt{164, "hh:mm:ss"})
  291. }
  292. func (s *CellSuite) TestAddNumFmt(c *C) {
  293. styles := &xlsxStyleSheet{}
  294. styles.NumFmts = xlsxNumFmts{}
  295. styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
  296. c.Assert(styles.addNumFmt(xlsxNumFmt{1, "0"}), Equals, -1)
  297. c.Assert(styles.addNumFmt(xlsxNumFmt{14, "mm-dd-yy"}), Equals, -1)
  298. c.Assert(styles.addNumFmt(xlsxNumFmt{164, "hh:mm:ss"}), DeepEquals, 0)
  299. c.Assert(styles.addNumFmt(xlsxNumFmt{165, "yyyy/mm/dd"}), DeepEquals, 1)
  300. }