-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle post-digit caps, add test, and robustify JavaStyle (#288)
- Loading branch information
Showing
4 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/test/java/com/google/api/generator/gapic/utils/BUILD.bazel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
TESTS = [ | ||
"JavaStyleTest", | ||
] | ||
|
||
filegroup( | ||
name = "utils_files", | ||
srcs = ["{0}.java".format(f) for f in TESTS], | ||
) | ||
|
||
[java_test( | ||
name = test_name, | ||
srcs = ["{0}.java".format(test_name)], | ||
test_class = "com.google.api.generator.gapic.utils.{0}".format(test_name), | ||
deps = [ | ||
"//src/main/java/com/google/api/generator/gapic/utils", | ||
"@com_google_truth_truth//jar", | ||
"@junit_junit//jar", | ||
], | ||
) for test_name in TESTS] |
114 changes: 114 additions & 0 deletions
114
src/test/java/com/google/api/generator/gapic/utils/JavaStyleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2020 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 | ||
// | ||
// http://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. | ||
|
||
package com.google.api.generator.gapic.utils; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class JavaStyleTest { | ||
@Test | ||
public void emptyOrNull() { | ||
String value = ""; | ||
assertEquals("", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("", JavaStyle.toUpperCamelCase(value)); | ||
|
||
value = null; | ||
assertThat(JavaStyle.toLowerCamelCase(value)).isNull(); | ||
assertThat(JavaStyle.toUpperCamelCase(value)).isNull(); | ||
} | ||
|
||
@Test | ||
public void singleWord() { | ||
String value = "dog"; | ||
assertEquals("dog", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("Dog", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
|
||
@Test | ||
public void fromLowerSnake() { | ||
String value = "factory_decorator_delegate_impl"; | ||
assertEquals("factoryDecoratorDelegateImpl", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("FactoryDecoratorDelegateImpl", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
|
||
@Test | ||
public void fromUpperSnake() { | ||
String value = "FACTORY_DECORATOR_DELEGATE_IMPL"; | ||
assertEquals("factoryDecoratorDelegateImpl", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("FactoryDecoratorDelegateImpl", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
|
||
@Test | ||
public void fromLowerCamelCase() { | ||
String value = "factoryDecoratorDelegateImpl"; | ||
assertEquals("factoryDecoratorDelegateImpl", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("FactoryDecoratorDelegateImpl", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
|
||
@Test | ||
public void fromUpperCamelCase() { | ||
String value = "FactoryDecoratorDelegateImpl"; | ||
assertEquals("factoryDecoratorDelegateImpl", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("FactoryDecoratorDelegateImpl", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
|
||
@Test | ||
public void wordAndNumber() { | ||
String value = "dog2"; | ||
assertEquals("dog2", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("Dog2", JavaStyle.toUpperCamelCase(value)); | ||
value = "dog_2"; | ||
assertEquals("dog2", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("Dog2", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
|
||
@Test | ||
public void upperWordAndNumber() { | ||
String value = "Dog_v2"; | ||
assertEquals("dogV2", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("DogV2", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
|
||
@Test | ||
public void upperWordAndCharsAfterDigit() { | ||
String value = "dogV2cc"; | ||
assertEquals("dogV2Cc", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("DogV2Cc", JavaStyle.toUpperCamelCase(value)); | ||
|
||
value = "dogv2_cc"; | ||
assertEquals("dogv2Cc", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("Dogv2Cc", JavaStyle.toUpperCamelCase(value)); | ||
|
||
value = "dog_v2_cc"; | ||
assertEquals("dogV2Cc", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("DogV2Cc", JavaStyle.toUpperCamelCase(value)); | ||
|
||
value = "foo1bar2car3"; | ||
assertEquals("foo1Bar2Car3", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("Foo1Bar2Car3", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
|
||
@Test | ||
public void acronyms() { | ||
String value = "iam_http_xml_dog"; | ||
assertEquals("iamHttpXmlDog", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("IamHttpXmlDog", JavaStyle.toUpperCamelCase(value)); | ||
value = "IAM_HTTP_XML_DOG"; | ||
assertEquals("iamHttpXmlDog", JavaStyle.toLowerCamelCase(value)); | ||
assertEquals("IamHttpXmlDog", JavaStyle.toUpperCamelCase(value)); | ||
} | ||
} |