CommonTestNewStyleClass2   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 4
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8
2
"""Common logic for unit tests."""
3
import sys
4
5
old_errors = (IOError, OSError)
6
7
try:
8
    NoFileIoError = NoFileOsError = FileNotFoundError
9
except NameError:
10
    NoFileIoError, NoFileOsError = old_errors
11
12
try:
13
    IsDirIoError = IsDirOsError = IsADirectoryError
14
except NameError:
15
    IsDirIoError, IsDirOsError = old_errors
16
17
18
try:
19
    NotDirIoError = NotDirOsError = NotADirectoryError
20
except NameError:
21
    NotDirIoError, NotDirOsError = old_errors
22
23
24
def no_exception(code):
25
    """Helper function to run code and check it works."""
26
    exec(code)
27
28
29
def get_exception(code):
30
    """Helper function to run code and get what it throws."""
31
    try:
32
        no_exception(code)
33
    except:
34
        return sys.exc_info()
35
    return None
36
37
38
class CommonTestOldStyleClass:
39
    """Dummy class for testing purposes."""
40
41
    pass
42
43
44
class CommonTestOldStyleClass2:
45
    """Dummy class for testing purposes."""
46
47
    pass
48
49
50
class CommonTestNewStyleClass(object):
51
    """Dummy class for testing purposes."""
52
53
    pass
54
55
56
class CommonTestNewStyleClass2(object):
57
    """Dummy class for testing purposes."""
58
59
    pass
60
61
62
class TestWithStringFunction(object):
63
    """Unit test class with an helper method."""
64
65
    def assertIn(self, first, second):
66
        """Check that `first` argument is in `second`.
67
68
        Just like self.assertTrue(a in b), but with a nicer default message.
69
        This is part of standard library but only from Python 2.7.
70
        """
71
        msg = '"%s" not found in "%s"' % (first, second)
72
        self.assertTrue(first in second, msg)
73
74
    def assertNotIn(self, first, second):
75
        """Check that `first` argument is NOT in `second`.
76
77
        Just like self.assertFalse(a in b), but with a nicer default message.
78
        This is part of standard library but only from Python 2.7.
79
        """
80
        msg = '"%s" unexpectedly found in "%s"' % (first, second)
81
        self.assertFalse(first in second, msg)
82
83
    def assertStringAdded(self, string, before, after, check_str_sum):
84
        """Check that `string` has been added to `before` to get `after`.
85
86
        If the `check_str_sum` argument is True, we check that adding `string`
87
        somewhere in the `before` string gives the `after` string. If the
88
        argument is false, we just check that `string` can be found in `after`
89
        but not in `before`.
90
        """
91
        if string:
92
            self.assertNotEqual(before, after)
93
            self.assertNotIn(string, before)
94
            self.assertIn(string, after)
95
            # Removing string and checking that we get the original string
96
            begin, mid, end = after.partition(string)
97
            self.assertEqual(mid, string)
98
            if check_str_sum:
99
                self.assertEqual(begin + end, before)
100
        else:
101
            self.assertEqual(before, after)
102
103
if __name__ == '__main__':
104
    print(sys.version_info)
105