Total Complexity | 43 |
Total Lines | 181 |
Duplicated Lines | 54.14 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Integration tests for the package.""" |
||
151 | class TestStandard: |
||
152 | """Integration tests for standard attribute types.""" |
||
153 | |||
154 | @yorm.attr(status=yorm.types.Boolean) |
||
155 | class StatusDictionary(Dictionary): |
||
156 | pass |
||
157 | |||
158 | def test_decorator(self, tmpdir): |
||
159 | """Verify standard attribute types dump/parse correctly (decorator).""" |
||
160 | tmpdir.chdir() |
||
161 | sample = SampleStandardDecorated('sample') |
||
162 | assert "tmp/default/sample.yml" == sample.__mapper__.path |
||
163 | |||
164 | log("Checking object default values...") |
||
165 | assert {} == sample.object |
||
166 | assert [] == sample.array |
||
167 | assert "" == sample.string |
||
168 | assert 0 == sample.number_int |
||
169 | assert 0.0 == sample.number_real |
||
170 | assert True is sample.truthy |
||
171 | assert False is sample.falsey |
||
172 | assert None is sample.null |
||
173 | |||
174 | log("Changing object values...") |
||
175 | sample.object = {'key2': 'value'} |
||
176 | sample.array = [0, 1, 2] |
||
177 | sample.string = "Hello, world!" |
||
178 | sample.number_int = 42 |
||
179 | sample.number_real = 4.2 |
||
180 | sample.truthy = False |
||
181 | sample.falsey = True |
||
182 | |||
183 | log("Checking file contents...") |
||
184 | assert strip(""" |
||
185 | array: |
||
186 | - 0 |
||
187 | - 1 |
||
188 | - 2 |
||
189 | falsey: true |
||
190 | number_int: 42 |
||
191 | number_real: 4.2 |
||
192 | object: {} |
||
193 | string: Hello, world! |
||
194 | truthy: false |
||
195 | """) == sample.__mapper__.text |
||
196 | |||
197 | log("Changing file contents...") |
||
198 | refresh_file_modification_times() |
||
199 | sample.__mapper__.text = strip(""" |
||
200 | array: [4, 5, 6] |
||
201 | falsey: null |
||
202 | number_int: 42 |
||
203 | number_real: '4.2' |
||
204 | object: {'status': false} |
||
205 | string: "abc" |
||
206 | truthy: null |
||
207 | """) |
||
208 | |||
209 | log("Checking object values...") |
||
210 | assert {'status': False} == sample.object |
||
211 | assert [4, 5, 6] == sample.array |
||
212 | assert "abc" == sample.string |
||
213 | assert 42 == sample.number_int |
||
214 | assert 4.2 == sample.number_real |
||
215 | assert False is sample.truthy |
||
216 | assert False is sample.falsey |
||
217 | |||
218 | View Code Duplication | def test_function(self, tmpdir): |
|
219 | """Verify standard attribute types dump/parse correctly (function).""" |
||
220 | tmpdir.chdir() |
||
221 | _sample = SampleStandard() |
||
222 | attrs = {'object': self.StatusDictionary, |
||
223 | 'array': IntegerList, |
||
224 | 'string': String, |
||
225 | 'number_int': Integer, |
||
226 | 'number_real': Float, |
||
227 | 'truthy': Boolean, |
||
228 | 'falsey': Boolean} |
||
229 | sample = yorm.sync(_sample, "tmp/directory/sample.yml", attrs) |
||
230 | assert "tmp/directory/sample.yml" == sample.__mapper__.path |
||
231 | |||
232 | # check defaults |
||
233 | assert {'status': False} == sample.object |
||
234 | assert [] == sample.array |
||
235 | assert "" == sample.string |
||
236 | assert 0 == sample.number_int |
||
237 | assert 0.0 == sample.number_real |
||
238 | assert True is sample.truthy |
||
239 | assert False is sample.falsey |
||
240 | assert None is sample.null |
||
241 | |||
242 | # change object values |
||
243 | sample.object = {'key': 'value'} |
||
244 | sample.array = [1, 2, 3] |
||
245 | sample.string = "Hello, world!" |
||
246 | sample.number_int = 42 |
||
247 | sample.number_real = 4.2 |
||
248 | sample.truthy = None |
||
249 | sample.falsey = 1 |
||
250 | |||
251 | # check file values |
||
252 | assert strip(""" |
||
253 | array: |
||
254 | - 1 |
||
255 | - 2 |
||
256 | - 3 |
||
257 | falsey: true |
||
258 | number_int: 42 |
||
259 | number_real: 4.2 |
||
260 | object: |
||
261 | status: false |
||
262 | string: Hello, world! |
||
263 | truthy: false |
||
264 | """) == sample.__mapper__.text |
||
265 | |||
266 | View Code Duplication | def test_function_to_json(self, tmpdir): |
|
267 | """Verify standard attribute types dump/parse correctly (function).""" |
||
268 | tmpdir.chdir() |
||
269 | _sample = SampleStandard() |
||
270 | attrs = {'object': self.StatusDictionary, |
||
271 | 'array': IntegerList, |
||
272 | 'string': String, |
||
273 | 'number_int': Integer, |
||
274 | 'number_real': Float, |
||
275 | 'truthy': Boolean, |
||
276 | 'falsey': Boolean} |
||
277 | sample = yorm.sync(_sample, "tmp/directory/sample.json", attrs) |
||
278 | assert "tmp/directory/sample.json" == sample.__mapper__.path |
||
279 | |||
280 | # check defaults |
||
281 | assert {'status': False} == sample.object |
||
282 | assert [] == sample.array |
||
283 | assert "" == sample.string |
||
284 | assert 0 == sample.number_int |
||
285 | assert 0.0 == sample.number_real |
||
286 | assert True is sample.truthy |
||
287 | assert False is sample.falsey |
||
288 | assert None is sample.null |
||
289 | |||
290 | # change object values |
||
291 | sample.object = {'key': 'value'} |
||
292 | sample.array = [1, 2, 3] |
||
293 | sample.string = "Hello, world!" |
||
294 | sample.number_int = 42 |
||
295 | sample.number_real = 4.2 |
||
296 | sample.truthy = None |
||
297 | sample.falsey = 1 |
||
298 | |||
299 | # check file values |
||
300 | assert strip(""" |
||
301 | { |
||
302 | "array": [ |
||
303 | 1, |
||
304 | 2, |
||
305 | 3 |
||
306 | ], |
||
307 | "falsey": true, |
||
308 | "number_int": 42, |
||
309 | "number_real": 4.2, |
||
310 | "object": { |
||
311 | "status": false |
||
312 | }, |
||
313 | "string": "Hello, world!", |
||
314 | "truthy": false |
||
315 | } |
||
316 | """, tabs=2, end='') == sample.__mapper__.text |
||
317 | |||
318 | def test_auto_off(self, tmpdir): |
||
319 | """Verify file updates are disabled with auto save off.""" |
||
320 | tmpdir.chdir() |
||
321 | sample = SampleDecoratedAutoOff() |
||
322 | |||
323 | sample.string = "hello" |
||
324 | assert "" == sample.__mapper__.text |
||
325 | |||
326 | sample.__mapper__.auto_save = True |
||
327 | sample.string = "world" |
||
328 | |||
329 | assert strip(""" |
||
330 | string: world |
||
331 | """) == sample.__mapper__.text |
||
332 | |||
483 |