Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ private FormatterChain(final List<Formatter> formatter, final int index) {
* Gets the next formatter in the chain.
* @return The formatter at the next index.
*/
private FormatterChain next() {
return new FormatterChain(chain, index + 1);
private Formatter.Chain next() {
if (index + 1 < chain.size()) {
return new FormatterChain(chain, index + 1);
} else {
return Formatter.NOOP;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ public class FormatterTest extends AbstractTest {
@Override
protected void configure(final Handlebars handlebars) {
handlebars.with(new Formatter() {

@Override
public Object format(final Object value, final Chain chain) {
if (value instanceof Date) {
return ((Date) value).getTime();
}
return chain.format(value);
}

});
}

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

@Test
public void testFormatterWithoutMatch() throws IOException {
Template t = compile("string is {{this}}");

assertEquals("string is testvalue", t.apply("testvalue"));
}

@Test
public void useTemplateTwice() throws IOException {
Template t = compile("time is {{this}}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.jknack.handlebars;

import org.junit.Test;

import java.io.IOException;
import java.util.Date;

import static org.junit.Assert.assertEquals;

public class MultipleFormatterTest extends AbstractTest {

static final long now = System.currentTimeMillis();

@Override
protected void configure(final Handlebars handlebars) {
handlebars.with(new Formatter() {

@Override
public Object format(final Object value, final Chain chain) {
if (value instanceof Date) {
return ((Date) value).getTime();
}
return chain.format(value);
}

}).with(new Formatter() {
@Override
public Object format(final Object value, final Chain chain) {

if (value instanceof Integer) {
return Integer.toHexString((Integer) value);
}
return chain.format(value);
}

});
}

@Test
public void testDateFormatter() throws IOException {
Template t = compile("time is {{this}}");

assertEquals("time is " + now, t.apply(new Date(now)));
}

@Test
public void testIntegerFormatter() throws IOException {
Template t = compile("Hex-Value is {{this}}");

assertEquals("Hex-Value is 10", t.apply(16));
}
}