1
0

Compare commits

...

3 Commits

Author SHA1 Message Date
15d8705ac9 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
2025-03-06 12:28:08 +01:00
e6ab2d2857 Fixed docs 2025-03-06 12:22:22 +01:00
a105db28f1 Fix commandline 2025-03-06 12:13:58 +01:00
3 changed files with 35 additions and 12 deletions

View File

@ -10,7 +10,7 @@ Is capable of migrating from Javadoc based property specification to the
Convert tests with `@DisplayName` to Javadoc: Convert tests with `@DisplayName` to Javadoc:
```bash ```bash
python -m standardize_java_text --javadoc --only-displayname <REPOSITORY_PATH> python -m standardize_test_format --javadoc --naming preserve --only-displayname -i <REPOSITORY_PATH>
``` ```
It it recommended that you do this with no unstaged changes, to more easily It it recommended that you do this with no unstaged changes, to more easily
@ -47,8 +47,11 @@ class Config:
PATTERN_JAVADOC = r'/\*\*(?P<javadoc>.*(\n\s*\*.*)*?)\s*\*/' PATTERN_JAVADOC = r'/\*\*(?P<javadoc>.*(\n\s*\*.*)*?)\s*\*/'
PATTERN_DISPLAY_NAME_EXPRESSION = r'\s*(?:"[^"]*?")(?:\s*\+\s*"[^"]*?")*\s*' PATTERN_DISPLAY_NAME_EXPRESSION = r'\s*(?:"[^"]*?")(?:\s*\+\s*"[^"]*?")*\s*'
PATTERN_DISPLAY_NAME = ( PATTERN_DISPLAY_NAME_1 = (
r'@DisplayName\((?P<displayname>' + PATTERN_DISPLAY_NAME_EXPRESSION + r')\)' 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( TEST_PATTERN: re.Pattern = re.compile(
@ -56,9 +59,12 @@ TEST_PATTERN: re.Pattern = re.compile(
+ r'(?:' + r'(?:'
+ PATTERN_JAVADOC + PATTERN_JAVADOC
+ r'\s*)?' + r'\s*)?'
+ r'(?:'
+ PATTERN_DISPLAY_NAME_1
+ r'\s*)?'
+ r'@(?P<annotation>Test|BeforeEach|ParameterizedTest)\s*' + r'@(?P<annotation>Test|BeforeEach|ParameterizedTest)\s*'
+ r'(?:' + r'(?:'
+ PATTERN_DISPLAY_NAME + PATTERN_DISPLAY_NAME_2
+ r'\s*)?' + r'\s*)?'
+ r'(?P<visibility>public\s+|private\s+)?void\s+(?P<name>\w+)\(', + r'(?P<visibility>public\s+|private\s+)?void\s+(?P<name>\w+)\(',
re.IGNORECASE, re.IGNORECASE,
@ -183,7 +189,9 @@ def replace_test_pattern(
annotation = match.group('annotation').strip() annotation = match.group('annotation').strip()
visibility = (match.group('visibility') or '').strip() visibility = (match.group('visibility') or '').strip()
name = match.group('name').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 # Filtering
if config.only_for_javadoc and not javadoc: if config.only_for_javadoc and not javadoc:

View File

@ -3,7 +3,7 @@ import re
import pytest import pytest
from standardize_test_format import ( from standardize_test_format import (
PATTERN_DISPLAY_NAME, PATTERN_DISPLAY_NAME_1,
PATTERN_DISPLAY_NAME_EXPRESSION, PATTERN_DISPLAY_NAME_EXPRESSION,
PATTERN_JAVADOC, PATTERN_JAVADOC,
) )
@ -38,7 +38,7 @@ def test_javadoc_parsing(text: str):
@pytest.mark.parametrize('text', VALID_DISPLAY_NAMES) @pytest.mark.parametrize('text', VALID_DISPLAY_NAMES)
def test_display_name_parsing(text: str): 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) @pytest.mark.parametrize('text', VALID_DISPLAY_NAME_EXPRESSIONS)

View File

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