mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix format
This commit is contained in:
parent
a1eaec3a1f
commit
0ad2b9e25d
@ -55,8 +55,8 @@ public class StrTemplateTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void namedPlaceholderFormatSequenceTest() {
|
public void namedPlaceholderFormatSequenceTest() {
|
||||||
String text = "select * from #[tableName] where id = #[id]";
|
final String text = "select * from #[tableName] where id = #[id]";
|
||||||
NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed(text).prefix("#[").suffix("]").build();
|
final NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed(text).prefix("#[").suffix("]").build();
|
||||||
Assertions.assertEquals(
|
Assertions.assertEquals(
|
||||||
"select * from user where id = 1001",
|
"select * from user where id = 1001",
|
||||||
strTemplate.formatSequence("user", 1001)
|
strTemplate.formatSequence("user", 1001)
|
||||||
@ -77,8 +77,8 @@ public class StrTemplateTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void namedPlaceholderFormatIndexedTest() {
|
public void namedPlaceholderFormatIndexedTest() {
|
||||||
String text = "select * from #[1] where id = #[2]";
|
final String text = "select * from #[1] where id = #[2]";
|
||||||
NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed(text).prefix("#[").suffix("]").build();
|
final NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed(text).prefix("#[").suffix("]").build();
|
||||||
|
|
||||||
Assertions.assertEquals(
|
Assertions.assertEquals(
|
||||||
"select * from user where id = 1001",
|
"select * from user where id = 1001",
|
||||||
@ -104,10 +104,10 @@ public class StrTemplateTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void namedPlaceholderFormatTest() {
|
public void namedPlaceholderFormatTest() {
|
||||||
String text = "select * from #[tableName] where id = #[id]";
|
final String text = "select * from #[tableName] where id = #[id]";
|
||||||
NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed(text).prefix("#[").suffix("]").build();
|
final NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed(text).prefix("#[").suffix("]").build();
|
||||||
|
|
||||||
Map<String, Object> map = MapUtil.<String, Object>builder().put("tableName", "user").put("id", 1001).build();
|
final Map<String, Object> map = MapUtil.<String, Object>builder().put("tableName", "user").put("id", 1001).build();
|
||||||
Assertions.assertEquals(
|
Assertions.assertEquals(
|
||||||
"select * from user where id = 1001",
|
"select * from user where id = 1001",
|
||||||
strTemplate.format(map)
|
strTemplate.format(map)
|
||||||
@ -214,7 +214,7 @@ public class StrTemplateTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void namedPlaceholderEscapeTest() {
|
public void namedPlaceholderEscapeTest() {
|
||||||
Map<String, Object> map = MapUtil.<String, Object>builder().put("tableName", "user").put("id", 1001).build();
|
final Map<String, Object> map = MapUtil.<String, Object>builder().put("tableName", "user").put("id", 1001).build();
|
||||||
// 转义符
|
// 转义符
|
||||||
String text = "select * from \\#[tableName] where id = \\#[id]";
|
String text = "select * from \\#[tableName] where id = \\#[id]";
|
||||||
NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed(text).prefix("#[").suffix("]").build();
|
NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed(text).prefix("#[").suffix("]").build();
|
||||||
@ -276,10 +276,10 @@ public class StrTemplateTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testSinglePlaceholderFormat(String placeholder, char escape) {
|
private void testSinglePlaceholderFormat(final String placeholder, final char escape) {
|
||||||
// 通常使用
|
// 通常使用
|
||||||
String commonTemplate = "this is " + placeholder + " for " + placeholder;
|
final String commonTemplate = "this is " + placeholder + " for " + placeholder;
|
||||||
SinglePlaceholderStrTemplate template = StrTemplate.of(commonTemplate)
|
final SinglePlaceholderStrTemplate template = StrTemplate.of(commonTemplate)
|
||||||
.placeholder(placeholder)
|
.placeholder(placeholder)
|
||||||
.escape(escape)
|
.escape(escape)
|
||||||
.build();
|
.build();
|
||||||
@ -566,8 +566,8 @@ public class StrTemplateTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void namedPlaceholderMatchesTest() {
|
public void namedPlaceholderMatchesTest() {
|
||||||
NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed("this is {tableName} for {id}").build();
|
final NamedPlaceholderStrTemplate strTemplate = StrTemplate.ofNamed("this is {tableName} for {id}").build();
|
||||||
Supplier<Map<String, String>> mapSupplier = HashMap::new;
|
final Supplier<Map<String, String>> mapSupplier = HashMap::new;
|
||||||
Assertions.assertEquals(MapUtil.builder("tableName", "aaa").put("id", "666").build(), strTemplate.matches("this is aaa for 666", mapSupplier));
|
Assertions.assertEquals(MapUtil.builder("tableName", "aaa").put("id", "666").build(), strTemplate.matches("this is aaa for 666", mapSupplier));
|
||||||
Assertions.assertEquals(MapUtil.builder("tableName", null).put("id", "666").build(), strTemplate.matches("this is for 666", mapSupplier));
|
Assertions.assertEquals(MapUtil.builder("tableName", null).put("id", "666").build(), strTemplate.matches("this is for 666", mapSupplier));
|
||||||
Assertions.assertEquals(MapUtil.builder("tableName", "aaa").put("id", null).build(), strTemplate.matches("this is aaa for ", mapSupplier));
|
Assertions.assertEquals(MapUtil.builder("tableName", "aaa").put("id", null).build(), strTemplate.matches("this is aaa for ", mapSupplier));
|
||||||
@ -575,7 +575,7 @@ public class StrTemplateTest {
|
|||||||
Assertions.assertEquals(Collections.emptyMap(), strTemplate.matches("", mapSupplier));
|
Assertions.assertEquals(Collections.emptyMap(), strTemplate.matches("", mapSupplier));
|
||||||
|
|
||||||
|
|
||||||
Supplier<FormatEntity> beanSupplier = FormatEntity::new;
|
final Supplier<FormatEntity> beanSupplier = FormatEntity::new;
|
||||||
Assertions.assertEquals(new FormatEntity("aaa", 666), strTemplate.matches("this is aaa for 666", beanSupplier));
|
Assertions.assertEquals(new FormatEntity("aaa", 666), strTemplate.matches("this is aaa for 666", beanSupplier));
|
||||||
Assertions.assertEquals(new FormatEntity(null, 666), strTemplate.matches("this is for 666", beanSupplier));
|
Assertions.assertEquals(new FormatEntity(null, 666), strTemplate.matches("this is for 666", beanSupplier));
|
||||||
Assertions.assertEquals(new FormatEntity("aaa", null), strTemplate.matches("this is aaa for ", beanSupplier));
|
Assertions.assertEquals(new FormatEntity("aaa", null), strTemplate.matches("this is aaa for ", beanSupplier));
|
||||||
@ -586,7 +586,7 @@ public class StrTemplateTest {
|
|||||||
@Test
|
@Test
|
||||||
public void featureTest() {
|
public void featureTest() {
|
||||||
// 通常使用
|
// 通常使用
|
||||||
String commonTemplate = "this is {tableName} for {id}";
|
final String commonTemplate = "this is {tableName} for {id}";
|
||||||
// ##### 使用新的策略 替换 默认策略 #####
|
// ##### 使用新的策略 替换 默认策略 #####
|
||||||
NamedPlaceholderStrTemplate template = StrTemplate.ofNamed(commonTemplate)
|
NamedPlaceholderStrTemplate template = StrTemplate.ofNamed(commonTemplate)
|
||||||
.features(StrTemplate.Feature.FORMAT_MISSING_KEY_PRINT_EMPTY, StrTemplate.Feature.MATCH_IGNORE_EMPTY_VALUE)
|
.features(StrTemplate.Feature.FORMAT_MISSING_KEY_PRINT_EMPTY, StrTemplate.Feature.MATCH_IGNORE_EMPTY_VALUE)
|
||||||
@ -600,7 +600,7 @@ public class StrTemplateTest {
|
|||||||
testFeature(template);
|
testFeature(template);
|
||||||
|
|
||||||
// ##### 删除策略 #####
|
// ##### 删除策略 #####
|
||||||
NamedPlaceholderStrTemplate template2 = StrTemplate.ofNamed(commonTemplate)
|
final NamedPlaceholderStrTemplate template2 = StrTemplate.ofNamed(commonTemplate)
|
||||||
.removeFeatures(StrTemplate.Feature.FORMAT_MISSING_KEY_PRINT_WHOLE_PLACEHOLDER)
|
.removeFeatures(StrTemplate.Feature.FORMAT_MISSING_KEY_PRINT_WHOLE_PLACEHOLDER)
|
||||||
.build();
|
.build();
|
||||||
Assertions.assertEquals("this is aaa for 666", template2.format(MapUtil.builder("tableName", "aaa").put("id", "666").build()));
|
Assertions.assertEquals("this is aaa for 666", template2.format(MapUtil.builder("tableName", "aaa").put("id", "666").build()));
|
||||||
@ -667,7 +667,7 @@ public class StrTemplateTest {
|
|||||||
Assertions.assertEquals(MapUtil.builder("tableName", "aaa").put("id", "null").build(), template.matches("this is aaa for null"));
|
Assertions.assertEquals(MapUtil.builder("tableName", "aaa").put("id", "null").build(), template.matches("this is aaa for null"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testFeature(NamedPlaceholderStrTemplate template) {
|
private void testFeature(final NamedPlaceholderStrTemplate template) {
|
||||||
// 格式化
|
// 格式化
|
||||||
Assertions.assertEquals("this is aaa for 666", template.format(MapUtil.builder("tableName", "aaa").put("id", "666").build()));
|
Assertions.assertEquals("this is aaa for 666", template.format(MapUtil.builder("tableName", "aaa").put("id", "666").build()));
|
||||||
Assertions.assertEquals("this is aaa for ", template.format(MapUtil.builder("tableName", "aaa").build()));
|
Assertions.assertEquals("this is aaa for ", template.format(MapUtil.builder("tableName", "aaa").build()));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user