time_test.go 764 Bytes
Newer Older
Patrick Devine's avatar
Patrick Devine committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package format

import (
	"testing"
	"time"
)

func assertEqual(t *testing.T, a interface{}, b interface{}) {
	if a != b {
		t.Errorf("Assert failed, expected %v, got %v", b, a)
	}
}

func TestHumanTime(t *testing.T) {
	now := time.Now()

	t.Run("zero value", func(t *testing.T) {
		assertEqual(t, HumanTime(time.Time{}, "never"), "never")
	})
Michael Yang's avatar
Michael Yang committed
20

Patrick Devine's avatar
Patrick Devine committed
21
22
23
24
	t.Run("time in the future", func(t *testing.T) {
		v := now.Add(48 * time.Hour)
		assertEqual(t, HumanTime(v, ""), "2 days from now")
	})
Michael Yang's avatar
Michael Yang committed
25

Patrick Devine's avatar
Patrick Devine committed
26
27
28
29
30
	t.Run("time in the past", func(t *testing.T) {
		v := now.Add(-48 * time.Hour)
		assertEqual(t, HumanTime(v, ""), "2 days ago")
	})

Michael Yang's avatar
Michael Yang committed
31
32
33
34
	t.Run("soon", func(t *testing.T) {
		v := now.Add(800*time.Millisecond)
		assertEqual(t, HumanTime(v, ""), "Less than a second from now")
	})
Patrick Devine's avatar
Patrick Devine committed
35
}