Conditions | 5 |
Total Lines | 56 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | package tests |
||
11 | func TestParseZh(t *testing.T) { |
||
12 | cases := []struct { |
||
13 | date time.Time |
||
14 | res string |
||
15 | }{ |
||
16 | {utils.SubMinutes(1), "1分钟前"}, |
||
17 | {utils.SubMinutes(2), "2分钟前"}, |
||
18 | {utils.SubMinutes(5), "5分钟前"}, |
||
19 | {utils.SubMinutes(9), "9分钟前"}, |
||
20 | {utils.SubMinutes(10), "10分钟前"}, |
||
21 | {utils.SubMinutes(11), "11分钟前"}, |
||
22 | {utils.SubMinutes(20), "20分钟前"}, |
||
23 | {utils.SubMinutes(21), "21分钟前"}, |
||
24 | {utils.SubMinutes(22), "22分钟前"}, |
||
25 | {utils.SubMinutes(30), "30分钟前"}, |
||
26 | {utils.SubMinutes(31), "31分钟前"}, |
||
27 | {utils.SubMinutes(59), "59分钟前"}, |
||
28 | {utils.SubHours(1), "1小时前"}, |
||
29 | {utils.SubHours(2), "2小时前"}, |
||
30 | {utils.SubHours(9), "9小时前"}, |
||
31 | {utils.SubHours(10), "10小时前"}, |
||
32 | {utils.SubHours(11), "11小时前"}, |
||
33 | {utils.SubHours(20), "20小时前"}, |
||
34 | {utils.SubHours(21), "21小时前"}, |
||
35 | {utils.SubHours(23), "23小时前"}, |
||
36 | {utils.SubDays(1), "1天前"}, |
||
37 | {utils.SubDays(2), "2天前"}, |
||
38 | {utils.SubDays(4), "4天前"}, |
||
39 | {utils.SubDays(5), "5天前"}, |
||
40 | {utils.SubDays(6), "6天前"}, |
||
41 | {utils.SubWeeks(1), "1周前"}, |
||
42 | {utils.SubWeeks(2), "2周前"}, |
||
43 | {utils.SubWeeks(3), "3周前"}, |
||
44 | {utils.SubMonths(1), "1个月前"}, |
||
45 | {utils.SubMonths(2), "2个月前"}, |
||
46 | {utils.SubMonths(9), "9个月前"}, |
||
47 | {utils.SubMonths(11), "11个月前"}, |
||
48 | {utils.SubYears(1), "1年前"}, |
||
49 | {utils.SubYears(2), "2年前"}, |
||
50 | {utils.SubYears(21), "21年前"}, |
||
51 | {utils.SubYears(31), "31年前"}, |
||
52 | {utils.SubYears(100), "100年前"}, |
||
53 | } |
||
54 | |||
55 | for _, tc := range cases { |
||
56 | t.Run("result for "+tc.date.String(), func(test *testing.T) { |
||
57 | ago.Reconfigure(ago.Config{Language: ago.LangZh}) |
||
58 | |||
59 | res, err := ago.Parse(tc.date) |
||
60 | |||
61 | if err != nil { |
||
62 | test.Errorf("Error must be nil, but got %v instead", err) |
||
63 | } |
||
64 | |||
65 | if res != tc.res { |
||
66 | test.Errorf("Result must be %s, but got %s instead", tc.res, res) |
||
67 | } |
||
106 |