From a9b40ea5a70d344edef708552d207410c328b00a Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 27 Feb 2025 14:41:35 +0100 Subject: [PATCH] Working on standardization script --- standardize_test_format/__init__.py | 2 + standardize_test_format/__main__.py | 75 +++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 standardize_test_format/__init__.py create mode 100644 standardize_test_format/__main__.py diff --git a/standardize_test_format/__init__.py b/standardize_test_format/__init__.py new file mode 100644 index 0000000..47528e1 --- /dev/null +++ b/standardize_test_format/__init__.py @@ -0,0 +1,2 @@ +"""# TODO +""" diff --git a/standardize_test_format/__main__.py b/standardize_test_format/__main__.py new file mode 100644 index 0000000..d6bb111 --- /dev/null +++ b/standardize_test_format/__main__.py @@ -0,0 +1,75 @@ +import pathlib +import argparse +import re + +TEST_PATTERN: re.Pattern = re.compile( + r'(?:/\*\*(?P.*)\*/)\s*' + + r'@(?PTest|BeforeEach|ParameterizedTest)\s*' + + r'(?Ppublic\s+|private\s+)?void\s+(?P\w+)\(', re.IGNORECASE) + +OUTPUT_JAVADOC = False +OUTPUT_DISPLAY_NAME = True + +def format_test_prefix(name: str, description: str, annotation: + str,visibility:str) -> str: + str_builder = [] + if OUTPUT_JAVADOC: + str_builder += ['/** ',description,' */','\n'] + str_builder += ['@', annotation] + if OUTPUT_DISPLAY_NAME: + str_builder += ['\n', '@DisplayName("',description,'")'] + str_builder += ['\n'] + if visibility: + str_builder += [visibility, ' '] + str_builder += ['void ',name,'('] + return ''.join(str_builder) + +def name_from_description(description: str) -> str: + description = description.strip(' \t.') + words = description.split(' ') + + words[0] = words[0].lower() + for i in range(1, len(words)): + words[i] = words[i].capitalize() + + return ''.join(words) + +def replace_test_pattern(match: re.Match) -> str: + description = match.group('comment').strip() + annotation = match.group('annotation').strip() + visibility = (match.group('visibility') or '').strip() + name = match.group('name').strip() + formatted = format_test_prefix(name_from_description(description), + description, annotation, visibility) + print(formatted) + return formatted + + +def standardize_java_text(text: str): + return TEST_PATTERN.sub(replace_test_pattern, text) + + +def standardize_in_file(path: pathlib.Path): + with open(path) as f: + text = f.read() + + text = standardize_java_text(text) + + if False: + with open(path, 'w') as f: + f.write(text) + +def test_files(repo: pathlib.Path) -> list[pathlib.Path]: + return list(repo.rglob('*.java')) + +def main(): + argparser = argparse.ArgumentParser() + argparser .add_argument('repo', type=pathlib.Path) + args = argparser.parse_args() + + for path in test_files(args.repo): + standardize_in_file(path) + +if __name__ == '__main__': + main() +