|
4 | 4 | """
|
5 | 5 |
|
6 | 6 | import pytest
|
| 7 | +import pandas.util.testing as tm |
7 | 8 |
|
| 9 | +from warnings import catch_warnings |
8 | 10 | from pandas.io.formats.excel import CSSToExcelConverter
|
9 | 11 |
|
10 | 12 |
|
@@ -212,3 +214,50 @@ def test_css_to_excel_multiple():
|
212 | 214 | def test_css_to_excel_inherited(css, inherited, expected):
|
213 | 215 | convert = CSSToExcelConverter(inherited)
|
214 | 216 | assert expected == convert(css)
|
| 217 | + |
| 218 | + |
| 219 | +@pytest.mark.parametrize("input_color,output_color", ( |
| 220 | + [(name, rgb) for name, rgb in CSSToExcelConverter.NAMED_COLORS.items()] + |
| 221 | + [("#" + rgb, rgb) for rgb in CSSToExcelConverter.NAMED_COLORS.values()] + |
| 222 | + [("#F0F", "FF00FF"), ("#ABC", "AABBCC")] + |
| 223 | + [(None, None), ("not-a-color", None)]) |
| 224 | +) |
| 225 | +def test_css_to_excel_colors(input_color, output_color): |
| 226 | + # see gh-18392 |
| 227 | + css = ("border-top-color: {color}; " |
| 228 | + "border-right-color: {color}; " |
| 229 | + "border-bottom-color: {color}; " |
| 230 | + "border-left-color: {color}; " |
| 231 | + "background-color: {color}; " |
| 232 | + "color: {color}").format(color=input_color) |
| 233 | + |
| 234 | + expected = {} |
| 235 | + |
| 236 | + if input_color is not None: |
| 237 | + expected["fill"] = { |
| 238 | + "patternType": "solid" |
| 239 | + } |
| 240 | + |
| 241 | + if output_color is not None: |
| 242 | + expected["border"] = { |
| 243 | + k: { |
| 244 | + "color": output_color, |
| 245 | + } for k in ("top", "right", "bottom", "left") |
| 246 | + } |
| 247 | + |
| 248 | + expected["font"] = { |
| 249 | + "color": output_color |
| 250 | + } |
| 251 | + |
| 252 | + expected["fill"]["fgColor"] = output_color |
| 253 | + |
| 254 | + if input_color in (None, "not-a-color"): |
| 255 | + # Lots of CSSWarnings will be issued |
| 256 | + # because the input color is invalid. |
| 257 | + context = catch_warnings(record=True) |
| 258 | + else: |
| 259 | + context = tm.assert_produces_warning(None) |
| 260 | + |
| 261 | + with context: |
| 262 | + convert = CSSToExcelConverter() |
| 263 | + assert expected == convert(css) |
0 commit comments