Skip to content

Commit 9f07018

Browse files
committed
TST: Add test for named colors
1 parent 8a1f91b commit 9f07018

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

pandas/tests/io/formats/test_to_excel.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"""
55

66
import pytest
7+
import pandas.util.testing as tm
78

9+
from warnings import catch_warnings
810
from pandas.io.formats.excel import CSSToExcelConverter
911

1012

@@ -212,3 +214,50 @@ def test_css_to_excel_multiple():
212214
def test_css_to_excel_inherited(css, inherited, expected):
213215
convert = CSSToExcelConverter(inherited)
214216
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

Comments
 (0)