Skip to content

Commit

Permalink
Update 2020-09-01-a022.quiz.message-formatter.md
Browse files Browse the repository at this point in the history
a22 mod
  • Loading branch information
trydofor committed Sep 2, 2020
1 parent 1defda8 commit f9627ae
Showing 1 changed file with 21 additions and 50 deletions.
71 changes: 21 additions & 50 deletions _posts/actions/2020-09-01-a022.quiz.message-formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,27 @@ typora-copy-images-to: ../../../moilioncircle.github.com/images/post/actions/a02

{% raw %}

1.`str``null`时, 返回`null`. `format(null, new Object[]{"a"})`返回`null`
2.`str`不存在`{}`等pattern时,直接返回str. `format("abc", new Object[]{"a"})`返回`abc`
3. 可能存在嵌套括号. `format("{{}}", new Object[]{"a"})`返回`{a}`
4. 使用`\`进行`{`, `}`的转义. `format("\\{\\}", new Object[]{"a"})`返回`{}`
5. 使用`\`转义`\`自身. `format("\\\\", new Object[]{"a"})`返回`\`
6. 可能仅存在`{`或者`}`. `format("{c", new Object[]{"a"})`返回`{c`
7. 当args为null时. `format("{} {} {a}", null)`返回`{null} {null} {a}`

更多测试用例参见[MessageFormatterTest](https://github.com/moilioncircle/message-formatter/blob/master/src/test/java/com/moilioncircle/message/formatter/MessageFormatterTest.java)

``` java
Object[] args = null;
String pattern = "a {} {} b";
assertEquals("a null null b", formatter.format(pattern, args));

String pattern = "a {{}} {{}} b";
assertEquals("a {null} {null} b", formatter.format(pattern, args));

String pattern = "a {{}} {b} b";
assertEquals("a {null} {b} b", formatter.format(pattern, args));

String pattern = "c {a} {b} d";
assertEquals("c {a} {b} d", formatter.format(pattern, args));

String pattern = "c \\{} \\{} d";
assertEquals("c {} {} d", formatter.format(pattern, args));

String pattern = "c \\{\\} \\{\\} d";
assertEquals("c {} {} d", formatter.format(pattern, args));

String pattern = "c \\\\} \\\\} d";
assertEquals("c \\} \\} d", formatter.format(pattern, args));

String pattern = "c \\";
assertEquals("c \\", formatter.format(pattern, args));

String pattern = "c {";
assertEquals("c {", formatter.format(pattern, args));

String pattern = "c {a {a";
assertEquals("c {a {a", formatter.format(pattern, args));

String pattern = "c {\\";
assertEquals("c {\\", formatter.format(pattern, args));

String pattern = "c {\\}";
assertEquals("c {}", formatter.format(pattern, args));

String pattern = "c {\\a";
assertEquals("c {\\a", formatter.format(pattern, args));
1.`str``null`时, 返回`null`. `format(null, new Object[]{"a"})`返回`null`
2.`args``null`或者`args.length < pattern length`时. `format("{} {} {a}", null)`返回`{null} {null} {a}`
3.`str`不存在`{}`等pattern时,直接返回str. `format("abc", new Object[]{"a"})`返回`abc`
4. 可能存在嵌套括号. `format("{{}}", new Object[]{"a"})`返回`{a}`
5. 使用`\`进行`{`, `}`的转义. `format("\\{\\}", new Object[]{"a"})`返回`{}`
6. 使用`\`转义`\`自身. `format("\\\\", new Object[]{"a"})`返回`\`
7. 可能仅存在`{`或者`}`. `format("{c", new Object[]{"a"})`返回`{c`

更多测试用例参见[MessageFormatterTest](https://github.com/moilioncircle/message-formatter/blob/master/src/test/java/com/moilioncircle/message/formatter/MessageFormatterTest.java),部分示例如下。

```java
format(null, new Object[]{"a"}) return null
format("{} {} {a}", null) return "{null} {null} {a}"
format("{} {} {a}", new Object[]{"b"}) return "{b} {null} {a}"
format("{} {} {a}", new Object[]{"b", "c", "d"}) return "{b} {c} {a}"
format("abc", new Object[]{"a"}) return "abc"
format("{{}}", new Object[]{"a"}) return "{a}"
format("\\{\\}", new Object[]{"a"}) return "{}"
format("\\{}", new Object[]{"a"}) return "{}"
format("\\\\", new Object[]{"a"}) return "\"
format("{c", new Object[]{"a"}) return "{c"
```
{% endraw %}
Expand Down

0 comments on commit f9627ae

Please sign in to comment.