Skip to content

Commit

Permalink
add stable test for 'elapsed' template function
Browse files Browse the repository at this point in the history
  • Loading branch information
julio-lopez committed Dec 8, 2023
1 parent 341dab2 commit 2ad6e5a
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions internal/adapter/handlebars/handlebars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,103 @@ func TestFormatDateHelperElapsedStatic(t *testing.T) {
testString(t, "{{format-date now 'elapsed'}}", context, "15 years ago")
}

func TestFormatDateHelperElapsed(t *testing.T) {
cases := []struct {
elapsed time.Duration
want string
}{
{
elapsed: -12 * time.Minute,
want: "not yet",
},
{
elapsed: 23 * time.Second,
want: "just now",
},
{
elapsed: 1 * time.Minute,
want: "1 minute ago",
},
{
elapsed: 90 * time.Second,
want: "1 minute ago",
},
{
elapsed: 17 * time.Minute,
want: "17 minutes ago",
},
{
elapsed: time.Hour,
want: "1 hour ago",
},
{
elapsed: 3 * time.Hour,
want: "3 hours ago",
},
{
elapsed: 4 * 24 * time.Hour,
want: "4 days ago",
},
{
elapsed: 7 * 24 * time.Hour,
want: "1 week 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: 45 * 24 * time.Hour,
want: "2 months ago",
},
{
elapsed: 60 * 24 * time.Hour,
want: "2 months ago",
},
{
elapsed: 90 * 24 * time.Hour,
want: "3 months ago",
},
{
elapsed: 93 * 24 * time.Hour,
want: "4 months ago",
},
{
elapsed: 180 * 24 * time.Hour,
want: "6 months ago",
},
{
elapsed: 193 * 24 * time.Hour,
want: "7 months ago",
},
{
elapsed: 365 * 24 * time.Hour,
want: "1 year ago",
},
{
elapsed: 367 * 24 * time.Hour,
want: "2 years ago",
},
}

for _, tc := range cases {
t.Run(tc.want, func(t *testing.T) {
templateContext := map[string]interface{}{"now": time.Now().Add(-tc.elapsed)}

// t.Log("test case", i, tc.want)
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")
Expand Down

0 comments on commit 2ad6e5a

Please sign in to comment.