Skip to content

Commit efccb92

Browse files
committed
1 parent a07c83f commit efccb92

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

codewars_test/test_framework.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ def format_message(message):
1010

1111

1212
def display(type, message, label="", mode=""):
13-
print("\n<{0}:{1}:{2}>{3}".format(
14-
type.upper(), mode.upper(), label, format_message(message)))
13+
print(
14+
"\n<{0}:{1}:{2}>{3}".format(
15+
type.upper(), mode.upper(), label, format_message(message)
16+
)
17+
)
1518

1619

1720
def expect(passed=None, message=None, allow_raise=False):
1821
if passed:
19-
display('PASSED', 'Test Passed')
22+
display("PASSED", "Test Passed")
2023
else:
2124
message = message or "Value is not what was expected"
22-
display('FAILED', message)
25+
display("FAILED", message)
2326
if allow_raise:
2427
raise AssertException(message)
2528

@@ -67,14 +70,17 @@ def expect_no_error(message, function, exception=BaseException):
6770
pass_()
6871

6972

70-
def pass_(): expect(True)
73+
def pass_():
74+
expect(True)
7175

7276

73-
def fail(message): expect(False, message)
77+
def fail(message):
78+
expect(False, message)
7479

7580

7681
def assert_approx_equals(
77-
actual, expected, margin=1e-9, message=None, allow_raise=False):
82+
actual, expected, margin=1e-9, message=None, allow_raise=False
83+
):
7884
msg = "{0} should be close to {1} with absolute or relative margin of {2}"
7985
equals_msg = msg.format(repr(actual), repr(expected), repr(margin))
8086
if message is None:
@@ -85,14 +91,14 @@ def assert_approx_equals(
8591
expect(abs((actual - expected) / div) < margin, message, allow_raise)
8692

8793

88-
'''
94+
"""
8995
Usage:
9096
@describe('describe text')
9197
def describe1():
9298
@it('it text')
9399
def it1():
94100
# some test cases...
95-
'''
101+
"""
96102

97103

98104
def _timed_block_factory(opening_text):
@@ -110,44 +116,49 @@ def wrapper(func):
110116
try:
111117
func()
112118
except AssertionError as e:
113-
display('FAILED', str(e))
119+
display("FAILED", str(e))
114120
except Exception:
115-
fail('Unexpected exception raised')
116-
tb_str = ''.join(format_exception(*exc_info()))
117-
display('ERROR', tb_str)
118-
display('COMPLETEDIN', '{:.2f}'.format((timer() - time) * 1000))
121+
fail("Unexpected exception raised")
122+
tb_str = "".join(format_exception(*exc_info()))
123+
display("ERROR", tb_str)
124+
display("COMPLETEDIN", "{:.2f}".format((timer() - time) * 1000))
119125
if callable(after):
120126
after()
127+
121128
return wrapper
129+
122130
return _timed_block_decorator
123131

124132

125-
describe = _timed_block_factory('DESCRIBE')
126-
it = _timed_block_factory('IT')
133+
describe = _timed_block_factory("DESCRIBE")
134+
it = _timed_block_factory("IT")
127135

128136

129-
'''
137+
"""
130138
Timeout utility
131139
Usage:
132140
@timeout(sec)
133141
def some_tests():
134142
any code block...
135143
Note: Timeout value can be a float.
136-
'''
144+
"""
137145

138146

139147
def timeout(sec):
140148
def wrapper(func):
141149
from multiprocessing import Process
142-
msg = 'Should not throw any exceptions inside timeout'
150+
151+
msg = "Should not throw any exceptions inside timeout"
143152

144153
def wrapped():
145154
expect_no_error(msg, func)
155+
146156
process = Process(target=wrapped)
147157
process.start()
148158
process.join(sec)
149159
if process.is_alive():
150-
fail('Exceeded time limit of {:.3f} seconds'.format(sec))
160+
fail("Exceeded time limit of {:.3f} seconds".format(sec))
151161
process.terminate()
152162
process.join()
163+
153164
return wrapper

0 commit comments

Comments
 (0)