Skip to content

Commit

Permalink
add Token.matches(*, name, str)
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Jul 30, 2023
1 parent 09fb26d commit dcfe4e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/tokenize_rt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def test_token_offset():
assert token.offset == Offset(line=1, utf8_byte_offset=2)


def test_token_matches():
token = Token('NAME', 'x', line=1, utf8_byte_offset=2)
assert token.matches(name='NAME', src='x')
assert not token.matches(name='OP', src='x')
assert not token.matches(name='NAME', src='y')
assert not token.matches(name='OP', src=':')


def test_src_to_tokens_simple():
src = 'x = 5\n'
ret = src_to_tokens(src)
Expand Down
3 changes: 3 additions & 0 deletions tokenize_rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Token(NamedTuple):
def offset(self) -> Offset:
return Offset(self.line, self.utf8_byte_offset)

def matches(self, *, name: str, src: str) -> bool:
return self.name == name and self.src == src


_string_re = re.compile('^([^\'"]*)(.*)$', re.DOTALL)
_escaped_nl_re = re.compile(r'\\(\n|\r\n|\r)')
Expand Down

0 comments on commit dcfe4e9

Please sign in to comment.