From c988de9bc85a1285b29a40ca716160e4d1b95c74 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 26 Dec 2025 16:17:15 +0800 Subject: [PATCH] =?UTF-8?q?`CharUtil.toCloseByNumber`=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=8B=E8=BE=B9=E7=95=8C=E6=A3=80=E6=9F=A5=EF=BC=88pr#1421@G?= =?UTF-8?q?itee=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/v7/core/text/CharUtil.java | 2 +- .../cn/hutool/v7/core/text/CharUtilTest.java | 23 ++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/v7/core/text/CharUtil.java b/hutool-core/src/main/java/cn/hutool/v7/core/text/CharUtil.java index 1f80d7a7d8..18589bfd11 100644 --- a/hutool-core/src/main/java/cn/hutool/v7/core/text/CharUtil.java +++ b/hutool-core/src/main/java/cn/hutool/v7/core/text/CharUtil.java @@ -419,7 +419,7 @@ public class CharUtil implements CharPool { * @since 5.6.2 */ public static char toCloseByNumber(final int number) { - if (number > 20) { + if (number < 1 || number > 20) { throw new IllegalArgumentException("Number must be [1-20]"); } return (char) ('①' + number - 1); diff --git a/hutool-core/src/test/java/cn/hutool/v7/core/text/CharUtilTest.java b/hutool-core/src/test/java/cn/hutool/v7/core/text/CharUtilTest.java index 8ab5fd34d8..23c226a3b2 100644 --- a/hutool-core/src/test/java/cn/hutool/v7/core/text/CharUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/v7/core/text/CharUtilTest.java @@ -19,7 +19,7 @@ package cn.hutool.v7.core.text; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class CharUtilTest { @@ -27,7 +27,7 @@ public class CharUtilTest { public void trimTest() { //此字符串中的第一个字符为不可见字符: '\u202a' final String str = "‪C:/Users/maple/Desktop/tone.txt"; - Assertions.assertEquals('\u202a', str.charAt(0)); + assertEquals('\u202a', str.charAt(0)); Assertions.assertTrue(CharUtil.isBlankChar(str.charAt(0))); } @@ -66,16 +66,16 @@ public class CharUtilTest { @Test public void toCloseCharTest(){ - Assertions.assertEquals('②', CharUtil.toCloseChar('2')); - Assertions.assertEquals('Ⓜ', CharUtil.toCloseChar('M')); - Assertions.assertEquals('ⓡ', CharUtil.toCloseChar('r')); + assertEquals('②', CharUtil.toCloseChar('2')); + assertEquals('Ⓜ', CharUtil.toCloseChar('M')); + assertEquals('ⓡ', CharUtil.toCloseChar('r')); } @Test public void toCloseByNumberTest(){ - Assertions.assertEquals('②', CharUtil.toCloseByNumber(2)); - Assertions.assertEquals('⑫', CharUtil.toCloseByNumber(12)); - Assertions.assertEquals('⑳', CharUtil.toCloseByNumber(20)); + assertEquals('②', CharUtil.toCloseByNumber(2)); + assertEquals('⑫', CharUtil.toCloseByNumber(12)); + assertEquals('⑳', CharUtil.toCloseByNumber(20)); } @SuppressWarnings("UnnecessaryUnicodeEscape") @@ -87,4 +87,11 @@ public class CharUtilTest { c = '\u2800'; Assertions.assertTrue(CharUtil.isBlankChar(c)); } + + @Test + public void issueIDFNHETest(){ + //Console.log(CharUtil.toCloseByNumber(0)); //此时会打印"⑟" + assertThrows(IllegalArgumentException.class, () -> CharUtil.toCloseByNumber(0)); + assertThrows(IllegalArgumentException.class, () -> CharUtil.toCloseByNumber(-1)); + } }