Passed
Pull Request — main (#40)
by Serhii
01:18
created

timeago.TestParseFunctionCanExceptTimePackage   B

Complexity

Conditions 5

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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