Passed
Push — master ( b486cf...8123a4 )
by Serhii
02:01 queued 28s
created

timeago.TestGetOption   A

Complexity

Conditions 5

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nop 1
dl 0
loc 19
rs 9.1832
c 0
b 0
f 0
1
package timeago
2
3
import (
4
	"testing"
5
	"time"
6
)
7
8
func TestGetWords(t *testing.T) {
9
	cases := []struct {
10
		timeKind string
11
		num      int
12
		result   string
13
		lang     string
14
	}{
15
		// english
16
		{"days", 11, "11 days ago", "en"},
17
		{"days", 21, "21 days ago", "en"},
18
		{"seconds", 30, "30 seconds ago", "en"},
19
		{"seconds", 31, "31 seconds ago", "en"},
20
		{"hours", 10, "10 hours ago", "en"},
21
		{"years", 2, "2 years ago", "en"},
22
		// russian
23
		{"hours", 5, "5 часов назад", "ru"},
24
		{"days", 11, "11 дней назад", "ru"},
25
		{"years", 21, "21 год назад", "ru"},
26
		{"minutes", 59, "59 минут назад", "ru"},
27
	}
28
29
	for _, tc := range cases {
30
		t.Run(tc.result, func(test *testing.T) {
31
			SetConfig(Config{
32
				Language: tc.lang,
33
				Location: "Europe/Kiev",
34
			})
35
36
			if res := getWords(tc.timeKind, tc.num); res != tc.result {
37
				test.Errorf("Result must be `%s` but got `%s` instead", tc.result, res)
38
			}
39
		})
40
	}
41
}
42
43
func TestGetLastNumberDigit(t *testing.T) {
44
	cases := []struct {
45
		number int
46
		result int
47
		name   string
48
	}{
49
		{0, 0, "must return 0"},
50
		{1, 1, "must return 1"},
51
		{9, 9, "must return 9"},
52
		{20, 0, "must return 0"},
53
		{253, 3, "must return 3"},
54
		{23423252, 2, "must return 2"},
55
		{223424342325, 5, "must return 5"},
56
		{23423521562348, 8, "must return 8"},
57
	}
58
59
	for _, tc := range cases {
60
		t.Run(tc.name, func(test *testing.T) {
61
			if res := getLastNumberDigit(tc.number); res != tc.result {
62
				test.Errorf("Result must be %d, but got %d instead", tc.result, res)
63
			}
64
		})
65
	}
66
}
67
68
func TestParseFunctionCanExceptTimestamp(t *testing.T) {
69
	cases := []struct {
70
		timestamp int
71
		result    string
72
	}{
73
		{getTimestampOfPastDate(time.Minute), "1 minute ago"},
74
		{getTimestampOfPastDate(time.Minute * 5), "5 minutes ago"},
75
		{getTimestampOfPastDate(time.Hour), "1 hour ago"},
76
		{getTimestampOfPastDate(time.Hour * 3), "3 hours ago"},
77
		{getTimestampOfPastDate(time.Hour * 24), "1 day ago"},
78
		{getTimestampOfPastDate(time.Hour * 48), "2 days ago"},
79
	}
80
81
	SetConfig(Config{
82
		Language: "en",
83
	})
84
85
	for _, tc := range cases {
86
		t.Run(tc.result, func(test *testing.T) {
87
			if res := Parse(tc.timestamp); res != tc.result {
88
				test.Errorf("Result must be %v, but got %v instead", tc.result, res)
89
			}
90
		})
91
	}
92
}
93
94
func TestParseFunctionCanExceptTimePackage(t *testing.T) {
95
	cases := []struct {
96
		time   time.Time
97
		result string
98
	}{
99
		{time.Now().Add(-time.Minute), "1 minute ago"},
100
		{time.Now().Add(-time.Minute * 2), "2 minutes ago"},
101
		{time.Now().Add(-time.Minute * 3), "3 minutes ago"},
102
		{time.Now().Add(-time.Minute * 4), "4 minutes ago"},
103
		{time.Now().Add(-time.Minute * 5), "5 minutes ago"},
104
		{time.Now().Add(-time.Minute * 6), "6 minutes ago"},
105
		{time.Now().Add(-time.Hour * 7), "7 hours ago"},
106
		{time.Now().Add(-time.Hour * 8), "8 hours ago"},
107
		{time.Now().Add(-time.Hour * 9), "9 hours ago"},
108
		{time.Now().Add(-time.Hour * 10), "10 hours ago"},
109
		{time.Now().Add(-time.Hour * 11), "11 hours ago"},
110
	}
111
112
	SetConfig(Config{
113
		Language: "en",
114
	})
115
116
	for _, tc := range cases {
117
		t.Run(tc.result, func(test *testing.T) {
118
			if res := Parse(tc.time); res != tc.result {
119
				test.Errorf("Result must be %v, but got %v instead", tc.result, res)
120
			}
121
		})
122
	}
123
}
124