Skip to content

Commit c03ab99

Browse files
committed
create BaseTestCase
1 parent 1beec05 commit c03ab99

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

keen/tests/base_test_case.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,67 @@
1+
import string
2+
import unittest
3+
14
__author__ = 'dkador'
5+
6+
class BaseTestCase(unittest.TestCase):
7+
# --------------------
8+
# Magic for snake_case
9+
# --------------------
10+
11+
class __metaclass__(type):
12+
def __getattr__(cls, name):
13+
"""
14+
15+
This provides snake_case aliases for mixedCase @classmethods.
16+
17+
For instance, if you were to ask for `cls.tear_down_class`, and it
18+
didn't exist, you would transparently get a reference to
19+
`cls.tearDownClass` instead.
20+
21+
"""
22+
23+
name = BaseTestCase.to_mixed(name)
24+
return type.__getattribute__(cls, name)
25+
26+
27+
def __getattr__(self, name):
28+
"""
29+
30+
This provides snake_case aliases for mixedCase instance methods.
31+
32+
For instance, if you were to ask for `self.assert_equal`, and it
33+
didn't exist, you would transparently get a reference to
34+
`self.assertEqual` instead.
35+
36+
"""
37+
38+
mixed_name = BaseTestCase.to_mixed(name)
39+
mixed_attr = None
40+
41+
try:
42+
mixed_attr = object.__getattribute__(self, mixed_name)
43+
except TypeError:
44+
pass
45+
46+
if mixed_attr:
47+
return mixed_attr
48+
49+
return self.__getattribute__(name)
50+
51+
@classmethod
52+
def to_mixed(cls, underscore_input):
53+
"""
54+
55+
Transforms an input string from underscore_separated to mixedCase
56+
57+
mixedCaseLooksLikeThis
58+
59+
"""
60+
61+
word_list = underscore_input.split('_')
62+
word_count = len(word_list)
63+
if word_count > 1:
64+
for i in range(1, word_count):
65+
word_list[i] = string.capwords(word_list[i])
66+
ret = ''.join(word_list)
67+
return ret

0 commit comments

Comments
 (0)