1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
097d11875d Improve javadoc parsing
Some checks failed
Run Python tests (through Pytest) / Test (push) Successful in 23s
Verify Python project can be installed, loaded and have version checked / Test (push) Failing after 22s
2025-02-27 17:00:52 +01:00
950b33e94d Support multiline javadoc 2025-02-27 16:41:43 +01:00
2 changed files with 47 additions and 12 deletions

View File

@ -5,26 +5,31 @@ Tool for standardizing Java tests.
import re
PATTERN_JAVADOC = r'/\*\*(?P<javadoc>.*(\n\s*\*.*)*?)\s*\*/'
TEST_PATTERN: re.Pattern = re.compile(
r'(?:/\*\*(?P<comment>.*)\*/)?\s*'
r''
+ r'(?:' + PATTERN_JAVADOC + r'\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)
+ r'(?:@DisplayName\("(?P<display>[^"]*?)"\)\s*)?'
+ r'(?P<visibility>public\s+|private\s+)?void\s+(?P<name>\w+)\(',
re.IGNORECASE)
END_SYMBOLS = '.?!'
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 += ['@', annotation]
str_builder += [indent, '@', annotation]
if with_display_name:
str_builder += ['\n', '@DisplayName("']
str_builder += ['\n', indent, '@DisplayName("']
str_builder.append(description.rstrip(END_SYMBOLS))
str_builder += ['")']
str_builder += ['\n']
str_builder += ['\n', indent]
if visibility:
str_builder += [visibility, ' ']
str_builder += ['void ',name,'(']
@ -43,15 +48,22 @@ def to_camel_case(description: str) -> str:
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 ''
return re.sub(r'^\s*\*', ' ', text.strip()).strip()
def replace_test_pattern(match: re.Match, with_javadoc: bool, with_display_name: bool) -> str:
comment = (match.group('comment') or '').strip()
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 = (match.group('display') or '').strip()
description = display or comment or from_camel_case(name)
description = display or javadoc or from_camel_case(name)
return format_test_prefix(to_camel_case(description),
description, annotation, visibility,

View File

@ -1,5 +1,6 @@
from standardize_test_format import standardize_java_text
from standardize_test_format import standardize_java_text, PATTERN_JAVADOC
import re
INPUT_1 = """
@ -37,14 +38,36 @@ OUTPUT_3 = """
public void helloWorldTest(
"""
INPUT_4 = """
/**
* Concat two produces a new array with the two input arrays joined together, with no separators.
*/
@Test
public void concatTwo()
"""
OUTPUT_4 = """
@Test
@DisplayName("Concat two produces a new array with the two input arrays joined together, with no separators")
public void concatTwoProducesANewArrayWithTheTwoInputArraysJoinedTogetherWithNoSeparators()
"""
def test_javadoc_parsing():
assert re.match(PATTERN_JAVADOC, '/** Hello World */') is not None
assert re.match(PATTERN_JAVADOC, '/** Hello\n * World */') is not None
assert re.match(PATTERN_JAVADOC, '/**\n * Hello World\n */') is not None
def test_1():
assert standardize_java_text(INPUT_1.strip()) == 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()) == 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()) == 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()