Conditions | 4 |
Total Lines | 55 |
Code Lines | 40 |
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 domain_test |
||
145 | func TestMcq_UpdateAnswer(t *testing.T) { |
||
146 | testCases := []struct { |
||
147 | name string |
||
148 | answers string |
||
149 | oldAnswer string |
||
150 | newAnswer string |
||
151 | expected string |
||
152 | }{ |
||
153 | { |
||
154 | name: "SingleAnswer", |
||
155 | answers: "A", |
||
156 | oldAnswer: "A", |
||
157 | newAnswer: "B", |
||
158 | expected: "B", |
||
159 | }, |
||
160 | { |
||
161 | name: "MultipleAnswers", |
||
162 | answers: "A;B;C", |
||
163 | oldAnswer: "B", |
||
164 | newAnswer: "D", |
||
165 | expected: "A;D;C", |
||
166 | }, |
||
167 | { |
||
168 | name: "NoAnswers", |
||
169 | answers: "", |
||
170 | oldAnswer: "A", |
||
171 | newAnswer: "B", |
||
172 | expected: "", |
||
173 | }, |
||
174 | { |
||
175 | name: "AnswerNotInAnswers", |
||
176 | answers: "A;B;C", |
||
177 | oldAnswer: "D", |
||
178 | newAnswer: "E", |
||
179 | expected: "A;B;C", |
||
180 | }, |
||
181 | { |
||
182 | name: "AnswerNotInAnswers", |
||
183 | answers: "A;B;C", |
||
184 | oldAnswer: "B", |
||
185 | newAnswer: "A", |
||
186 | expected: "A;A;C", |
||
187 | }, |
||
188 | } |
||
189 | |||
190 | for _, tc := range testCases { |
||
191 | t.Run(tc.name, func(t *testing.T) { |
||
192 | mcq := &domain.Mcq{ |
||
193 | Answers: tc.answers, |
||
194 | } |
||
195 | |||
196 | mcq.UpdateAnswer(tc.oldAnswer, tc.newAnswer) |
||
197 | |||
198 | if mcq.Answers != tc.expected { |
||
199 | t.Errorf("Expected Answers to be %s, but got %s", tc.expected, mcq.Answers) |
||
200 | } |
||
204 |