You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.1KB

  1. package excelize
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestStyleFill(t *testing.T) {
  7. cases := []struct {
  8. label string
  9. format string
  10. expectFill bool
  11. }{{
  12. label: "no_fill",
  13. format: `{"alignment":{"wrap_text":true}}`,
  14. expectFill: false,
  15. }, {
  16. label: "fill",
  17. format: `{"fill":{"type":"pattern","pattern":1,"color":["#000000"]}}`,
  18. expectFill: true,
  19. }}
  20. for _, testCase := range cases {
  21. xl := NewFile()
  22. styleID, err := xl.NewStyle(testCase.format)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. styles := xl.stylesReader()
  27. style := styles.CellXfs.Xf[styleID]
  28. if testCase.expectFill {
  29. assert.NotEqual(t, style.FillID, 0, testCase.label)
  30. } else {
  31. assert.Equal(t, style.FillID, 0, testCase.label)
  32. }
  33. }
  34. }
  35. func TestSetConditionalFormat(t *testing.T) {
  36. cases := []struct {
  37. label string
  38. format string
  39. rules []*xlsxCfRule
  40. }{{
  41. label: "3_color_scale",
  42. format: `[{
  43. "type":"3_color_scale",
  44. "criteria":"=",
  45. "min_type":"num",
  46. "mid_type":"num",
  47. "max_type":"num",
  48. "min_value": "-10",
  49. "mid_value": "0",
  50. "max_value": "10",
  51. "min_color":"ff0000",
  52. "mid_color":"00ff00",
  53. "max_color":"0000ff"
  54. }]`,
  55. rules: []*xlsxCfRule{{
  56. Priority: 1,
  57. Type: "colorScale",
  58. ColorScale: &xlsxColorScale{
  59. Cfvo: []*xlsxCfvo{{
  60. Type: "num",
  61. Val: "-10",
  62. }, {
  63. Type: "num",
  64. Val: "0",
  65. }, {
  66. Type: "num",
  67. Val: "10",
  68. }},
  69. Color: []*xlsxColor{{
  70. RGB: "FFFF0000",
  71. }, {
  72. RGB: "FF00FF00",
  73. }, {
  74. RGB: "FF0000FF",
  75. }},
  76. },
  77. }},
  78. }, {
  79. label: "3_color_scale default min/mid/max",
  80. format: `[{
  81. "type":"3_color_scale",
  82. "criteria":"=",
  83. "min_type":"num",
  84. "mid_type":"num",
  85. "max_type":"num",
  86. "min_color":"ff0000",
  87. "mid_color":"00ff00",
  88. "max_color":"0000ff"
  89. }]`,
  90. rules: []*xlsxCfRule{{
  91. Priority: 1,
  92. Type: "colorScale",
  93. ColorScale: &xlsxColorScale{
  94. Cfvo: []*xlsxCfvo{{
  95. Type: "num",
  96. Val: "0",
  97. }, {
  98. Type: "num",
  99. Val: "50",
  100. }, {
  101. Type: "num",
  102. Val: "0",
  103. }},
  104. Color: []*xlsxColor{{
  105. RGB: "FFFF0000",
  106. }, {
  107. RGB: "FF00FF00",
  108. }, {
  109. RGB: "FF0000FF",
  110. }},
  111. },
  112. }},
  113. }, {
  114. label: "2_color_scale default min/max",
  115. format: `[{
  116. "type":"2_color_scale",
  117. "criteria":"=",
  118. "min_type":"num",
  119. "max_type":"num",
  120. "min_color":"ff0000",
  121. "max_color":"0000ff"
  122. }]`,
  123. rules: []*xlsxCfRule{{
  124. Priority: 1,
  125. Type: "colorScale",
  126. ColorScale: &xlsxColorScale{
  127. Cfvo: []*xlsxCfvo{{
  128. Type: "num",
  129. Val: "0",
  130. }, {
  131. Type: "num",
  132. Val: "0",
  133. }},
  134. Color: []*xlsxColor{{
  135. RGB: "FFFF0000",
  136. }, {
  137. RGB: "FF0000FF",
  138. }},
  139. },
  140. }},
  141. }}
  142. for _, testCase := range cases {
  143. xl := NewFile()
  144. const sheet = "Sheet1"
  145. const cellRange = "A1:A1"
  146. err := xl.SetConditionalFormat(sheet, cellRange, testCase.format)
  147. if err != nil {
  148. t.Fatalf("%s", err)
  149. }
  150. xlsx, err := xl.workSheetReader(sheet)
  151. assert.NoError(t, err)
  152. cf := xlsx.ConditionalFormatting
  153. assert.Len(t, cf, 1, testCase.label)
  154. assert.Len(t, cf[0].CfRule, 1, testCase.label)
  155. assert.Equal(t, cellRange, cf[0].SQRef, testCase.label)
  156. assert.EqualValues(t, testCase.rules, cf[0].CfRule, testCase.label)
  157. }
  158. }
  159. func TestNewStyle(t *testing.T) {
  160. f := NewFile()
  161. styleID, err := f.NewStyle(`{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777"}}`)
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. styles := f.stylesReader()
  166. fontID := styles.CellXfs.Xf[styleID].FontID
  167. font := styles.Fonts.Font[fontID]
  168. assert.Contains(t, font.Name.Val, "Times New Roman", "Stored font should contain font name")
  169. assert.Equal(t, 2, styles.CellXfs.Count, "Should have 2 styles")
  170. }
  171. func TestGetDefaultFont(t *testing.T) {
  172. f := NewFile()
  173. s := f.GetDefaultFont()
  174. assert.Equal(t, s, "Calibri", "Default font should be Calibri")
  175. }
  176. func TestSetDefaultFont(t *testing.T) {
  177. f := NewFile()
  178. f.SetDefaultFont("Ariel")
  179. styles := f.stylesReader()
  180. s := f.GetDefaultFont()
  181. assert.Equal(t, s, "Ariel", "Default font should change to Ariel")
  182. assert.Equal(t, *styles.CellStyles.CellStyle[0].CustomBuiltIn, true)
  183. }