diff --git a/standardize_test_format/__init__.py b/standardize_test_format/__init__.py index 46831f4..6bbe18c 100644 --- a/standardize_test_format/__init__.py +++ b/standardize_test_format/__init__.py @@ -10,33 +10,48 @@ import re PATTERN_JAVADOC = r'/\*\*(?P.*(\n\s*\*.*)*?)\s*\*/' PATTERN_DISPLAY_NAME_EXPRESSION = r'\s*(?:"[^"]*?")(?:\s*\+\s*"[^"]*?")*\s*' -PATTERN_DISPLAY_NAME = r'@DisplayName\((?P'+PATTERN_DISPLAY_NAME_EXPRESSION+r')\)' +PATTERN_DISPLAY_NAME = ( + r'@DisplayName\((?P' + PATTERN_DISPLAY_NAME_EXPRESSION + r')\)' +) TEST_PATTERN: re.Pattern = re.compile( - r'' - + r'(?:' + PATTERN_JAVADOC + r'\s*)?' - + r'@(?PTest|BeforeEach|ParameterizedTest)\s*' - + r'(?:'+PATTERN_DISPLAY_NAME+r'\s*)?' - + r'(?Ppublic\s+|private\s+)?void\s+(?P\w+)\(', - re.IGNORECASE) + r'' + + r'(?:' + + PATTERN_JAVADOC + + r'\s*)?' + + r'@(?PTest|BeforeEach|ParameterizedTest)\s*' + + r'(?:' + + PATTERN_DISPLAY_NAME + + r'\s*)?' + + r'(?Ppublic\s+|private\s+)?void\s+(?P\w+)\(', + re.IGNORECASE, +) END_SYMBOLS = '.?!' -IGNORABLE_WORDS_IN_NAME = frozenset({ - '', - 'a', - 'the', - 'is', - 'are', -}) +IGNORABLE_WORDS_IN_NAME = frozenset( + { + '', + 'a', + 'the', + 'is', + 'are', + }, +) -def format_test_prefix(name: str, description: str, annotation: - str,visibility:str, with_javadoc: bool, - with_display_name: bool) -> str: + +def format_test_prefix( + name: str, + description: str, + annotation: str, + visibility: str, + with_javadoc: bool, + with_display_name: bool, +) -> str: indent = '' str_builder = [] if with_javadoc: - str_builder += ['/** ',description,' */','\n'] + str_builder += ['/** ', description, ' */', '\n'] str_builder += [indent, '@', annotation] if with_display_name: str_builder += ['\n', indent, '@DisplayName("'] @@ -45,9 +60,10 @@ def format_test_prefix(name: str, description: str, annotation: str_builder += ['\n', indent] if visibility: str_builder += [visibility, ' '] - str_builder += ['void ',name,'('] + str_builder += ['void ', name, '('] return ''.join(str_builder) + def to_camel_case_word(word: str, first: bool = False) -> str | None: if word.lower() in IGNORABLE_WORDS_IN_NAME: return None @@ -55,8 +71,15 @@ def to_camel_case_word(word: str, first: bool = False) -> str | None: word = word.lower() return (word[0].lower() if first else word[0].upper()) + word[1:] + def to_camel_case(description: str) -> str: - description = description.replace(',',' ').replace('.',' ').replace('"', ' ').replace('+', ' ').strip(' \t.') + description = ( + description.replace(',', ' ') + .replace('.', ' ') + .replace('"', ' ') + .replace('+', ' ') + .strip(' \t.') + ) words = description.split(' ') words[0] = to_camel_case_word(words[0], first=True) @@ -69,10 +92,14 @@ def to_camel_case(description: str) -> str: return ''.join(words) -def from_camel_case(name: str) -> str: - return re.sub(r'(^|[A-Z])[a-z]*', lambda x: ' '+x.group(0).capitalize(), name).strip() -def parse_javadoc_to_description(text: str| None): +def from_camel_case(name: str) -> str: + return re.sub( + r'(^|[A-Z])[a-z]*', lambda x: ' ' + x.group(0).capitalize(), name, + ).strip() + + +def parse_javadoc_to_description(text: str | None): if text is None: return '' text = re.sub(r'\n\s*\*', ' ', text).strip() @@ -80,7 +107,7 @@ def parse_javadoc_to_description(text: str| None): return text -def parse_display_name_to_description(text: str|None): +def parse_display_name_to_description(text: str | None): if text is None: return '' @@ -91,28 +118,35 @@ def parse_display_name_to_description(text: str|None): return text -def replace_test_pattern(match: re.Match, with_javadoc: bool, with_display_name: bool) -> str: +def replace_test_pattern( + match: re.Match, with_javadoc: bool, with_display_name: bool, +) -> str: javadoc = parse_javadoc_to_description(match.group('javadoc')) - annotation = match.group('annotation').strip() visibility = (match.group('visibility') or '').strip() name = match.group('name').strip() display = parse_display_name_to_description(match.group('display')) - description = display or javadoc or from_camel_case(name) - return format_test_prefix(to_camel_case(description), - description, annotation, visibility, - with_javadoc=with_javadoc, - with_display_name=with_display_name) + return format_test_prefix( + to_camel_case(description), + description, + annotation, + visibility, + with_javadoc=with_javadoc, + with_display_name=with_display_name, + ) + IMPORT_DISPLAY_NAME = 'import org.junit.jupiter.api.DisplayName;' def standardize_java_text(text: str, with_javadoc: bool, with_display_name: bool): - text = TEST_PATTERN.sub(lambda m: replace_test_pattern(m, with_javadoc, with_display_name), text) + text = TEST_PATTERN.sub( + lambda m: replace_test_pattern(m, with_javadoc, with_display_name), text, + ) if '@DisplayName' in text and IMPORT_DISPLAY_NAME not in text: lines = text.split('\n') lines.insert(1, IMPORT_DISPLAY_NAME) diff --git a/standardize_test_format/__main__.py b/standardize_test_format/__main__.py index e80f233..6dd65cb 100644 --- a/standardize_test_format/__main__.py +++ b/standardize_test_format/__main__.py @@ -1,13 +1,16 @@ -import pathlib import argparse +import pathlib from . import standardize_java_text + def test_files(repo: pathlib.Path) -> list[pathlib.Path]: return list(repo.rglob('*.java')) -def standardize_in_file(path: pathlib.Path, inline: bool, with_javadoc: bool, - with_display_name: bool): + +def standardize_in_file( + path: pathlib.Path, inline: bool, with_javadoc: bool, with_display_name: bool, +): with open(path) as f: text = f.read() @@ -21,6 +24,7 @@ def standardize_in_file(path: pathlib.Path, inline: bool, with_javadoc: bool, else: print(text_updated) + def argument_parser(): argparser = argparse.ArgumentParser() argparser.add_argument('repo', type=pathlib.Path) @@ -29,11 +33,18 @@ def argument_parser(): argparser.add_argument('--displayname', action='store_true') return argparser + def main(): args = argument_parser().parse_args() for path in test_files(args.repo): - standardize_in_file(path, inline=args.i, with_javadoc=args.javadoc, with_display_name=args.displayname) + standardize_in_file( + path, + inline=args.i, + with_javadoc=args.javadoc, + with_display_name=args.displayname, + ) + if __name__ == '__main__': main() diff --git a/test/test_standardize.py b/test/test_standardize.py index 8c7c59b..ace4416 100644 --- a/test/test_standardize.py +++ b/test/test_standardize.py @@ -1,7 +1,4 @@ - -from standardize_test_format import standardize_java_text, PATTERN_JAVADOC -import re - +from standardize_test_format import standardize_java_text INPUT_1 = """ package test; @@ -115,22 +112,28 @@ public void zkBinderContextImplPropagatesLotOfFieldsDirectlyFromUnderlyingZkBind def test_1(): - assert standardize_java_text(INPUT_1.strip(),False,True) == OUTPUT_1.strip() + assert standardize_java_text(INPUT_1.strip(), False, True) == OUTPUT_1.strip() + def test_2(): - assert standardize_java_text(INPUT_2.strip(),False,True) == OUTPUT_2.strip() + assert standardize_java_text(INPUT_2.strip(), False, True) == OUTPUT_2.strip() + def test_3(): - assert standardize_java_text(INPUT_3.strip(),False,True) == OUTPUT_3.strip() + assert standardize_java_text(INPUT_3.strip(), False, True) == OUTPUT_3.strip() + def test_4(): - assert standardize_java_text(INPUT_4.strip(),False,True) == OUTPUT_4.strip() + assert standardize_java_text(INPUT_4.strip(), False, True) == OUTPUT_4.strip() + def test_5(): - assert standardize_java_text(INPUT_5.strip(),False,True) == OUTPUT_5.strip() + assert standardize_java_text(INPUT_5.strip(), False, True) == OUTPUT_5.strip() + def test_5b(): - assert standardize_java_text(INPUT_5B.strip(),False,True) == OUTPUT_5.strip() + assert standardize_java_text(INPUT_5B.strip(), False, True) == OUTPUT_5.strip() + def test_5c(): - assert standardize_java_text(INPUT_5C.strip(),False,True) == OUTPUT_5.strip() + assert standardize_java_text(INPUT_5C.strip(), False, True) == OUTPUT_5.strip()