Skip to content

Commit 3071bac

Browse files
committed
Complete tests
1 parent ceb9171 commit 3071bac

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

pandas/formats/css.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,23 @@ def __call__(self, declarations_str, inherited=None):
150150
# Default: medium only if solid
151151
})
152152

153-
def size_to_pt(self, val, em_pt=None, conversions=UNIT_RATIOS):
153+
def size_to_pt(self, in_val, em_pt=None, conversions=UNIT_RATIOS):
154+
def _error():
155+
warnings.warn('Unhandled size: %r' % in_val, CSSWarning)
156+
return self.size_to_pt('1!!default', conversions=conversions)
157+
154158
try:
155-
val, unit = re.match('(.*?)([a-zA-Z%!].*)', val).groups()
159+
val, unit = re.match(r'^(\S*?)([a-zA-Z%!].*)', in_val).groups()
156160
except AttributeError:
157-
warnings.warn('Unhandled font size: %r' % val, CSSWarning)
158-
return
161+
return _error()
159162
if val == '':
160163
# hack for 'large' etc.
161164
val = 1
162165
else:
163166
try:
164167
val = float(val)
165168
except ValueError:
166-
warnings.warn('Unhandled size: %r' % val + unit,
167-
CSSWarning)
168-
return self.size_to_pt('1!!default', conversions=conversions)
169+
return _error()
169170

170171
while unit != 'pt':
171172
if unit == 'em':
@@ -179,8 +180,7 @@ def size_to_pt(self, val, em_pt=None, conversions=UNIT_RATIOS):
179180
try:
180181
unit, mul = conversions[unit]
181182
except KeyError:
182-
warnings.warn('Unknown size unit: %r' % unit, CSSWarning)
183-
return self.size_to_pt('1!!default', conversions=conversions)
183+
return _error()
184184
val *= mul
185185

186186
val = round(val, 5)
@@ -241,7 +241,8 @@ def parse(self, declarations_str):
241241
prop = prop.strip().lower()
242242
# TODO: don't lowercase case sensitive parts of values (strings)
243243
val = val.strip().lower()
244-
if not sep:
244+
if sep:
245+
yield prop, val
246+
else:
245247
warnings.warn('Ill-formatted attribute: expected a colon '
246248
'in %r' % decl, CSSWarning)
247-
yield prop, val

pandas/tests/formats/test_css.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_css_parse_comments():
3838
markers like !important, but we should
3939
ignore them in the future''')
4040
def test_css_parse_specificity():
41-
pass # TODO
41+
assert_same_resolution('font-weight: bold', 'font-weight: bold !important')
4242

4343

4444
@pytest.mark.xfail(reason='Splitting CSS declarations not yet sensitive to '
@@ -51,8 +51,36 @@ def test_css_parse_strings():
5151
{'background-image': 'url("http://blah.com/foo?a;b=c")'})
5252

5353

54-
def test_css_parse_invalid():
55-
pass # TODO
54+
@pytest.mark.parametrize(
55+
'invalid_css,remainder', [
56+
# No colon
57+
('hello-world', ''),
58+
('border-style: solid; hello-world', 'border-style: solid'),
59+
('border-style: solid; hello-world; font-weight: bold',
60+
'border-style: solid; font-weight: bold'),
61+
# Unclosed string
62+
pytest.mark.xfail(('background-image: "abc', ''),
63+
reason='Unclosed CSS strings not detected'),
64+
pytest.mark.xfail(('font-family: "abc', ''),
65+
reason='Unclosed CSS strings not detected'),
66+
pytest.mark.xfail(('background-image: \'abc', ''),
67+
reason='Unclosed CSS strings not detected'),
68+
pytest.mark.xfail(('font-family: \'abc', ''),
69+
reason='Unclosed CSS strings not detected'),
70+
# Invalid size
71+
('font-size: blah', 'font-size: 1em'),
72+
('font-size: 1a2b', 'font-size: 1em'),
73+
('font-size: 1e5pt', 'font-size: 1em'),
74+
('font-size: 1+6pt', 'font-size: 1em'),
75+
('font-size: 1unknownunit', 'font-size: 1em'),
76+
('font-size: 10', 'font-size: 1em'),
77+
('font-size: 10 pt', 'font-size: 1em'),
78+
])
79+
def test_css_parse_invalid(invalid_css, remainder):
80+
with pytest.warns(CSSWarning):
81+
assert_same_resolution(invalid_css, remainder)
82+
83+
# TODO: we should be checking that in other cases no warnings are raised
5684

5785

5886
@pytest.mark.parametrize(
@@ -226,7 +254,3 @@ def test_css_relative_font_size(size, relative_to, resolved):
226254
inherited = {'font-size': relative_to}
227255
assert_resolves('font-size: %s' % size, {'font-size': resolved},
228256
inherited=inherited)
229-
230-
231-
def test_css_font_size_invalid():
232-
pass # TODO

pandas/tests/formats/test_to_excel.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,6 @@ def test_css_to_excel_inherited(css, inherited, expected):
215215
@pytest.mark.xfail(reason='We are not currently warning for all unconverted '
216216
'CSS, but possibly should')
217217
def test_css_to_excel_warns_when_not_supported():
218-
pass # TODO
218+
convert = CSSToExcelConverter()
219+
with pytest.warns(UserWarning):
220+
convert('background: red')

0 commit comments

Comments
 (0)