示例#1
0
def test_make_lexer(params, superclass):
    lexer = make_lexer(**params)

    assert superclass in lexer.mro(), \
        "Highlighted{Language}Lexer should be subclassed from language lexer"

    assert lexer.__name__ == f"Highlighted{superclass.__name__}", \
        "The lexer class name should be the parents name prefixed by 'Highlighted'"
示例#2
0
def test_for_errors(datadir, filename):
    filepath = datadir / filename
    code = filepath.read_text()
    lexer = make_lexer("python", code)
    tokens = list(lex(code, lexer()))
    errors = [(i, tok, text) for i, (tok, text) in enumerate(tokens)
              if "Error" in str(tok)]

    assert not errors
示例#3
0
def test_lexer(datadir):
    filepath = datadir / "while-loop.py"
    code = filepath.read_text()
    lexer = make_lexer("python", code)

    highlighted, markers = [], []

    for token, text in lex(code, lexer()):
        if str(token).endswith("Highlight"):
            highlighted.append(text)
        elif token is Token.Marker:
            markers.append(text)

    assert not len(markers), "Markers tokens should be removed"
    assert len(
        highlighted) == 4, "Tokens enclosed by !!! should be highlighted"
    assert "Grumpy" in highlighted
    assert "dwarves" in highlighted
    assert '"' in highlighted
示例#4
0
def test_add_highlight():
    code = """print("!!!well hello!!!")"""
    klass = make_lexer("python", code)
    match = re.search(rf'(!!!)(.+?)(!!!)(.*)$', code)
    lexer = klass()

    # [(10, Token.Name.Highlight, 'well'),
    #  (14, Token.Text.Highlight, ' '),
    #  (15, Token.Name.Highlight, 'hello'),
    #  (23, Token.Literal.String.Double, '"'),
    #  (24, Token.Literal.String.Double, ')')]

    tokens = list(lexer.add_highlight(match, PythonLexer))

    assert "well hello" == "".join(
        [val for _, tok, val in tokens if str(tok).endswith("Highlight")]), \
        "Only tokens enclosed by !!! should be appended with .Highlight"

    assert not any( [token == Token.Marker for pos, token, text in tokens]), \
        "Highlight markers (!!!) should be removed."
示例#5
0
def test_make_lexer_exceptions(params, error):
    with pytest.raises(error):
        make_lexer(**params)