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 assertStringAdded(self, string, before, after, check_str_sum): |
66
|
|
|
"""Check that `string` has been added to `before` to get `after`. |
67
|
|
|
|
68
|
|
|
If the `check_str_sum` argument is True, we check that adding `string` |
69
|
|
|
somewhere in the `before` string gives the `after` string. If the |
70
|
|
|
argument is false, we just check that `string` can be found in `after` |
71
|
|
|
but not in `before`. |
72
|
|
|
""" |
73
|
|
|
if string: |
74
|
|
|
self.assertNotEqual(before, after) |
75
|
|
|
self.assertFalse(string in before, before) |
76
|
|
|
self.assertTrue(string in after, after) |
77
|
|
|
# Removing string and checking that we get the original string |
78
|
|
|
begin, mid, end = after.partition(string) |
79
|
|
|
self.assertEqual(mid, string) |
80
|
|
|
if check_str_sum: |
81
|
|
|
self.assertEqual(begin + end, before) |
82
|
|
|
else: |
83
|
|
|
self.assertEqual(before, after) |
84
|
|
|
|
85
|
|
|
if __name__ == '__main__': |
86
|
|
|
print(sys.version_info) |
87
|
|
|
|