import com.github.jknack.handlebars.Context; import com.github.jknack.handlebars.Handlebars; import com.github.jknack.handlebars.Template; import com.github.jknack.handlebars.context.MapValueResolver; import junit.framework.Assert; import org.apache.commons.lang3.StringEscapeUtils; import org.junit.Test; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; public class MomentJsDateFormatHelperTest { @Test public void testMomentJsDateFormatting () throws IOException, ParseException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss a" ); Date date = sdf.parse( "2014-10-01 12:32:23 PM" ); Template template = compileTemplate( "date is {{dateFormat date format='MMMM Do, YYYY [at] hh:mm a [and] ss [seconds]'}}" ); Context context = createContext( Collections.singletonMap( "date", date ) ); Assert.assertEquals( "date is October 1st, 2014 at 12:32 pm and 23 seconds", template.apply( context ) ); } @Test // RAJ - internally we evaluate the javascript inline using single quotes to surround the format public void testFormatIncludingQuotes1 () throws IOException, ParseException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss a" ); Date date = sdf.parse( "2014-10-01 12:32:23 PM" ); Template template = compileTemplate( "date is {{dateFormat date format='MMMM Do, [\\']YY [\"yay\"]'}}" ); Context context = createContext( Collections.singletonMap( "date", date ) ); // RAJ - By default, Handlebars escapes text for HTML and we have quotes in our generated output String htmlEscapedText = template.apply( context ); String unescapedText = StringEscapeUtils.unescapeHtml4( htmlEscapedText ); Assert.assertEquals( "date is October 1st, '14 \"yay\"", unescapedText ); } @Test // RAJ - internally we evaluate the javascript inline using single quotes to surround the format public void testFormatIncludingQuotes2 () throws IOException, ParseException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss a" ); Date date = sdf.parse( "2014-10-01 12:32:23 PM" ); Template template = compileTemplate( "date is {{dateFormat date format=\"MMMM Do, [']YY [\\\"yay\\\"]\"}}" ); Context context = createContext( Collections.singletonMap( "date", date ) ); // RAJ - By default, Handlebars escapes text for HTML and we have quotes in our generated output String htmlEscapedText = template.apply( context ); String unescapedText = StringEscapeUtils.unescapeHtml4( htmlEscapedText ); Assert.assertEquals( "date is October 1st, '14 \"yay\"", unescapedText ); } @Test public void testFormatAndEmptyFormatString () throws IOException, ParseException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss a" ); Date date = sdf.parse( "2014-10-01 12:32:23 PM" ); Template template = compileTemplate( "date is {{dateFormat date}}" ); Context context = createContext( Collections.singletonMap( "date", date ) ); // RAJ - I have no idea how this should behave under non-US locales, so I'm not sure how useful this assertion is // RAJ - We should probably just check to make sure that something was generated here, rather than the specific format // RAJ - Then again, all of the MMMM stuff above is also locale (language) specific... Assert.assertEquals( "date is 10/01/2014", template.apply( context ) ); } private Context createContext ( Object data ) { return Context.newBuilder( data ) .resolver( MapValueResolver.INSTANCE ) .build(); } private Template compileTemplate ( String templateText ) throws IOException { Handlebars handlebars = new Handlebars(); handlebars.registerHelper( "dateFormat", new MomentJsDateFormatHelper() ); return handlebars.compileInline( templateText ); } }