def test_attribute_value_errors(): with pytest.raises(DialectError, match="invalid character"): apply_dialects("""html(f"<div 'a'x")""", "html") with pytest.raises(DialectError, match="unexpected end of data"): apply_dialects('html(f"<div a{1}")', "html") with pytest.raises(DialectError, match="unexpected end of data"): apply_dialects('html(f"<div a")', "html") with pytest.raises(DialectError, match="unexpected end of data"): apply_dialects('html(f"<div a=")', "html")
def test_tag_errors(): with pytest.raises(DialectError, match="no token found"): apply_dialects('html(f"< >")', "html") with pytest.raises(DialectError, match="no token found"): apply_dialects('html(f"<>")', "html") with pytest.raises(DialectError, match="no token found"): apply_dialects("""html(f"<'")""", "html") with pytest.raises(DialectError, match="unexpected end of data"): apply_dialects('html(f"<")', "html")
def test_attribute_name_errors(): with pytest.raises(DialectError, match="expression not allowed"): apply_dialects('html(f"<div {1}>")', "html") with pytest.raises(DialectError, match="unexpected end of data"): apply_dialects('html(f"<div ")', "html") with pytest.raises(DialectError, match="no token found"): apply_dialects("""html(f"<div '")""", "html")
def eval_html(src, variables=None): tree = apply_dialects(src, "html") if len(tree.body) > 1 or not isinstance(tree.body[0], ast.Expr): raise ValueError(f"Expected a single expression, not {src!r}") code = compile(ast.Expression(tree.body[0].value), "<string>", "eval") return eval(code, {"html": html}, variables)
def test_structural_errors(): with pytest.raises(DialectError, match="all opened tags not closed"): apply_dialects('html(f"<div>")', "html")