Skip to content

Commit

Permalink
Complete tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jnothman committed Apr 19, 2017
1 parent ceb9171 commit 3071bac
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
23 changes: 12 additions & 11 deletions pandas/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,23 @@ def __call__(self, declarations_str, inherited=None):
# Default: medium only if solid
})

def size_to_pt(self, val, em_pt=None, conversions=UNIT_RATIOS):
def size_to_pt(self, in_val, em_pt=None, conversions=UNIT_RATIOS):
def _error():
warnings.warn('Unhandled size: %r' % in_val, CSSWarning)
return self.size_to_pt('1!!default', conversions=conversions)

try:
val, unit = re.match('(.*?)([a-zA-Z%!].*)', val).groups()
val, unit = re.match(r'^(\S*?)([a-zA-Z%!].*)', in_val).groups()
except AttributeError:
warnings.warn('Unhandled font size: %r' % val, CSSWarning)
return
return _error()
if val == '':
# hack for 'large' etc.
val = 1
else:
try:
val = float(val)
except ValueError:
warnings.warn('Unhandled size: %r' % val + unit,
CSSWarning)
return self.size_to_pt('1!!default', conversions=conversions)
return _error()

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

val = round(val, 5)
Expand Down Expand Up @@ -241,7 +241,8 @@ def parse(self, declarations_str):
prop = prop.strip().lower()
# TODO: don't lowercase case sensitive parts of values (strings)
val = val.strip().lower()
if not sep:
if sep:
yield prop, val
else:
warnings.warn('Ill-formatted attribute: expected a colon '
'in %r' % decl, CSSWarning)
yield prop, val
38 changes: 31 additions & 7 deletions pandas/tests/formats/test_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_css_parse_comments():
markers like !important, but we should
ignore them in the future''')
def test_css_parse_specificity():
pass # TODO
assert_same_resolution('font-weight: bold', 'font-weight: bold !important')


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


def test_css_parse_invalid():
pass # TODO
@pytest.mark.parametrize(
'invalid_css,remainder', [
# No colon
('hello-world', ''),
('border-style: solid; hello-world', 'border-style: solid'),
('border-style: solid; hello-world; font-weight: bold',
'border-style: solid; font-weight: bold'),
# Unclosed string
pytest.mark.xfail(('background-image: "abc', ''),
reason='Unclosed CSS strings not detected'),
pytest.mark.xfail(('font-family: "abc', ''),
reason='Unclosed CSS strings not detected'),
pytest.mark.xfail(('background-image: \'abc', ''),
reason='Unclosed CSS strings not detected'),
pytest.mark.xfail(('font-family: \'abc', ''),
reason='Unclosed CSS strings not detected'),
# Invalid size
('font-size: blah', 'font-size: 1em'),
('font-size: 1a2b', 'font-size: 1em'),
('font-size: 1e5pt', 'font-size: 1em'),
('font-size: 1+6pt', 'font-size: 1em'),
('font-size: 1unknownunit', 'font-size: 1em'),
('font-size: 10', 'font-size: 1em'),
('font-size: 10 pt', 'font-size: 1em'),
])
def test_css_parse_invalid(invalid_css, remainder):
with pytest.warns(CSSWarning):
assert_same_resolution(invalid_css, remainder)

# TODO: we should be checking that in other cases no warnings are raised


@pytest.mark.parametrize(
Expand Down Expand Up @@ -226,7 +254,3 @@ def test_css_relative_font_size(size, relative_to, resolved):
inherited = {'font-size': relative_to}
assert_resolves('font-size: %s' % size, {'font-size': resolved},
inherited=inherited)


def test_css_font_size_invalid():
pass # TODO
4 changes: 3 additions & 1 deletion pandas/tests/formats/test_to_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,6 @@ def test_css_to_excel_inherited(css, inherited, expected):
@pytest.mark.xfail(reason='We are not currently warning for all unconverted '
'CSS, but possibly should')
def test_css_to_excel_warns_when_not_supported():
pass # TODO
convert = CSSToExcelConverter()
with pytest.warns(UserWarning):
convert('background: red')

0 comments on commit 3071bac

Please sign in to comment.