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

tests/misc/AnnotationsTest.py (17 issues)

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):
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):
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