fix bug and add method

This commit is contained in:
Looly
2022-03-14 00:32:35 +08:00
parent 2bfa2cebf2
commit e389227964
7 changed files with 71 additions and 82 deletions

View File

@@ -1,21 +1,44 @@
package cn.hutool.core.text;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.CharUtil;
import org.junit.Assert;
import org.junit.Test;
public class NamingCaseTest {
@Test
public void toUnderlineCaseTest(){
// https://github.com/dromara/hutool/issues/2070
public void toCamelCaseTest() {
Dict.create()
.set("customerNickV2", "customer_nick_v2")
.forEach((key, value) -> Assert.assertEquals(value, NamingCase.toUnderlineCase(key)));
.set("Table_Test_Of_day","tableTestOfDay")
.set("TableTestOfDay","TableTestOfDay")
.set("abc_1d","abc1d")
.forEach((key, value) -> Assert.assertEquals(value, NamingCase.toCamelCase(key)));
}
@Test
public void toUnderLineCaseTest2(){
final String wPRunOZTime = NamingCase.toUnderlineCase("wPRunOZTime");
Assert.assertEquals("w_P_run_OZ_time", wPRunOZTime);
public void toCamelCaseFromDashedTest() {
Dict.create()
.set("Table-Test-Of-day","tableTestOfDay")
.forEach((key, value) -> Assert.assertEquals(value, NamingCase.toCamelCase(key, CharUtil.DASHED)));
}
@Test
public void toUnderLineCaseTest() {
Dict.create()
.set("Table_Test_Of_day", "table_test_of_day")
.set("_Table_Test_Of_day_", "_table_test_of_day_")
.set("_Table_Test_Of_DAY_", "_table_test_of_DAY_")
.set("_TableTestOfDAYToday", "_table_test_of_DAY_today")
.set("HelloWorld_test", "hello_world_test")
.set("H2", "H2")
.set("H#case", "H#case")
.set("PNLabel", "PN_label")
.set("wPRunOZTime", "w_P_run_OZ_time")
// https://github.com/dromara/hutool/issues/2070
.set("customerNickV2", "customer_nick_v2")
// https://gitee.com/dromara/hutool/issues/I4X9TT
.set("DEPT_NAME","DEPT_NAME")
.forEach((key, value) -> Assert.assertEquals(value, NamingCase.toUnderlineCase(key)));
}
}

View File

@@ -472,36 +472,32 @@ public class ArrayUtilTest {
String[] a = {"1", "2", "3", "4"};
String[] b = {"a", "b", "c"};
// 在小于0的位置-1位置插入返回b+a
// 在小于0的位置-1位置插入返回b+a,新数组
String[] result = ArrayUtil.replace(a, -1, b);
Assert.assertArrayEquals(new String[]{"a", "b", "c", "1", "2", "3", "4"}, result);
// 在第0个位置插入即覆盖a直接返回b
result = ArrayUtil.replace(a, 0, b);
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
// 在第0个位置开始替换,返回a
result = ArrayUtil.replace(ArrayUtil.clone(a), 0, b);
Assert.assertArrayEquals(new String[]{"a", "b", "c", "4"}, result);
// 在第1个位置插入,即"2"之前
result = ArrayUtil.replace(a, 1, b);
// 在第1个位置替换,即"2"开始
result = ArrayUtil.replace(ArrayUtil.clone(a), 1, b);
Assert.assertArrayEquals(new String[]{"1", "a", "b", "c"}, result);
//上一步测试修改了原数组结构
String[] c = {"1", "2", "3", "4"};
String[] d = {"a", "b", "c"};
// 在第2个位置插入即"3"之后
result = ArrayUtil.replace(c, 2, d);
result = ArrayUtil.replace(ArrayUtil.clone(a), 2, b);
Assert.assertArrayEquals(new String[]{"1", "2", "a", "b", "c"}, result);
// 在第3个位置插入即"4"之后
result = ArrayUtil.replace(c, 3, d);
result = ArrayUtil.replace(ArrayUtil.clone(a), 3, b);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "a", "b", "c"}, result);
// 在第4个位置插入数组长度为4在索引4出替换即两个数组相加
result = ArrayUtil.replace(c, 4, d);
result = ArrayUtil.replace(ArrayUtil.clone(a), 4, b);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
// 在大于3个位置插入数组长度为4即两个数组相加
result = ArrayUtil.replace(c, 5, d);
result = ArrayUtil.replace(ArrayUtil.clone(a), 5, b);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
String[] e = null;
@@ -509,13 +505,13 @@ public class ArrayUtilTest {
// e为null 返回 f
result = ArrayUtil.replace(e, -1, f);
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
Assert.assertArrayEquals(f, result);
String[] g = {"a", "b", "c"};
String[] h = null;
// h为null 返回 g
result = ArrayUtil.replace(g, 0, h);
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
Assert.assertArrayEquals(g, result);
}
}

View File

@@ -391,41 +391,6 @@ public class StrUtilTest {
Assert.assertEquals(text, str);
}
@Test
public void toCamelCaseTest() {
String str = "Table_Test_Of_day";
String result = StrUtil.toCamelCase(str);
Assert.assertEquals("tableTestOfDay", result);
String str1 = "TableTestOfDay";
String result1 = StrUtil.toCamelCase(str1);
Assert.assertEquals("TableTestOfDay", result1);
String abc1d = StrUtil.toCamelCase("abc_1d");
Assert.assertEquals("abc1d", abc1d);
String str2 = "Table-Test-Of-day";
String result2 = StrUtil.toCamelCase(str2, CharUtil.DASHED);
System.out.println(result2);
Assert.assertEquals("tableTestOfDay", result2);
}
@Test
public void toUnderLineCaseTest() {
Dict.create()
.set("Table_Test_Of_day", "table_test_of_day")
.set("_Table_Test_Of_day_", "_table_test_of_day_")
.set("_Table_Test_Of_DAY_", "_table_test_of_DAY_")
.set("_TableTestOfDAYToday", "_table_test_of_DAY_today")
.set("HelloWorld_test", "hello_world_test")
.set("H2", "H2")
.set("H#case", "H#case")
.set("PNLabel", "PN_label")
.set("DEPT_NAME","DEPT_NAME")
.forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key)));
}
@Test
public void containsAnyTest() {
//字符