forked from aaronchall/pythontrainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
119 lines (95 loc) · 3.79 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#! usr/bin/env python
import data
import unittest
import sys
from trainer import check_answer
class BackportTestCase(unittest.TestCase):
"""
Helper class to provide backport support for `assertIn` and `assertIsInstance`
for python 2.6
"""
MAX_LENGTH = 80
def safe_repr(self, obj, short=False):
"""
Backport of unittest.util.safe_repr from python 2.7 for 2.6
https://github.com/python/cpython/blob/cc14aa98797433709230c5522261b0e42d9fb284/Lib/unittest/util.py
"""
try:
result = repr(obj)
except Exception:
result = object.__repr__(obj)
if not short or len(result) < self.MAX_LENGTH:
return result
return result[:self.MAX_LENGTH] + ' [truncated]...'
def assertIn(self, member, container, msg=None):
"""
Just like self.assertTrue(a in b), but with a nicer default message.
Backported from python 2.7 for python 2.6
https://github.com/python/cpython/blob/cc14aa98797433709230c5522261b0e42d9fb284/Lib/unittest/case.py
"""
if member not in container:
standard_msg = '%s not found in %s' % (self.safe_repr(member),
self.safe_repr(container))
self.fail(self._formatMessage(msg, standard_msg))
def assertIsInstance(self, obj, cls, msg=None):
"""
Same as self.assertTrue(isinstance(obj, cls)), with a nicer
default message.
backported from python 2.7 for python 2.6
https://github.com/python/cpython/blob/cc14aa98797433709230c5522261b0e42d9fb284/Lib/unittest/case.py
"""
if not isinstance(obj, cls):
standard_msg = '%s is not an instance of %r' % (self.safe_repr(obj), cls)
self.fail(self._formatMessage(msg, standard_msg))
if sys.version_info < (2, 7):
BaseTestCase = BackportTestCase
else:
BaseTestCase = unittest.TestCase
class DataTest(BaseTestCase):
data = data.DATA
def test_data(self):
self.assertIsInstance(self.data, dict)
def test_data_keys(self):
self.assertIn('keywords', self.data)
self.assertIn('exceptions', self.data)
self.assertIn('functions', self.data)
self.assertIn('modules', self.data)
self.assertIn('datatypes', self.data)
def test_functions(self):
funcs = self.data['functions']
self.assertIn('min', funcs)
self.assertIn('type', funcs)
self.assertIn('enumerate', funcs)
def test_keywords(self):
kw = self.data['keywords']
self.assertIn('with', kw)
self.assertIn('if', kw)
def test_modules(self):
mods = self.data['modules']
self.assertIn('os', mods)
# self.assertIn('os.path', mods)
self.assertIn('string', mods)
def test_datatypes(self):
datatypes = self.data['datatypes']
# self.assertIn('str.maketrans', datatypes) # not in Python 2
self.assertIn('str.lower', datatypes)
def test_exceptions(self):
exceptions = self.data['exceptions']
self.assertIn('BaseException', exceptions)
self.assertIn('OSError', exceptions)
class TrainerTest(BaseTestCase):
def test_check_answer_returns_tuple(self):
res = check_answer('modules', 'some_answer', 'answer')
self.assertIsInstance(res, tuple)
def test_check_answer_returns_true_for_correct_answer(self):
check, answer = check_answer('modules', 'split', 'split')
self.assertTrue(check)
self.assertEqual(answer, 'split')
def test_check_answer_for_datatypes(self):
check, answer = check_answer('datatypes', 'split', 'S.split')
self.assertTrue(check)
self.assertEqual(answer, 'split')
def main():
unittest.main()
if __name__ == '__main__':
main()