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
|
import re
import sys
import inspect
from colors import red, green
DEBUG = False
current_function = ""
class Opinion(object):
def __init__(self, thing):
self.thing = thing
self.args = []
self.kwargs = {}
def should(self, matcher):
if not inspect.getargspec(matcher).args:
matcher = matcher()
try:
matcher(self.thing, *self.args, **self.kwargs)
except AssertionError as e:
print(red("".join([current_function, " failed: ", str(e)])))
else:
print(green(current_function))
return self
def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
return self
def tracer(frame, event, arg):
if DEBUG and event == 'call':
print(event, frame, arg, frame.f_code.co_name, frame.f_globals.keys())
for name, function in frame.f_globals.items():
if callable(function) and function.__module__ == "__main__":
frame.f_globals[name] = Opinion(function)
return tracer
def rspec_run(function):
global current_function
ptrace = sys.gettrace()
try:
sys.settrace(tracer)
current_function = re.sub("_", " ", function.__name__)
function()
finally:
sys.settrace(ptrace)
|