Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: snake case converter corner cases #1515

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions graphene/utils/str_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def to_camel_case(snake_str):
return components[0] + "".join(x.capitalize() if x else "_" for x in components[1:])


# From this response in Stackoverflow
# http://stackoverflow.com/a/1176023/1072990
def to_snake_case(name):
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
if not name:
return name
# Replace every capitalized letter (except the first one) by its lower case variant with an underscore before.
return (name[0] + re.sub("([A-Z])", r"_\1", name[1:])).lower()
24 changes: 24 additions & 0 deletions graphene/utils/tests/test_str_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,41 @@


def test_snake_case():
assert to_snake_case("") == ""
assert to_snake_case("snakesOnAPlane") == "snakes_on_a_plane"
assert to_snake_case("SnakesOnAPlane") == "snakes_on_a_plane"
assert to_snake_case("SnakesOnA_Plane") == "snakes_on_a__plane"
assert to_snake_case("snakes_on_a_plane") == "snakes_on_a_plane"
assert to_snake_case("snakes_on_a__plane") == "snakes_on_a__plane"
assert to_snake_case("IPhoneHysteria") == "i_phone_hysteria"
assert to_snake_case("iPhoneHysteria") == "i_phone_hysteria"
assert to_snake_case("_IPhoneHysteria") == "__i_phone_hysteria"
assert to_snake_case("_File__") == "__file__"
assert to_snake_case("potato01") == "potato01"
assert to_camel_case("a") == "a"
assert to_snake_case("A") == "a"
assert to_snake_case("_A") == "__a"
assert to_snake_case("_A0") == "__a0"
assert to_snake_case("AAA") == "a_a_a"


def test_camel_case():
assert to_camel_case("") == ""
assert to_camel_case("snakesOnAPlane") == "snakesOnAPlane"
assert to_camel_case("snakesOn_a_plane") == "snakesOnAPlane"
assert to_camel_case("snakes_On_a_plane") == "snakesOnAPlane"
assert to_camel_case("snakes_on_a_plane") == "snakesOnAPlane"
assert to_camel_case("snakes_on_a__plane") == "snakesOnA_Plane"
assert to_camel_case("i_phone_hysteria") == "iPhoneHysteria"
assert to_camel_case("_i_phone_hysteria") == "IPhoneHysteria"
assert to_camel_case("__i_phone_hysteria") == "_IPhoneHysteria"
assert to_camel_case("field_i18n") == "fieldI18n"
assert to_camel_case("__file__") == "_File__"
assert to_camel_case("potato01") == "potato01"
assert to_camel_case("potato_0_1") == "potato01"
assert to_camel_case("a") == "a"
assert to_camel_case("A") == "A"
assert to_camel_case("_a") == "A"
assert to_camel_case("__a") == "_A"
assert to_camel_case("__a0") == "_A0"
assert to_camel_case("a_a_a") == "aAA"