From 2ac9a028694f75f7118ae3988dbf5384d93947aa Mon Sep 17 00:00:00 2001 From: anders-albert Date: Sun, 10 Nov 2024 15:14:58 +0100 Subject: [PATCH] fix: to_pascal case --- cognite/pygen/utils/text.py | 8 ++++++-- tests/test_unit/test_utils/test_text.py | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cognite/pygen/utils/text.py b/cognite/pygen/utils/text.py index 7073e1c19..2f84210c3 100644 --- a/cognite/pygen/utils/text.py +++ b/cognite/pygen/utils/text.py @@ -73,9 +73,13 @@ def to_camel(string: str, pluralize: bool = False, singularize: bool = False) -> # Ensure pascal string = string[0].upper() + string[1:] pascal_splits = [string] - string_split = [] + string_split: list[str] = [] for part in pascal_splits: - string_split.extend(re.findall(r"[A-Z][a-z0-9]*", part)) + # Split on capital letters to maintain the capital letters from the original string + # The extra filter is to remove empty strings + # This can happen if the string starts with a capital letter + string_split.extend(sub for sub in re.split(r"(?=[A-Z])", part) if sub) + if not string_split: string_split = [string] if pluralize and singularize: diff --git a/tests/test_unit/test_utils/test_text.py b/tests/test_unit/test_utils/test_text.py index e8e748030..9053f2bff 100644 --- a/tests/test_unit/test_utils/test_text.py +++ b/tests/test_unit/test_utils/test_text.py @@ -1,6 +1,6 @@ import pytest -from cognite.pygen.utils.text import to_pascal, to_snake +from cognite.pygen.utils.text import to_camel, to_pascal, to_snake @pytest.mark.parametrize( @@ -28,6 +28,20 @@ def test_to_pascal(word: str, singularize: bool, pluralize: bool, expected: str) assert actual == expected +@pytest.mark.parametrize( + "word, singularize, pluralize, expected", + [ + ("a_b", False, False, "aB"), + ], +) +def test_to_camel(word: str, singularize: bool, pluralize: bool, expected: str): + # Act + actual = to_camel(word, singularize=singularize, pluralize=pluralize) + + # Assert + assert actual == expected + + @pytest.mark.parametrize( "word, singularize, pluralize, expected", [