[css] Add a "Did you mean" suggestion when the value of a color is wrong but we can find a close one

This commit is contained in:
Olivier Philippon
2022-05-10 11:09:29 +01:00
parent 8fd7703fc5
commit e37036c816
6 changed files with 86 additions and 8 deletions

View File

@@ -101,3 +101,47 @@ def test_did_you_mean_for_css_property_names(
f'Did you mean "{expected_property_name_suggestion}"?'
)
assert help_text.bullets[0].markup == expected_suggestion_message
@pytest.mark.parametrize(
"css_property_name,css_property_value,expected_color_suggestion",
[
["color", "blu", "blue"],
["background", "chartruse", "chartreuse"],
["tint", "ansi_whi", "ansi_white"],
["scrollbar-color", "transprnt", "transparent"],
["color", "xkcd", None],
],
)
def test_did_you_mean_for_color_names(
css_property_name: str, css_property_value: str, expected_color_suggestion
):
stylesheet = Stylesheet()
css = """
* {
border: blue;
${PROPERTY}: ${VALUE};
}
""".replace(
"${PROPERTY}", css_property_name
).replace(
"${VALUE}", css_property_value
)
stylesheet.add_source(css)
with pytest.raises(StylesheetParseError) as err:
stylesheet.parse()
_, help_text = err.value.errors.rules[0].errors[0] # type: Any, HelpText
displayed_css_property_name = css_property_name.replace("_", "-")
assert (
help_text.summary
== f"Invalid value for the [i]{displayed_css_property_name}[/] property"
)
first_bullet = help_text.bullets[0]
if expected_color_suggestion is not None:
expected_suggestion_message = f'Did you mean "{expected_color_suggestion}"?'
assert first_bullet.markup == expected_suggestion_message
else:
assert "Did you mean" not in first_bullet.markup