Moved into module
This commit is contained in:
parent
a9b40ea5a7
commit
f941882e35
|
@ -1,2 +1,66 @@
|
||||||
"""# TODO
|
"""# TODO
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
TEST_PATTERN: re.Pattern = re.compile(
|
||||||
|
r'(?:/\*\*(?P<comment>.*)\*/)\s*'
|
||||||
|
+ r'@(?P<annotation>Test|BeforeEach|ParameterizedTest)\s*'
|
||||||
|
+ r'(?:@DisplayName\("(?P<display>.*)"\)\s*)?'
|
||||||
|
+ r'(?P<visibility>public\s+|private\s+)?void\s+(?P<name>\w+)\(', re.IGNORECASE)
|
||||||
|
|
||||||
|
OUTPUT_JAVADOC = False
|
||||||
|
OUTPUT_DISPLAY_NAME = True
|
||||||
|
|
||||||
|
END_SYMBOLS = '.?!'
|
||||||
|
|
||||||
|
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("']
|
||||||
|
str_builder.append(description.rstrip(END_SYMBOLS))
|
||||||
|
str_builder += ['")']
|
||||||
|
str_builder += ['\n']
|
||||||
|
if visibility:
|
||||||
|
str_builder += [visibility, ' ']
|
||||||
|
str_builder += ['void ',name,'(']
|
||||||
|
return ''.join(str_builder)
|
||||||
|
|
||||||
|
def to_camel_case(description: str) -> str:
|
||||||
|
description = description.strip(' \t.').replace(',',' ')
|
||||||
|
words = description.split(' ')
|
||||||
|
|
||||||
|
words[0] = words[0].lower()
|
||||||
|
for i in range(1, len(words)):
|
||||||
|
words[i] = words[i].capitalize()
|
||||||
|
|
||||||
|
return ''.join(words)
|
||||||
|
|
||||||
|
def from_camel_case(name: str) -> str:
|
||||||
|
# TODO
|
||||||
|
return name
|
||||||
|
|
||||||
|
def replace_test_pattern(match: re.Match) -> str:
|
||||||
|
comment = match.group('comment').strip()
|
||||||
|
annotation = match.group('annotation').strip()
|
||||||
|
visibility = (match.group('visibility') or '').strip()
|
||||||
|
name = match.group('name').strip()
|
||||||
|
display = (match.group('display') or '').strip()
|
||||||
|
|
||||||
|
|
||||||
|
description = display or comment or from_camel_case(name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
formatted = format_test_prefix(to_camel_case(description),
|
||||||
|
description, annotation, visibility)
|
||||||
|
print(formatted)
|
||||||
|
return formatted
|
||||||
|
|
||||||
|
|
||||||
|
def standardize_java_text(text: str):
|
||||||
|
return TEST_PATTERN.sub(replace_test_pattern, text)
|
||||||
|
|
|
@ -1,53 +1,10 @@
|
||||||
import pathlib
|
import pathlib
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
|
||||||
|
|
||||||
TEST_PATTERN: re.Pattern = re.compile(
|
from . import standardize_java_text
|
||||||
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 test_files(repo: pathlib.Path) -> list[pathlib.Path]:
|
||||||
|
return list(repo.rglob('*.java'))
|
||||||
|
|
||||||
def standardize_in_file(path: pathlib.Path):
|
def standardize_in_file(path: pathlib.Path):
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
|
@ -58,10 +15,6 @@ def standardize_in_file(path: pathlib.Path):
|
||||||
if False:
|
if False:
|
||||||
with open(path, 'w') as f:
|
with open(path, 'w') as f:
|
||||||
f.write(text)
|
f.write(text)
|
||||||
|
|
||||||
def test_files(repo: pathlib.Path) -> list[pathlib.Path]:
|
|
||||||
return list(repo.rglob('*.java'))
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argparser = argparse.ArgumentParser()
|
argparser = argparse.ArgumentParser()
|
||||||
argparser .add_argument('repo', type=pathlib.Path)
|
argparser .add_argument('repo', type=pathlib.Path)
|
||||||
|
@ -72,4 +25,3 @@ def main():
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user