Passed
Push — v3.0.0 ( d631d6...c0d1f3 )
by Serhii
01:16
created

timeago.TestParseFunctionCanExceptTimestamp   B

Complexity

Conditions 5

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
dl 0
loc 32
rs 8.8133
c 0
b 0
f 0
nop 1
1
package timeago
2
3
import (
4
	"testing"
5
	"time"
6
)
7
8
func TestParseFunctionCanExceptTimestamp(t *testing.T) {
9
	cases := []struct {
10
		date int
11
		res  string
12
	}{
13
		{getTimestampOfPastDate(time.Minute), "1 minute ago"},
14
		{getTimestampOfPastDate(time.Minute * 5), "5 minutes ago"},
15
		{getTimestampOfPastDate(time.Hour), "1 hour ago"},
16
		{getTimestampOfPastDate(time.Hour * 3), "3 hours ago"},
17
		{getTimestampOfPastDate(time.Hour * 5), "5 hours ago"},
18
		{getTimestampOfPastDate(time.Hour * 24), "1 day ago"},
19
		{getTimestampOfPastDate(time.Hour * 24 * 2), "2 days ago"},
20
		{getTimestampOfPastDate(time.Hour * 24 * 3), "3 days ago"},
21
		{getTimestampOfPastDate(time.Hour * 24 * 4), "4 days ago"},
22
		{getTimestampOfPastDate(time.Hour * 24 * 5), "5 days ago"},
23
		{getTimestampOfPastDate(time.Hour * 24 * 6), "6 days ago"},
24
		{getTimestampOfPastDate(time.Hour * 24 * 7), "1 week ago"},
25
	}
26
27
	ClearCache()
28
	Configure(Config{Language: "en"})
29
30
	for _, tc := range cases {
31
		t.Run(tc.res, func(t *testing.T) {
32
			res, err := Parse(tc.date)
33
34
			if err != nil {
35
				t.Errorf("Error must be nil, but got %q instead", err)
36
			}
37
38
			if res != tc.res {
39
				t.Errorf("Result must be %q, but got %q instead", tc.res, res)
40
			}
41
		})
42
	}
43
}
44
45
func TestParseFunctionCanExceptTimePackage(t *testing.T) {
46
	cases := []struct {
47
		date time.Time
48
		res  string
49
	}{
50
		{time.Now().Add(-time.Minute), "1 minute ago"},
51
		{time.Now().Add(-time.Minute * 2), "2 minutes ago"},
52
		{time.Now().Add(-time.Minute * 3), "3 minutes ago"},
53
		{time.Now().Add(-time.Minute * 4), "4 minutes ago"},
54
		{time.Now().Add(-time.Minute * 5), "5 minutes ago"},
55
		{time.Now().Add(-time.Minute * 6), "6 minutes ago"},
56
		{time.Now().Add(-time.Hour * 7), "7 hours ago"},
57
		{time.Now().Add(-time.Hour * 8), "8 hours ago"},
58
		{time.Now().Add(-time.Hour * 9), "9 hours ago"},
59
		{time.Now().Add(-time.Hour * 10), "10 hours ago"},
60
		{time.Now().Add(-time.Hour * 11), "11 hours ago"},
61
	}
62
63
	ClearCache()
64
	Configure(Config{Language: "en"})
65
66
	for _, tc := range cases {
67
		t.Run("Test for date "+tc.date.String(), func(t *testing.T) {
68
			res, err := Parse(tc.date)
69
70
			if err != nil {
71
				t.Errorf("Error must be nil, but got %q instead", err)
72
			}
73
74
			if res != tc.res {
75
				t.Errorf("Result must be %q, but got %q instead", tc.res, res)
76
			}
77
		})
78
	}
79
}
80
81
func TestParseFuncWillCalculateIntervalToFutureDate(t *testing.T) {
82
	testCases := []struct {
83
		date time.Time
84
		res  string
85
	}{
86
		{time.Now().Add(time.Minute * 2), "2 minutes"},
87
		{time.Now().Add(time.Minute * 5), "5 minutes"},
88
		{time.Now().Add(time.Minute * 10), "10 minutes"},
89
		{time.Now().Add(time.Hour), "1 hour"},
90
		{time.Now().Add(time.Hour * 24), "1 day"},
91
		{time.Now().Add(time.Hour * 48), "2 days"},
92
	}
93
94
	ClearCache()
95
	Configure(Config{Language: "en"})
96
97
	for _, tc := range testCases {
98
		t.Run("Test for date: "+tc.date.String(), func(t *testing.T) {
99
			res, err := Parse(tc.date)
100
101
			if err != nil {
102
				t.Errorf("Error must be nil, but got %q instead", err)
103
			}
104
105
			if res != tc.res {
106
				t.Errorf("Result must be %q, but got %q instead", tc.res, res)
107
			}
108
		})
109
	}
110
}
111