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