1
0

Working on standardization script

This commit is contained in:
Jon Michael Aanes 2025-02-27 14:41:35 +01:00
commit a9b40ea5a7
2 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,2 @@
"""# TODO
"""

View File

@ -0,0 +1,75 @@
import pathlib
import argparse
import re
TEST_PATTERN: re.Pattern = re.compile(
r'(?:/\*\*(?P<comment>.*)\*/)\s*'
+ r'@(?P<annotation>Test|BeforeEach|ParameterizedTest)\s*'
+ r'(?P<visibility>public\s+|private\s+)?void\s+(?P<name>\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()