diff --git a/standardize_test_format/__init__.py b/standardize_test_format/__init__.py index 9c64120..557d0ed 100644 --- a/standardize_test_format/__init__.py +++ b/standardize_test_format/__init__.py @@ -6,12 +6,13 @@ Tool for standardizing Java tests. import re PATTERN_JAVADOC = r'/\*\*(?P.*(\n\s*\*.*)*?)\s*\*/' +PATTERN_DISPLAY_NAME = r'@DisplayName\(\s*"(?P[^"]*?)"\s*\)' TEST_PATTERN: re.Pattern = re.compile( r'' + r'(?:' + PATTERN_JAVADOC + r'\s*)?' + r'@(?PTest|BeforeEach|ParameterizedTest)\s*' - + r'(?:@DisplayName\("(?P[^"]*?)"\)\s*)?' + + r'(?:'+PATTERN_DISPLAY_NAME+r'\s*)?' + r'(?Ppublic\s+|private\s+)?void\s+(?P\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: diff --git a/test/test_standardize.py b/test/test_standardize.py index 4e3f048..2172fd5 100644 --- a/test/test_standardize.py +++ b/test/test_standardize.py @@ -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()