diff --git a/synthtool/__main__.py b/synthtool/__main__.py index 12bcedb97..da1ae92af 100644 --- a/synthtool/__main__.py +++ b/synthtool/__main__.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import importlib import os import sys +import importlib.util from typing import List, Sequence import click diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index 5277caccc..ba050efae 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -151,12 +151,12 @@ def _load_partials(self, metadata: Dict): metadata["partials"] = yaml.load(f, Loader=yaml.SafeLoader) -def decamelize(str: str): +def decamelize(value: str): """ parser to convert fooBar.js to Foo Bar. """ - str2 = str[0].upper() - for chr in str[1:]: - if re.match(r"[A-Z]", chr): - str2 += " " + chr.upper() - else: - str2 += chr - return str2 + if not value: + return "" + str_decamelize = re.sub("^.", value[0].upper(), value) # apple -> Apple. + str_decamelize = re.sub( + "([A-Z]+)([A-Z])([a-z0-9])", r"\1 \2\3", str_decamelize + ) # ACLBatman -> ACL Batman. + return re.sub("([a-z0-9])([A-Z])", r"\1 \2", str_decamelize) # FooBar -> Foo Bar. diff --git a/tests/test_common.py b/tests/test_common.py new file mode 100644 index 000000000..f3f87f909 --- /dev/null +++ b/tests/test_common.py @@ -0,0 +1,31 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from synthtool.gcp.common import decamelize + + +def test_converts_camel_to_title(): + assert decamelize("fooBar") == "Foo Bar" + assert decamelize("fooBarSnuh") == "Foo Bar Snuh" + + +def test_handles_acronym(): + assert decamelize("ACL") == "ACL" + assert decamelize("coolACL") == "Cool ACL" + assert decamelize("loadJSONFromGCS") == "Load JSON From GCS" + + +def test_handles_empty_string(): + assert decamelize(None) == "" + assert decamelize("") == ""