garydgregory commented on code in PR #552:
URL: https://github.com/apache/commons-text/pull/552#discussion_r1614896311


##########
src/test/java/org/apache/commons/text/CasedStringTest.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.commons.text;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+
+import org.apache.commons.text.CasedString.StringCase;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+public class CasedStringTest {
+
+    private static String helloWorldValue(StringCase stringCase) {
+        switch (stringCase) {
+            case CAMEL:
+                return "helloWorld";
+            case KEBAB:
+                return "hello-World";
+            case PHRASE:
+                return "hello World";
+            case SNAKE:
+                return "hello_World";
+            case DOT:
+                return "hello.World";
+            default:
+                fail("Unsupported StringCase: " + stringCase);
+        }
+        return null; // keeps compiler happy
+    }
+
+    private static final CasedString CAMEL = new CasedString(StringCase.CAMEL, 
"aCamelString");
+    private static final CasedString PHRASE = new 
CasedString(StringCase.PHRASE, "A test PhrAse");
+    private static final CasedString KEBAB = new CasedString(StringCase.KEBAB, 
"A-kebAb-string");
+    private static final CasedString SNAKE = new CasedString(StringCase.SNAKE, 
"A_snaKE_string");
+    private static final CasedString DOT = new CasedString(StringCase.DOT, 
"A.dOt.string");
+    private static final CasedString ABCDEF = new 
CasedString(StringCase.PHRASE, "a  b  c  @def");
+    /**
+     * tests the conversion from each Cased string type to every other type.
+     * @param underTest the CasedString being tested.
+     */
+    @ParameterizedTest
+    @MethodSource("conversionProvider")
+    public void testCrossProductConversions(CasedString underTest) {
+        for (StringCase stringCase : StringCase.values()) {
+            assertEquals(helloWorldValue(stringCase), 
underTest.toCase(stringCase), () -> "failed converting to " + stringCase);
+        }
+    }
+    /* generates the hello world Cased String for every StringCase */
+    private static Stream<Arguments> conversionProvider() {
+        List<Arguments> lst = new ArrayList<>();
+        for (StringCase stringCase : StringCase.values()) {
+            lst.add(Arguments.of(new CasedString(stringCase, 
helloWorldValue(stringCase))));
+        }
+        return lst.stream();
+    }
+
+    @Test
+    public void testNullConstructor() {
+        for (StringCase stringCase : StringCase.values()) {
+            CasedString underTest = new CasedString(stringCase, null);
+            assertThat(underTest.toString()).isNull();
+            assertThat(underTest.getSegments()).isEmpty();
+            // test a null underTest can convert to all others types.
+            for (CasedString.StringCase otherCase : StringCase.values()) {
+                assertThat(underTest.toCase(otherCase)).isNull();
+            }
+        }
+    }
+
+    @Test
+    public void testToCamelCase() {
+        assertThat(new CasedString(StringCase.CAMEL, 
"").toString()).isEqualTo("");
+        assertThat(new CasedString(StringCase.CAMEL, "  
").toString()).isEqualTo("");
+        assertThat(new CasedString(StringCase.CAMEL, 
"Tocamelcase").toString()).isEqualTo("tocamelcase");

Review Comment:
   > I cam across a reference that says camel case is lower case first 
character and pascal case is upper case first character. to create a java 
method name use
   > 
   > ```
   > CasedString cString = new CasedString(StringCase.....
   > String javaClassName = 
WordUtils.capitalise(cCstring.toCase(StringCase.CAMEL));
   > String javaVarName = cString.toCase(StringCase.CAMEL));
   > ```
   > 
   > Alternatively we could add PASCAL case.
   
   That's what I'm getting at, camel case is ambiguous depending on your 
context. In Java it means one thing for classes and another for method names. 
And then there is Pascal (it's not an acronym BTW).
   
   > Camel case is a way of writing phrases without spaces, where the first 
letter of each word is capitalized, except for the first letter of the entire 
compound word, which may be either upper or lower case. 
   
   
   From https://developer.mozilla.org/en-US/docs/Glossary/Camel_case
   
   This tells me that StringCase.CAMEL does not define anything precise. 
StringCase.HIGHER_CAMEL, StringCase.LOWER_CAMEL lets you know what you get and 
it is _one_ variation.
   
   Another variation is what to do with words that are in all caps like 
acronyms. From the same URL:
   
   > Note that if the phrase contains acronyms (such as URI and HTML), camel 
casing practices vary. Some prefer to keep all of them capitalized, such as 
encodeURIComponent above. This may sometimes lead to ambiguity with multiple 
consecutive acronyms, such as XMLHTTPRequest. Others prefer to only capitalize 
the first letter, as XmlHttpRequest. The actual global variable, 
[XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest),
 uses a mix of both.
   
   If the input is "XMLHttpRequest", it would be odd to get back a camel string 
of "xMLHttpRequest" for LOWER_CAMEL, I'd expect "xmlHttpRequest".
   
   All of this to say, that we need to define the behavior better IMO.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to