Beispiel #1
0
def test_no_matching_token():
    """Ensure that we handle cases when there is no match."""
    code = """
foo
"""

    assert not list(match_tokens(tokenize(code),
                                 start=match_regex("foo"),
                                 end=match_regex("bar")))
Beispiel #2
0
def test_no_matching_token():
    """Ensure that we handle cases when there is no match."""
    code = """
foo
"""

    assert not list(
        match_tokens(
            tokenize(code), start=match_regex("foo"), end=match_regex("bar")))
Beispiel #3
0
def test_not_enough_lookahead():
    """Ensure that we don't return a match if there's not enough lookahead."""
    code = """
foo bar
"""

    assert not list(match_tokens(tokenize(code),
                                 start=match_regex("foo"),
                                 end=match_regex("bar"),
                                 lookahead=1))
Beispiel #4
0
def test_not_enough_lookahead():
    """Ensure that we don't return a match if there's not enough lookahead."""
    code = """
foo bar
"""

    assert not list(
        match_tokens(tokenize(code),
                     start=match_regex("foo"),
                     end=match_regex("bar"),
                     lookahead=1))
Beispiel #5
0
def test_match_length():
    """Ensure that we correctly find sequences of a provided length."""
    code = """
foo foo bar
"""
    result = match_tokens(tokenize(code),
                          start=match_regex("foo"),
                          end=match_regex("bar"),
                          length=3)
    result = list(result)
    assert len(result) == 1
    assert [i.value for i in result[0]] == ["foo", "foo", "bar"]
Beispiel #6
0
def test_with_matching_tokens():
    """Ensure that we can use the decorator to match tokens."""
    code = """
foo bar
"""

    @with_matched_tokens(start=match_regex("foo"),
                         end=match_regex("bar"))
    def func(tokens, match):
        yield "baz"

    assert list(func(tokenize(code))) == ["baz"]
Beispiel #7
0
def test_with_matching_tokens():
    """Ensure that we can use the decorator to match tokens."""
    code = """
foo bar
"""

    @with_matched_tokens(start=match_regex("foo"), end=match_regex("bar"))
    def func(source, *, match):
        yield "baz"

    source_code = SourceCode(filename="foo.cpp", tokens=tokenize(code))
    assert list(func(source_code)) == ["baz"]
Beispiel #8
0
def test_match_length():
    """Ensure that we correctly find sequences of a provided length."""
    code = """
foo foo bar
"""
    result = match_tokens(tokenize(code),
                          start=match_regex("foo"),
                          end=match_regex("bar"),
                          length=3)
    result = list(result)
    assert len(result) == 1
    assert [i.value for i in result[0]] == ["foo", "foo", "bar"]
Beispiel #9
0
def test_with_matching_tokens():
    """Ensure that we can use the decorator to match tokens."""
    code = """
foo bar
"""

    @with_matched_tokens(start=match_regex("foo"),
                         end=match_regex("bar"))
    def func(source, *, match):
        yield "baz"

    source_code = SourceCode(filename="foo.cpp",
                             tokens=tokenize(code))
    assert list(func(source_code)) == ["baz"]
Beispiel #10
0
def test_match_tokens():
    """Ensure that we match tokens correctly."""
    code = """
foo qux bar
foo grault bar
foo bar
"""

    expected = [["foo", "qux", "bar"], ["foo", "grault", "bar"],
                ["foo", "bar"]]
    assert expected == [[
        token.value for token in match
    ] for match in match_tokens(
        tokenize(code), start=match_regex("foo"), end=match_regex("bar"))]
Beispiel #11
0
def test_match_tokens():
    """Ensure that we match tokens correctly."""
    code = """
foo qux bar
foo grault bar
foo bar
"""

    expected = [["foo", "qux", "bar"],
                ["foo", "grault", "bar"],
                ["foo", "bar"]]
    assert expected == [
        [token.value for token in match]
        for match in match_tokens(tokenize(code),
                                  start=match_regex("foo"),
                                  end=match_regex("bar"))]
Beispiel #12
0
def test_match_regex():
    """Ensure that the regex matcher matches tokens correctly."""
    token = Token(type="identifier", value="foo", start=None, end=None)
    assert match_regex("^foo$")(token)
    assert not match_regex("^bar$")(token)
Beispiel #13
0
def test_match_regex():
    """Ensure that the regex matcher matches tokens correctly."""
    token = Token(type="identifier", value="foo", start=None, end=None)
    assert match_regex("^foo$")(token)
    assert not match_regex("^bar$")(token)