Skip to content

Commit 8861cef

Browse files
koebbingdjknack
authored andcommitted
Update FormatterChain.java (#646)
* Update FormatterChain.java Fix "index out of bounds" exception when calling `chain.format(o)` in Formatter implementation. * Tried to make checkstyle happy... * extended FormatterTest for FormatterChain change * fixed FormatterTest for FormatterChain change * added MultipleFormatterTest to achieve better test coverage * fixed FormatterChain * fixed FormatterChain * fixed FormatterChain
1 parent 94f392d commit 8861cef

3 files changed

Lines changed: 65 additions & 4 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/internal/FormatterChain.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ private FormatterChain(final List<Formatter> formatter, final int index) {
6161
* Gets the next formatter in the chain.
6262
* @return The formatter at the next index.
6363
*/
64-
private FormatterChain next() {
65-
return new FormatterChain(chain, index + 1);
64+
private Formatter.Chain next() {
65+
if (index + 1 < chain.size()) {
66+
return new FormatterChain(chain, index + 1);
67+
} else {
68+
return Formatter.NOOP;
69+
}
6670
}
6771

6872
@Override

handlebars/src/test/java/com/github/jknack/handlebars/FormatterTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ public class FormatterTest extends AbstractTest {
1414
@Override
1515
protected void configure(final Handlebars handlebars) {
1616
handlebars.with(new Formatter() {
17-
1817
@Override
1918
public Object format(final Object value, final Chain chain) {
2019
if (value instanceof Date) {
2120
return ((Date) value).getTime();
2221
}
2322
return chain.format(value);
2423
}
25-
2624
});
2725
}
2826

@@ -33,6 +31,13 @@ public void useFormatterTwice() throws IOException {
3331
assertEquals("time is " + now + "/" + now, t.apply(new Date(now)));
3432
}
3533

34+
@Test
35+
public void testFormatterWithoutMatch() throws IOException {
36+
Template t = compile("string is {{this}}");
37+
38+
assertEquals("string is testvalue", t.apply("testvalue"));
39+
}
40+
3641
@Test
3742
public void useTemplateTwice() throws IOException {
3843
Template t = compile("time is {{this}}");
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.github.jknack.handlebars;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.util.Date;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class MultipleFormatterTest extends AbstractTest {
11+
12+
static final long now = System.currentTimeMillis();
13+
14+
@Override
15+
protected void configure(final Handlebars handlebars) {
16+
handlebars.with(new Formatter() {
17+
18+
@Override
19+
public Object format(final Object value, final Chain chain) {
20+
if (value instanceof Date) {
21+
return ((Date) value).getTime();
22+
}
23+
return chain.format(value);
24+
}
25+
26+
}).with(new Formatter() {
27+
@Override
28+
public Object format(final Object value, final Chain chain) {
29+
30+
if (value instanceof Integer) {
31+
return Integer.toHexString((Integer) value);
32+
}
33+
return chain.format(value);
34+
}
35+
36+
});
37+
}
38+
39+
@Test
40+
public void testDateFormatter() throws IOException {
41+
Template t = compile("time is {{this}}");
42+
43+
assertEquals("time is " + now, t.apply(new Date(now)));
44+
}
45+
46+
@Test
47+
public void testIntegerFormatter() throws IOException {
48+
Template t = compile("Hex-Value is {{this}}");
49+
50+
assertEquals("Hex-Value is 10", t.apply(16));
51+
}
52+
}

0 commit comments

Comments
 (0)