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
|
|
|
msg = '"%s" not found in "%s"' % (first, second) |
71
|
|
|
self.assertTrue(first in second, msg) |
72
|
|
|
|
73
|
|
|
def assertNotIn(self, first, second): |
74
|
|
|
"""Check that `first` argument is NOT in `second`. |
75
|
|
|
|
76
|
|
|
Just like self.assertFalse(a in b), but with a nicer default message. |
77
|
|
|
This is part of standard library but only from Python 2.7.""" |
78
|
|
|
msg = '"%s" unexpectedly found in "%s"' % (first, second) |
79
|
|
|
self.assertFalse(first in second, msg) |
80
|
|
|
|
81
|
|
|
def assertStringAdded(self, string, before, after, check_str_sum): |
82
|
|
|
"""Check that `string` has been added to `before` to get `after`. |
83
|
|
|
|
84
|
|
|
If the `check_str_sum` argument is True, we check that adding `string` |
85
|
|
|
somewhere in the `before` string gives the `after` string. If the |
86
|
|
|
argument is false, we just check that `string` can be found in `after` |
87
|
|
|
but not in `before`. |
88
|
|
|
""" |
89
|
|
|
if string: |
90
|
|
|
self.assertNotEqual(before, after) |
91
|
|
|
self.assertNotIn(string, before) |
92
|
|
|
self.assertIn(string, after) |
93
|
|
|
# Removing string and checking that we get the original string |
94
|
|
|
begin, mid, end = after.partition(string) |
95
|
|
|
self.assertEqual(mid, string) |
96
|
|
|
if check_str_sum: |
97
|
|
|
self.assertEqual(begin + end, before) |
98
|
|
|
else: |
99
|
|
|
self.assertEqual(before, after) |
100
|
|
|
|
101
|
|
|
if __name__ == '__main__': |
102
|
|
|
print(sys.version_info) |
103
|
|
|
|