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: support consecutive capital letters #220

Merged
merged 7 commits into from
Apr 10, 2019
Merged
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
2 changes: 1 addition & 1 deletion synthtool/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions synthtool/gcp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
31 changes: 31 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -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("") == ""