Conditions | 6 |
Total Lines | 163 |
Code Lines | 106 |
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 timeago |
||
8 | func TestSetConfigTranslations(t *testing.T) { |
||
9 | cases := []struct { |
||
10 | lang string |
||
11 | name string |
||
12 | time time.Time |
||
13 | expect string |
||
14 | translations []Translation |
||
15 | }{ |
||
16 | { |
||
17 | lang: "en", |
||
18 | name: "changes 'minutes' to 'm'", |
||
19 | time: time.Now().Add(-time.Minute * 2), |
||
20 | expect: "2 m ago", |
||
21 | translations: []Translation{ |
||
22 | { |
||
23 | Language: "en", |
||
24 | Translations: map[string]string{ |
||
25 | "minutes": "m", |
||
26 | }, |
||
27 | }, |
||
28 | }, |
||
29 | }, |
||
30 | { |
||
31 | lang: "en", |
||
32 | name: "changes 'minute' to 'm'", |
||
33 | time: time.Now().Add(-time.Minute), |
||
34 | expect: "1 m ago", |
||
35 | translations: []Translation{ |
||
36 | { |
||
37 | Language: "en", |
||
38 | Translations: map[string]string{ |
||
39 | "minute": "m", |
||
40 | }, |
||
41 | }, |
||
42 | }, |
||
43 | }, |
||
44 | { |
||
45 | lang: "en", |
||
46 | name: "not changes 'minute' to 'm' when output should be '4 minutes ago'", |
||
47 | time: time.Now().Add(-time.Minute * 4), |
||
48 | expect: "4 minutes ago", |
||
49 | translations: []Translation{ |
||
50 | { |
||
51 | Language: "en", |
||
52 | Translations: map[string]string{ |
||
53 | "minute": "m", |
||
54 | }, |
||
55 | }, |
||
56 | }, |
||
57 | }, |
||
58 | { |
||
59 | lang: "en", |
||
60 | name: "you can remove 'ago' suffix by overwriting it to an empty string", |
||
61 | time: time.Now().Add(-time.Minute * 2), |
||
62 | expect: "2 minutes", |
||
63 | translations: []Translation{ |
||
64 | { |
||
65 | Language: "en", |
||
66 | Translations: map[string]string{ |
||
67 | "ago": "", |
||
68 | }, |
||
69 | }, |
||
70 | }, |
||
71 | }, |
||
72 | { |
||
73 | lang: "en", |
||
74 | name: "you can overwrite 'ago' suffix with different value", |
||
75 | time: time.Now().Add(-time.Minute * 2), |
||
76 | expect: "2 minutes passed", |
||
77 | translations: []Translation{ |
||
78 | { |
||
79 | Language: "en", |
||
80 | Translations: map[string]string{ |
||
81 | "ago": "passed", |
||
82 | }, |
||
83 | }, |
||
84 | }, |
||
85 | }, |
||
86 | { |
||
87 | lang: "ru", |
||
88 | name: "you can overwrite 'минуты' with new value", |
||
89 | time: time.Now().Add(-time.Minute * 2), |
||
90 | expect: "2 мин назад", |
||
91 | translations: []Translation{ |
||
92 | { |
||
93 | Language: "ru", |
||
94 | Translations: map[string]string{ |
||
95 | "минуты": "мин", |
||
96 | }, |
||
97 | }, |
||
98 | }, |
||
99 | }, |
||
100 | { |
||
101 | lang: "ru", |
||
102 | name: "you can remove 'назад' suffix by overwriting it to an empty string", |
||
103 | time: time.Now().Add(-time.Minute * 2), |
||
104 | expect: "2 минуты", |
||
105 | translations: []Translation{ |
||
106 | { |
||
107 | Language: "ru", |
||
108 | Translations: map[string]string{ |
||
109 | "назад": "", |
||
110 | }, |
||
111 | }, |
||
112 | }, |
||
113 | }, |
||
114 | { |
||
115 | lang: "ru", |
||
116 | name: "you can overwrite russian days", |
||
117 | time: time.Now().Add(-time.Hour * 24 * 2), |
||
118 | expect: "2 д", |
||
119 | translations: []Translation{ |
||
120 | { |
||
121 | Language: "ru", |
||
122 | Translations: map[string]string{ |
||
123 | "день": "д", |
||
124 | "дня": "д", |
||
125 | "дней": "д", |
||
126 | "назад": "", |
||
127 | }, |
||
128 | }, |
||
129 | }, |
||
130 | }, |
||
131 | } |
||
132 | |||
133 | for _, tc := range cases { |
||
134 | t.Run(tc.name, func(test *testing.T) { |
||
135 | config := Config{ |
||
136 | Language: tc.lang, |
||
137 | Translations: tc.translations, |
||
138 | } |
||
139 | |||
140 | SetConfig(config) |
||
141 | |||
142 | result := Parse(tc.time) |
||
143 | |||
144 | if result != tc.expect { |
||
145 | test.Errorf("Expected '%s', but got '%s'", tc.expect, result) |
||
146 | } |
||
147 | }) |
||
148 | } |
||
149 | |||
150 | t.Run("english does not change if you overwrite russian", func(test *testing.T) { |
||
151 | config := Config{ |
||
152 | Language: "en", |
||
153 | Translations: []Translation{ |
||
154 | { |
||
155 | Language: "ru", |
||
156 | Translations: map[string]string{ |
||
157 | "назад": "xx", |
||
158 | "минут": "xx", |
||
159 | "минуты": "xx", |
||
160 | }, |
||
161 | }, |
||
162 | }, |
||
163 | } |
||
164 | |||
165 | SetConfig(config) |
||
166 | |||
167 | result := Parse(time.Now().Add(-time.Minute * 2)) |
||
168 | |||
169 | if result != "2 minutes ago" { |
||
170 | test.Errorf("Expected '2 minutes ago', but got '%s'", result) |
||
171 | } |
||
174 |