test: add TestFormatDateHelperElapsed (#359)

improve and extend test coverage in handlebars_test.go, which deals with time and date formatting.
pull/363/head
Julio Lopez 4 months ago committed by GitHub
parent e3e52dfe69
commit 5a2333d0af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -238,11 +238,116 @@ func TestFormatDateHelper(t *testing.T) {
testString(t, "{{format-date now 'timestamp'}}", context, "200911172034")
testString(t, "{{format-date now 'timestamp-unix'}}", context, "1258490098")
testString(t, "{{format-date now 'cust: %Y-%m'}}", context, "cust: 2009-11")
}
func TestFormatDateHelperElapsedYear(t *testing.T) {
year := time.Now().UTC().Year() - 14
context = map[string]interface{}{"now": time.Date(year, 11, 17, 20, 34, 58, 651387237, time.UTC)}
context := map[string]interface{}{"now": time.Date(year, 11, 17, 20, 34, 58, 651387237, time.UTC)}
testString(t, "{{format-date now 'elapsed'}}", context, "14 years ago")
}
func TestFormatDateHelperElapsedViaTimeMultiplication(t *testing.T) {
// test for time being provided in via multiplications on seconds, minutes
// and hours, as expected by github.com/rvflash/elapsed
cases := []struct {
elapsed time.Duration
want string
}{
{
elapsed: -12 * time.Second,
want: "not yet",
},
{
elapsed: time.Second,
want: "just now",
},
{
elapsed: 59 * time.Second,
want: "just now",
},
{
elapsed: 60 * time.Second,
want: "1 minute ago",
},
{
elapsed: 1 * time.Minute,
want: "1 minute ago",
},
{
elapsed: 2 * time.Minute,
want: "2 minutes ago",
},
{
elapsed: 62 * time.Minute,
want: "1 hour ago",
},
{
elapsed: time.Hour,
want: "1 hour ago",
},
{
elapsed: 2 * time.Hour,
want: "2 hours ago",
},
{
elapsed: 24 * time.Hour,
want: "yesterday",
},
{
elapsed: 4 * 24 * time.Hour,
want: "4 days ago",
},
{
elapsed: 7 * 24 * time.Hour,
want: "1 week ago",
},
{
elapsed: 8 * 24 * time.Hour,
want: "2 weeks ago",
},
{
elapsed: 18 * 24 * time.Hour,
want: "3 weeks ago",
},
{
elapsed: 30 * 24 * time.Hour,
want: "1 month ago",
},
{
elapsed: 31 * 24 * time.Hour,
want: "2 months ago",
},
{
elapsed: 60 * 24 * time.Hour,
want: "2 months ago",
},
{
elapsed: 61 * 24 * time.Hour,
want: "3 months ago",
},
{
elapsed: 330 * 24 * time.Hour,
want: "11 months ago",
},
{
elapsed: 331 * 24 * time.Hour,
want: "1 year ago",
},
{
elapsed: 366 * 24 * time.Hour,
want: "2 years ago",
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d_%s", i, tc.want), func(t *testing.T) {
templateContext := map[string]interface{}{"now": time.Now().Add(-tc.elapsed)}
testString(t, "{{format-date now 'elapsed'}}", templateContext, tc.want)
})
}
}
func TestDateHelper(t *testing.T) {
context := map[string]interface{}{"now": time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)}
testString(t, "{{format-date (date \"2009-11-17T20:34:58\") 'timestamp'}}", context, "200911172034")

Loading…
Cancel
Save