1
0

Improved parsing of DisplayName and improved handling of abbreviations
Some checks failed
Run Python tests (through Pytest) / Test (push) Successful in 24s
Verify Python project can be installed, loaded and have version checked / Test (push) Failing after 22s

This commit is contained in:
Jon Michael Aanes 2025-02-28 09:33:54 +01:00
parent 5117a9afd2
commit e803f21f6e
2 changed files with 44 additions and 16 deletions

View File

@ -6,12 +6,13 @@ Tool for standardizing Java tests.
import re
PATTERN_JAVADOC = r'/\*\*(?P<javadoc>.*(\n\s*\*.*)*?)\s*\*/'
PATTERN_DISPLAY_NAME = r'@DisplayName\(\s*"(?P<display>[^"]*?)"\s*\)'
TEST_PATTERN: re.Pattern = re.compile(
r''
+ r'(?:' + PATTERN_JAVADOC + r'\s*)?'
+ r'@(?P<annotation>Test|BeforeEach|ParameterizedTest)\s*'
+ r'(?:@DisplayName\("(?P<display>[^"]*?)"\)\s*)?'
+ r'(?:'+PATTERN_DISPLAY_NAME+r'\s*)?'
+ r'(?P<visibility>public\s+|private\s+)?void\s+(?P<name>\w+)\(',
re.IGNORECASE)
@ -43,21 +44,18 @@ def format_test_prefix(name: str, description: str, annotation:
str_builder += ['void ',name,'(']
return ''.join(str_builder)
def to_camel_case_word(word: str) -> str | None:
print(word)
def to_camel_case_word(word: str, first: bool = False) -> str | None:
if word.lower() in IGNORABLE_WORDS_IN_NAME:
return None
if len(word) > 1 and word.upper() == word:
word = word[0].upper() + word[1:].lower()
else:
word = word[0].upper() + word[1:]
return word
word = word.lower()
return (word[0].lower() if first else word[0].upper()) + word[1:]
def to_camel_case(description: str) -> str:
description = description.strip(' \t.').replace(',',' ')
words = description.split(' ')
words[0] = words[0][0].lower() + words[0][1:]
words[0] = to_camel_case_word(words[0], first=True)
for i in range(1, len(words)):
word = to_camel_case_word(words[i])
if not word:

View File

@ -4,41 +4,58 @@ import re
INPUT_1 = """
package test;
@Test
@DisplayName("Invocations fail with callers without required permissions")
public void selectInvocationWithoutPermission() { }
"""
OUTPUT_1 = """
package test;
import org.junit.jupiter.api.DisplayName;
@Test
@DisplayName("Invocations fail with callers without required permissions")
public void invocationsFailWithCallersWithoutRequiredPermissions() { }
"""
INPUT_2 = """
package test;
/** Invocations fail with callers without required permissions. */
@Test
public void selectInvocationWithoutPermission() { }
"""
OUTPUT_2 = """
package test;
import org.junit.jupiter.api.DisplayName;
@Test
@DisplayName("Invocations fail with callers without required permissions")
public void invocationsFailWithCallersWithoutRequiredPermissions() { }
"""
INPUT_3 = """
package test;
@Test
public void helloWorldTest(
"""
OUTPUT_3 = """
package test;
import org.junit.jupiter.api.DisplayName;
@Test
@DisplayName("Hello World Test")
public void helloWorldTest(
"""
INPUT_4 = """
package test;
/**
* Concat two produces a new array with the two input arrays joined together, with no separators.
*/
@ -47,12 +64,17 @@ public void concatTwo()
"""
OUTPUT_4 = """
package test;
import org.junit.jupiter.api.DisplayName;
@Test
@DisplayName("Concat two produces a new array with the two input arrays joined together, with no separators")
public void concatTwoProducesANewArrayWithTheTwoInputArraysJoinedTogetherWithNoSeparators()
public void concatTwoProducesNewArrayWithTwoInputArraysJoinedTogetherWithNoSeparators()
"""
INPUT_5 = """
package test;
/**
* {@link ZkBinderContextImpl} propagates a lot of fields directly from the underlying {@link
* ZkBinderContext}.
@ -61,17 +83,23 @@ INPUT_5 = """
public void propagate()
"""
OUTPUT_5 = """
INPUT_5B = """
package test;
@Test
@DisplayName("ZkBinderContextImpl propagates a lot of fields directly from the underlying ZkBinderContext")
public void zkBinderContextImplPropagatesALotOfFieldsDirectlyFromTheUnderlyingZkBinderContext()
@DisplayName(
"ZkBinderContextImpl propagates a lot of fields directly from the underlying ZkBinderContext")
public void zkBinderContextImplPropagatesLotOfFieldsDirectlyFromTheUnderlyingZkBinderContext()
"""
OUTPUT_5 = """
package test;
import org.junit.jupiter.api.DisplayName;
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
@Test
@DisplayName("ZkBinderContextImpl propagates a lot of fields directly from the underlying ZkBinderContext")
public void zkBinderContextImplPropagatesLotOfFieldsDirectlyFromUnderlyingZkBinderContext()
"""
def test_1():
@ -89,3 +117,5 @@ def test_4():
def test_5():
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()