1
0

Support DisplayName before Test annotation
All checks were successful
Run Python tests (through Pytest) / Test (push) Successful in 23s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 21s

This commit is contained in:
Jon Michael Aanes 2025-03-06 12:28:08 +01:00
parent e6ab2d2857
commit 15d8705ac9
3 changed files with 34 additions and 11 deletions

View File

@ -47,8 +47,11 @@ class Config:
PATTERN_JAVADOC = r'/\*\*(?P<javadoc>.*(\n\s*\*.*)*?)\s*\*/'
PATTERN_DISPLAY_NAME_EXPRESSION = r'\s*(?:"[^"]*?")(?:\s*\+\s*"[^"]*?")*\s*'
PATTERN_DISPLAY_NAME = (
r'@DisplayName\((?P<displayname>' + PATTERN_DISPLAY_NAME_EXPRESSION + r')\)'
PATTERN_DISPLAY_NAME_1 = (
r'@DisplayName\((?P<displayname1>' + PATTERN_DISPLAY_NAME_EXPRESSION + r')\)'
)
PATTERN_DISPLAY_NAME_2 = (
r'@DisplayName\((?P<displayname2>' + PATTERN_DISPLAY_NAME_EXPRESSION + r')\)'
)
TEST_PATTERN: re.Pattern = re.compile(
@ -56,9 +59,12 @@ TEST_PATTERN: re.Pattern = re.compile(
+ r'(?:'
+ PATTERN_JAVADOC
+ r'\s*)?'
+ r'(?:'
+ PATTERN_DISPLAY_NAME_1
+ r'\s*)?'
+ r'@(?P<annotation>Test|BeforeEach|ParameterizedTest)\s*'
+ r'(?:'
+ PATTERN_DISPLAY_NAME
+ PATTERN_DISPLAY_NAME_2
+ r'\s*)?'
+ r'(?P<visibility>public\s+|private\s+)?void\s+(?P<name>\w+)\(',
re.IGNORECASE,
@ -183,7 +189,9 @@ def replace_test_pattern(
annotation = match.group('annotation').strip()
visibility = (match.group('visibility') or '').strip()
name = match.group('name').strip()
displayname = parse_display_name_to_description(match.group('displayname'))
displayname1 = parse_display_name_to_description(match.group('displayname1'))
displayname2 = parse_display_name_to_description(match.group('displayname2'))
displayname = displayname1 or displayname2
# Filtering
if config.only_for_javadoc and not javadoc:

View File

@ -3,7 +3,7 @@ import re
import pytest
from standardize_test_format import (
PATTERN_DISPLAY_NAME,
PATTERN_DISPLAY_NAME_1,
PATTERN_DISPLAY_NAME_EXPRESSION,
PATTERN_JAVADOC,
)
@ -38,7 +38,7 @@ def test_javadoc_parsing(text: str):
@pytest.mark.parametrize('text', VALID_DISPLAY_NAMES)
def test_display_name_parsing(text: str):
assert re.match(PATTERN_DISPLAY_NAME, text) is not None
assert re.match(PATTERN_DISPLAY_NAME_1, text) is not None
@pytest.mark.parametrize('text', VALID_DISPLAY_NAME_EXPRESSIONS)

View File

@ -1,6 +1,6 @@
import pytest
from standardize_test_format import NamingScheme, standardize_java_text
from standardize_test_format import NamingScheme, standardize_java_text, Config
INPUT_1 = """
package test;
@ -103,13 +103,25 @@ package test;
public void zkBinderContextImplPropagatesLotOfFieldsDirectlyFromTheUnderlyingZkBinderContext()
"""
INPUT_5D = """
package test;
@DisplayName(
"ZkBinderContextImpl propagates a lot of "
+ "fields directly from "
+ "the underlying ZkBinderContext"
)
@Test
public void zkBinderContextImplPropagatesLotOfFieldsDirectlyFromTheUnderlyingZkBinderContext()
"""
OUTPUT_5 = """
package test;
import org.junit.jupiter.api.DisplayName;
@Test
@DisplayName("ZkBinderContextImpl propagates a lot of fields directly from the underlying ZkBinderContext")
public void zkBinderContextImplPropagatesLotOfFieldsDirectlyFromUnderlyingZkBinderContext()
public void zkBinderContextImplPropagatesLotFieldsDirectlyFromUnderlyingZkBinderContext()
"""
JAVADOC_TO_DISPLAYNAME = [
@ -120,15 +132,18 @@ JAVADOC_TO_DISPLAYNAME = [
(INPUT_5, OUTPUT_5),
(INPUT_5B, OUTPUT_5),
(INPUT_5C, OUTPUT_5),
(INPUT_5D, OUTPUT_5),
]
@pytest.mark.parametrize('inp,output', JAVADOC_TO_DISPLAYNAME)
@pytest.mark.parametrize(('inp','output'), JAVADOC_TO_DISPLAYNAME)
def test_javadoc_to_displayname(inp: str, output: str):
converted = standardize_java_text(
inp.strip(),
config = Config(
with_javadoc=False,
with_display_name=True,
only_for_javadoc = False,
only_for_display_name = False,
naming_scheme=NamingScheme.FROM_DESC,
)
converted = standardize_java_text(inp.strip(), config)
assert converted == output.strip()