summaryrefslogtreecommitdiff
path: root/rspec.py
diff options
context:
space:
mode:
Diffstat (limited to 'rspec.py')
-rw-r--r--rspec.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/rspec.py b/rspec.py
new file mode 100644
index 0000000..72d0e45
--- /dev/null
+++ b/rspec.py
@@ -0,0 +1,57 @@
1import re
2import sys
3import inspect
4from colors import red, green
5
6
7DEBUG = False
8current_function = ""
9
10
11class Opinion(object):
12
13 def __init__(self, thing):
14 self.thing = thing
15 self.args = []
16 self.kwargs = {}
17
18 def should(self, matcher):
19 if not inspect.getargspec(matcher).args:
20 matcher = matcher()
21
22 try:
23 matcher(self.thing, *self.args, **self.kwargs)
24 except AssertionError as e:
25 print(red("".join([current_function, " failed: ", str(e)])))
26 else:
27 print(green(current_function))
28
29 return self
30
31 def __call__(self, *args, **kwargs):
32 self.args = args
33 self.kwargs = kwargs
34 return self
35
36
37def tracer(frame, event, arg):
38 if DEBUG and event == 'call':
39 print(event, frame, arg, frame.f_code.co_name, frame.f_globals.keys())
40
41 for name, function in frame.f_globals.items():
42 if callable(function) and function.__module__ == "__main__":
43 frame.f_globals[name] = Opinion(function)
44
45 return tracer
46
47
48def rspec_run(function):
49 global current_function
50 ptrace = sys.gettrace()
51
52 try:
53 sys.settrace(tracer)
54 current_function = re.sub("_", " ", function.__name__)
55 function()
56 finally:
57 sys.settrace(ptrace)