Skip to content

Commit

Permalink
fix: to_pascal case
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino committed Nov 10, 2024
1 parent 7ba120c commit 2ac9a02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 6 additions & 2 deletions cognite/pygen/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_unit/test_utils/test_text.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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",
[
Expand Down

0 comments on commit 2ac9a02

Please sign in to comment.