Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:04
created

AnnotationsTest

Size/Duplication

Total Lines 61
Duplicated Lines 49.18 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 30
loc 61

9 Methods

Rating   Name   Duplication   Size   Complexity  
B test_with_function() 13 13 5
A test_with_function_without_arguments() 0 6 3
A test_with_lambda() 0 5 3
A dummy() 0 2 1
A Positive.__init__() 6 6 2
A positive() 5 5 2
A test_with_empty_class() 0 9 2
A test_with_custom_type() 17 17 2
A test_empty() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

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:

1
from unittest import TestCase
2
3
from coalib.misc.Annotations import typechain
4
5
6
class AnnotationsTest(TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TestCase does not seem to be defined.
Loading history...
7
8
    def test_empty(self):
9
        with self.assertRaises(TypeError) as ctx:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ctx does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable TypeError does not seem to be defined.
Loading history...
10
            typechain()
11
        self.assertEqual(str(ctx.exception), "No arguments were provided.")
12
13
    def test_with_lambda(self):
14
        function = typechain(lambda x: int(x) > 0)
15
        with self.assertRaises(ValueError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
16
            function("str")
17
        self.assertEqual(function("10"), True)
18
19 View Code Duplication
    def test_with_function(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
        def positive(val):
21
            val = int(val)
22
            if val > 0:
23
                return val
24
            raise ValueError
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
25
        function = typechain(positive, ord)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ord does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable positive does not seem to be defined.
Loading history...
26
        with self.assertRaises(ValueError):
27
            function(0)
28
        with self.assertRaises(ValueError):
29
            function("str")
30
        self.assertEqual(function("10"), 10)
31
        self.assertEqual(function("0"), 48)
32
33
    def test_with_function_without_arguments(self):
34
        def dummy():
35
            return 10
36
        function = typechain(dummy)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable dummy does not seem to be defined.
Loading history...
37
        with self.assertRaises(ValueError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
38
            function(0)
39
40 View Code Duplication
    def test_with_custom_type(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
41
        class Positive:
42
43
            def __init__(self, val):
44
                val = int(val)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable val does not seem to be defined.
Loading history...
45
                if val > 0:
46
                    self.val = val
47
                else:
48
                    raise ValueError
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
49
50
        function = typechain(Positive, ord)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Positive does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable ord does not seem to be defined.
Loading history...
51
        with self.assertRaises(ValueError):
52
            function(0)
53
        obj = function("10")
54
        self.assertIsInstance(obj, Positive)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable obj does not seem to be defined.
Loading history...
55
        self.assertEqual(obj.val, 10)
56
        self.assertEqual(function("0"), 48)
57
58
    def test_with_empty_class(self):
59
        class Dummy:
60
            pass
61
62
        function = typechain(Dummy)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable Dummy does not seem to be defined.
Loading history...
63
        with self.assertRaises(ValueError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ValueError does not seem to be defined.
Loading history...
64
            function("str")
65
        dummy = Dummy()
66
        self.assertEqual(function(dummy), dummy)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable dummy does not seem to be defined.
Loading history...
67