Skip to content

Commit 3b161c5

Browse files
committed
TST: Add test for named colors
1 parent 51a4963 commit 3b161c5

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

pandas/tests/io/formats/test_to_excel.py

Lines changed: 60 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,61 @@ 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+
)
224+
def test_css_to_excel_good_colors(input_color, output_color):
225+
# see gh-18392
226+
css = ("border-top-color: {color}; "
227+
"border-right-color: {color}; "
228+
"border-bottom-color: {color}; "
229+
"border-left-color: {color}; "
230+
"background-color: {color}; "
231+
"color: {color}").format(color=input_color)
232+
233+
expected = dict()
234+
235+
expected["fill"] = {
236+
"patternType": "solid",
237+
"fgColor": output_color
238+
}
239+
240+
expected["font"] = {
241+
"color": output_color
242+
}
243+
244+
expected["border"] = {
245+
k: {
246+
"color": output_color,
247+
} for k in ("top", "right", "bottom", "left")
248+
}
249+
250+
with tm.assert_produces_warning(None):
251+
convert = CSSToExcelConverter()
252+
assert expected == convert(css)
253+
254+
255+
@pytest.mark.parametrize("input_color", [None, "not-a-color"])
256+
def test_css_to_excel_bad_colors(input_color):
257+
# see gh-18392
258+
css = ("border-top-color: {color}; "
259+
"border-right-color: {color}; "
260+
"border-bottom-color: {color}; "
261+
"border-left-color: {color}; "
262+
"background-color: {color}; "
263+
"color: {color}").format(color=input_color)
264+
265+
expected = dict()
266+
267+
if input_color is not None:
268+
expected["fill"] = {
269+
"patternType": "solid"
270+
}
271+
272+
with catch_warnings(record=True):
273+
convert = CSSToExcelConverter()
274+
assert expected == convert(css)

0 commit comments

Comments
 (0)