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

tests.TestTakeEnWithOnlineFlag   B

Complexity

Conditions 4

Size

Total Lines 63
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 59
dl 0
loc 63
rs 8.3417
c 0
b 0
f 0
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package tests
2
3
import (
4
	"testing"
5
	"time"
6
7
	. "github.com/SerhiiCho/timeago"
8
)
9
10
func TestParseEn(t *testing.T) {
11
	cases := []struct {
12
		date   string
13
		result string
14
	}{
15
		{smallSubTime(-60 * time.Second), "1 minute ago"},
16
		{smallSubTime(-1 * time.Minute), "1 minute ago"},
17
		{smallSubTime(-2 * time.Minute), "2 minutes ago"},
18
		{smallSubTime(-5 * time.Minute), "5 minutes ago"},
19
		{smallSubTime(-9 * time.Minute), "9 minutes ago"},
20
		{smallSubTime(-10 * time.Minute), "10 minutes ago"},
21
		{smallSubTime(-11 * time.Minute), "11 minutes ago"},
22
		{smallSubTime(-20 * time.Minute), "20 minutes ago"},
23
		{smallSubTime(-21 * time.Minute), "21 minutes ago"},
24
		{smallSubTime(-22 * time.Minute), "22 minutes ago"},
25
		{smallSubTime(-30 * time.Minute), "30 minutes ago"},
26
		{smallSubTime(-31 * time.Minute), "31 minutes ago"},
27
		{smallSubTime(-59 * time.Minute), "59 minutes ago"},
28
		{smallSubTime(-60 * time.Minute), "1 hour ago"},
29
		{smallSubTime(-1 * time.Hour), "1 hour ago"},
30
		{smallSubTime(-2 * time.Hour), "2 hours ago"},
31
		{smallSubTime(-9 * time.Hour), "9 hours ago"},
32
		{smallSubTime(-10 * time.Hour), "10 hours ago"},
33
		{smallSubTime(-11 * time.Hour), "11 hours ago"},
34
		{smallSubTime(-20 * time.Hour), "20 hours ago"},
35
		{smallSubTime(-21 * time.Hour), "21 hours ago"},
36
		{smallSubTime(-23 * time.Hour), "23 hours ago"},
37
		{smallSubTime(-24 * time.Hour), "1 day ago"},
38
		{smallSubTime(-30 * time.Hour), "1 day ago"},
39
		{smallSubTime((-24 * 2) * time.Hour), "2 days ago"},
40
		{smallSubTime((-24 * 6) * time.Hour), "6 days ago"},
41
		{smallSubTime((-24 * 7) * time.Hour), "1 week ago"},
42
		{smallSubTime((-24 * 14) * time.Hour), "2 weeks ago"},
43
		{smallSubTime((-24 * 21) * time.Hour), "3 weeks ago"},
44
		{bigSubTime(0, 1, 1), "1 month ago"},
45
		{bigSubTime(0, 2, 1), "2 months ago"},
46
		{bigSubTime(0, 9, 1), "9 months ago"},
47
		{bigSubTime(0, 11, 1), "11 months ago"},
48
		{bigSubTime(0, 12, 1), "1 year ago"},
49
		{bigSubTime(1, 0, 1), "1 year ago"},
50
		{bigSubTime(2, 0, 1), "2 years ago"},
51
		{bigSubTime(21, 0, 1), "21 years ago"},
52
		{bigSubTime(31, 0, 1), "31 years ago"},
53
		{bigSubTime(100, 0, 1), "100 years ago"},
54
	}
55
56
	for _, tc := range cases {
57
		t.Run("result for "+tc.date, func(test *testing.T) {
58
			SetConfig(Config{
59
				Language: "en",
60
				Location: "Europe/Kiev",
61
			})
62
63
			if res := Parse(tc.date); res != tc.result {
64
				test.Errorf("Result must be %s, but got %s instead", tc.result, res)
65
			}
66
		})
67
	}
68
}
69
70
func TestParseEnWithOnlineFlag(t *testing.T) {
71
	cases := []struct {
72
		date   string
73
		result string
74
	}{
75
		{smallSubTime(time.Second * 2), "Online"},
76
		{smallSubTime(time.Second), "Online"},
77
		{smallSubTime(-1 * time.Second), "Online"},
78
		{smallSubTime(-2 * time.Second), "Online"},
79
		{smallSubTime(-9 * time.Second), "Online"},
80
		{smallSubTime(-10 * time.Second), "Online"},
81
		{smallSubTime(-11 * time.Second), "Online"},
82
		{smallSubTime(-20 * time.Second), "Online"},
83
		{smallSubTime(-21 * time.Second), "Online"},
84
		{smallSubTime(-22 * time.Second), "Online"},
85
		{smallSubTime(-30 * time.Second), "Online"},
86
		{smallSubTime(-31 * time.Second), "Online"},
87
		{smallSubTime(-60 * time.Second), "1 minute ago"},
88
		{smallSubTime(-1 * time.Minute), "1 minute ago"},
89
		{smallSubTime(-2 * time.Minute), "2 minutes ago"},
90
		{smallSubTime(-9 * time.Minute), "9 minutes ago"},
91
		{smallSubTime(-10 * time.Minute), "10 minutes ago"},
92
		{smallSubTime(-11 * time.Minute), "11 minutes ago"},
93
		{smallSubTime(-20 * time.Minute), "20 minutes ago"},
94
		{smallSubTime(-21 * time.Minute), "21 minutes ago"},
95
		{smallSubTime(-22 * time.Minute), "22 minutes ago"},
96
		{smallSubTime(-30 * time.Minute), "30 minutes ago"},
97
		{smallSubTime(-31 * time.Minute), "31 minutes ago"},
98
		{smallSubTime(-60 * time.Minute), "1 hour ago"},
99
		{smallSubTime(-1 * time.Hour), "1 hour ago"},
100
		{smallSubTime(-2 * time.Hour), "2 hours ago"},
101
		{smallSubTime(-9 * time.Hour), "9 hours ago"},
102
		{smallSubTime(-10 * time.Hour), "10 hours ago"},
103
		{smallSubTime(-11 * time.Hour), "11 hours ago"},
104
		{smallSubTime(-20 * time.Hour), "20 hours ago"},
105
		{smallSubTime(-21 * time.Hour), "21 hours ago"},
106
		{smallSubTime(-23 * time.Hour), "23 hours ago"},
107
		{smallSubTime(-24 * time.Hour), "1 day ago"},
108
		{smallSubTime(-30 * time.Hour), "1 day ago"},
109
		{smallSubTime((-24 * 2) * time.Hour), "2 days ago"},
110
		{smallSubTime((-24 * 6) * time.Hour), "6 days ago"},
111
		{smallSubTime((-24 * 7) * time.Hour), "1 week ago"},
112
		{smallSubTime((-24 * 14) * time.Hour), "2 weeks ago"},
113
		{smallSubTime((-24 * 21) * time.Hour), "3 weeks ago"},
114
		{bigSubTime(0, 1, 1), "1 month ago"},
115
		{bigSubTime(0, 2, 1), "2 months ago"},
116
		{bigSubTime(0, 9, 1), "9 months ago"},
117
		{bigSubTime(0, 11, 1), "11 months ago"},
118
		{bigSubTime(0, 12, 1), "1 year ago"},
119
		{bigSubTime(1, 0, 1), "1 year ago"},
120
		{bigSubTime(2, 0, 1), "2 years ago"},
121
		{bigSubTime(21, 0, 1), "21 years ago"},
122
		{bigSubTime(31, 0, 1), "31 years ago"},
123
		{bigSubTime(100, 0, 1), "100 years ago"},
124
	}
125
126
	for _, tc := range cases {
127
		t.Run("result for "+tc.date, func(test *testing.T) {
128
129
			SetConfig(Config{
130
				Language: "en",
131
			})
132
133
			if res := Parse(tc.date, "online"); res != tc.result {
134
				test.Errorf("Result must be %s, but got %s instead", tc.result, res)
135
			}
136
		})
137
	}
138
}
139
140
func TestParseEnWithSeconds(t *testing.T) {
141
	cases := []struct {
142
		date   string
143
		result []string
144
	}{
145
		{smallSubTime(time.Second * 2), []string{"0 seconds ago", "1 second ago"}},
146
		{smallSubTime(time.Second), []string{"0 seconds ago", "1 second ago"}},
147
		{smallSubTime(-1 * time.Second), []string{"1 second ago", "2 seconds ago"}},
148
		{smallSubTime(-2 * time.Second), []string{"2 seconds ago", "3 seconds ago"}},
149
		{smallSubTime(-9 * time.Second), []string{"9 seconds ago", "10 seconds ago"}},
150
		{smallSubTime(-10 * time.Second), []string{"10 seconds ago", "11 seconds ago"}},
151
		{smallSubTime(-11 * time.Second), []string{"11 seconds ago", "12 seconds ago"}},
152
		{smallSubTime(-20 * time.Second), []string{"20 seconds ago", "21 seconds ago"}},
153
		{smallSubTime(-21 * time.Second), []string{"21 seconds ago", "22 seconds ago"}},
154
		{smallSubTime(-22 * time.Second), []string{"22 seconds ago", "23 seconds ago"}},
155
		{smallSubTime(-30 * time.Second), []string{"30 seconds ago", "31 seconds ago"}},
156
		{smallSubTime(-31 * time.Second), []string{"31 seconds ago", "32 seconds ago"}},
157
	}
158
159
	for _, tc := range cases {
160
		t.Run("result for "+tc.date, func(test *testing.T) {
161
			SetConfig(Config{
162
				Language: "en",
163
				Location: "Europe/Kiev",
164
			})
165
166
			if res := Parse(tc.date); res != tc.result[0] && res != tc.result[1] {
167
				test.Errorf("Result must be %s or %s, but got %s instead", tc.result[0], tc.result[1], res)
168
			}
169
		})
170
	}
171
}
172