Skip to content

Commit

Permalink
annotate.py: Add line_is_whitespace() and lightly test it
Browse files Browse the repository at this point in the history
  • Loading branch information
jnareb committed Dec 1, 2024
1 parent fdf61f8 commit 9e7722b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/diffannotator/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,19 @@ def line_is_empty(tokens_list: Iterable[tuple]) -> bool:
return len(tokens_list) == 1 and (tokens_list[0][2] == '\n' or tokens_list[0][2] == '\r\n')


def line_is_whitespace(tokens_list: Iterable[tuple]) -> bool:
"""Given results of parsing a line, find if it consists only of whitespace tokens
:param tokens_list: An iterable of (index, token_type, text_fragment) tuples,
supposedly created by parsing some line of source code text
:return: Whether set of tokens in `tokens_list` are all
whitespace tokens
"""
return all([token_type in Token.Text.Whitespace or
token_type in Token.Text and text_fragment.isspace()
for _, token_type, text_fragment in tokens_list])


def line_is_comment(tokens_list: Iterable[tuple]) -> bool:
"""Given results of parsing line, find if it is comment
Expand Down
14 changes: 13 additions & 1 deletion tests/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from diffannotator.annotate import (split_multiline_lex_tokens, line_ends_idx,
group_tokens_by_line, front_fill_gaps, deep_update,
clean_text, line_is_comment, line_is_empty, annotate_single_diff,
Bug, BugDataset, AnnotatedPatchedFile, AnnotatedHunk, AnnotatedPatchSet)
Bug, BugDataset, AnnotatedPatchedFile, AnnotatedHunk, AnnotatedPatchSet,
line_is_whitespace)
from diffannotator.utils.git import GitRepo, DiffSide, ChangeSet
from .conftest import count_pm_lines

Expand Down Expand Up @@ -1017,6 +1018,17 @@ def test__line_is__functions(self):
if k != 0 and k != len(actual) - 2]), \
"all lines but first and next to last line in example code are not empty"

actual = {
i: line_is_whitespace(line_tokens)
for i, line_tokens in tokens_grouped.items()
}

#print("{{{")
#for i, code_line in enumerate(example_C_code.splitlines(keepends=False)):
# print(f"{i:d}: {actual[i]!s:5}:{code_line}", end=':\n')
#print("{{{")

assert actual[0] and actual[5], \
"very basic test for whitespace-only lines (and empty lines)"

# end of test_annotate.py

0 comments on commit 9e7722b

Please sign in to comment.