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

tests.TestParseWithNoSuffixFlag   B

Complexity

Conditions 5

Size

Total Lines 68
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 61
dl 0
loc 68
rs 7.8096
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/v3"
8
)
9
10
func TestParseWithNoSuffixFlag(t *testing.T) {
11
	cases := []struct {
12
		date time.Time
13
		res  string
14
		lang string
15
	}{
16
		{subMinutes(1), "1 minute", "en"},
17
		{subMinutes(2), "2 minutes", "en"},
18
		{subMinutes(3), "3 minutes", "en"},
19
		{subMinutes(4), "4 minutes", "en"},
20
		{subMinutes(5), "5 minutes", "en"},
21
		{subMinutes(10), "10 minutes", "en"},
22
		{subMinutes(11), "11 minutes", "en"},
23
		{subMinutes(20), "20 minutes", "en"},
24
		{subMinutes(21), "21 minutes", "en"},
25
		{subMinutes(22), "22 minutes", "en"},
26
		{subMinutes(30), "30 minutes", "en"},
27
		{subMinutes(31), "31 minutes", "en"},
28
		{subHours(1), "1 hour", "en"},
29
		{subHours(2), "2 hours", "en"},
30
		{subHours(9), "9 hours", "en"},
31
		{subHours(10), "10 hours", "en"},
32
		{subHours(11), "11 hours", "en"},
33
		{subHours(20), "20 hours", "en"},
34
		{subHours(21), "21 hours", "en"},
35
		{subHours(23), "23 hours", "en"},
36
		{subDays(1), "1 day", "en"},
37
		{subDays(2), "2 days", "en"},
38
		{subDays(6), "6 days", "en"},
39
		{subWeeks(1), "1 week", "en"},
40
		{subWeeks(2), "2 weeks", "en"},
41
		{subWeeks(3), "3 weeks", "en"},
42
		{subMonths(1), "1 month", "en"},
43
		{subMonths(2), "2 months", "en"},
44
		{subMonths(3), "3 months", "en"},
45
		{subMonths(4), "4 months", "en"},
46
		{subMonths(5), "5 months", "en"},
47
		{subMonths(6), "6 months", "en"},
48
		{subMonths(7), "7 months", "en"},
49
		{subMonths(8), "8 months", "en"},
50
		{subMonths(9), "9 months", "en"},
51
		{subMonths(10), "10 months", "en"},
52
		{subMonths(11), "11 months", "en"},
53
		{subMonths(12), "1 year", "en"},
54
		{subYears(4), "4 years", "en"},
55
		{subYears(5), "5 years", "en"},
56
		{subYears(11), "11 years", "en"},
57
		{subYears(29), "29 years", "en"},
58
		{subYears(92), "92 years", "en"},
59
		{subMinutes(1), "1 минута", "ru"},
60
		{subMinutes(2), "2 минуты", "ru"},
61
		{subMonths(3), "3 месяца", "ru"},
62
		{subWeeks(2), "2 недели", "ru"},
63
		{subYears(77), "77 лет", "ru"},
64
	}
65
66
	for _, tc := range cases {
67
		t.Run("result for "+tc.date.String(), func(test *testing.T) {
68
			timeago.Reconfigure(timeago.Config{Language: tc.lang})
69
70
			res, err := timeago.Parse(tc.date, "noSuffix")
71
72
			if err != nil {
73
				test.Errorf("Error must be nil, but got %v instead", err)
74
			}
75
76
			if res != tc.res {
77
				test.Errorf("Result must be %s, but got %s instead", tc.res, res)
78
			}
79
		})
80
	}
81
}
82