Skip to content

Commit 2a12c0f

Browse files
committed
add function to modify value to java constant
1 parent 1b10caf commit 2a12c0f

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/enum_generator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import re
2+
3+
4+
def to_java_constant(value: str) -> str:
5+
value = re.sub(r"[^A-Za-z0-9]", "_", value) # delimiters a-a -> A_A
6+
value = re.sub(r"([a-z])[_]?([A-Z])([A-Z])([a-z])", r"\1_\2_\3\4", value) # aBCd / a_BCd-> a_B_CD
7+
value = re.sub(r"([a-z])([A-Z])", r"\1_\2", value) # aA -> A_A
8+
value = re.sub(r"([A-Za-z])([0-9])", r"\1_\2", value) # a9 / A9 -> a_9
9+
value = re.sub(r"([0-9])([A-Za-z])", r"\1_\2", value) # 9a / 9A -> 9_A
10+
11+
return value.upper()

tests/test_enum_generator.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from src.enum_generator import to_java_constant
2+
3+
4+
def test_to_java_constant():
5+
values_expected = {
6+
"kit": "KIT",
7+
"KIT": "KIT",
8+
"kitKat": "KIT_KAT",
9+
"KitKat": "KIT_KAT",
10+
"kit-kat": "KIT_KAT",
11+
"kit-Kat": "KIT_KAT",
12+
"kit_kat": "KIT_KAT",
13+
"kit_Kat": "KIT_KAT",
14+
"kit9": "KIT_9",
15+
"kit-9": "KIT_9",
16+
"kit_9": "KIT_9",
17+
"kit92": "KIT_92",
18+
"KiT": "KI_T",
19+
"KiTKat": "KI_T_KAT",
20+
"KitKAT": "KIT_KAT"
21+
}
22+
for key in values_expected.keys():
23+
assert to_java_constant(key) == values_expected[key]

0 commit comments

Comments
 (0)