This commit is contained in:
Looly
2024-08-26 14:52:07 +08:00
parent d8e3c7157d
commit 4e53a9c0b5
14 changed files with 195 additions and 103 deletions

View File

@@ -12,6 +12,8 @@
package org.dromara.hutool.core.text;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.dromara.hutool.core.text.placeholder.StrFormatter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -62,4 +64,20 @@ public class StrFormatterTest {
final String result3 = StrFormatter.formatWith("this is \\\\$$$ for $$$", "$$$", "a", "b");
Assertions.assertEquals("this is \\a for b", result3);
}
@Test
void formatByBeanTest() {
final User user = new User("张三", 18, true);
final String s = StrFormatter.formatByBean("User name: {name}, age: {age}, gender: {gender}", user, true);
Assertions.assertEquals("User name: 张三, age: 18, gender: true", s);
}
@Data
@AllArgsConstructor
private static class User{
private String name;
private int age;
private boolean gender;
}
}

View File

@@ -89,10 +89,10 @@ public class StrUtilTest {
@Test
public void formatTest() {
final String template = "你好,我是{name},我的电话是:{phone}";
final String result = StrUtil.format(template, Dict.of().set("name", "张三").set("phone", "13888881111"));
final String result = StrUtil.formatByMap(template, Dict.of().set("name", "张三").set("phone", "13888881111"));
Assertions.assertEquals("你好我是张三我的电话是13888881111", result);
final String result2 = StrUtil.format(template, Dict.of().set("name", "张三").set("phone", null));
final String result2 = StrUtil.formatByMap(template, Dict.of().set("name", "张三").set("phone", null));
Assertions.assertEquals("你好,我是张三,我的电话是:{phone}", result2);
}