mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add test
This commit is contained in:
parent
ca2476400c
commit
7927da3c10
@ -645,11 +645,15 @@ public class CharSequenceUtil extends StrValidator {
|
||||
|
||||
/**
|
||||
* 检查指定字符串中是否只包含给定的字符<br>
|
||||
* 这里的containsOnly并不是必须全部给定的testChars都需要有,而是一个子集。testChars是个限定集合,检查字符串中的字符是否在这个限定集合中。<br>
|
||||
* <ul>
|
||||
* <li>str 是 null,testChars 也是 null,直接返回 true</li>
|
||||
* <li>str 是 null,testChars 不是 null,直接返回 true</li>
|
||||
* <li>str 不是 null,testChars 是 null,直接返回 false</li>
|
||||
* </ul>
|
||||
* <pre>{@code
|
||||
* StrUtil.containsOnly("asdas", 'a', 'd', 's','l'); --> true
|
||||
* }</pre>
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param testChars 检查的字符
|
||||
|
@ -370,4 +370,22 @@ public class CharSequenceUtilTest {
|
||||
final String s3 = CharSequenceUtil.lowerAt(sb, 2);
|
||||
Assertions.assertEquals("KEy", s3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsOnly() {
|
||||
// 测试空字符串
|
||||
Assertions.assertTrue(CharSequenceUtil.containsOnly("", 'a', 'b'));
|
||||
|
||||
// 测试字符串只包含testChars中的字符
|
||||
Assertions.assertTrue(CharSequenceUtil.containsOnly("asdf", 'a', 's', 'd', 'f'));
|
||||
|
||||
// 测试字符串包含testChars中的字符和其它字符
|
||||
Assertions.assertFalse(CharSequenceUtil.containsOnly("asdf123", 'a', 's', 'd', 'f'));
|
||||
|
||||
// 测试字符串不包含testChars中的任何字符
|
||||
Assertions.assertFalse(CharSequenceUtil.containsOnly("hello", 'a', 'b'));
|
||||
|
||||
// 测试字符串为null
|
||||
Assertions.assertTrue(CharSequenceUtil.containsOnly(null, 'a', 'b'));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user