diff options
author | Mike Crute <mcrute@gmail.com> | 2012-03-09 20:08:09 -0800 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2012-03-09 20:08:09 -0800 |
commit | a256b3ba30044573df1f064e7d2d9c3bdceba05f (patch) | |
tree | fb3b925cde743f97e976bbdd0902220b6c8d3379 | |
download | py-rspec-a256b3ba30044573df1f064e7d2d9c3bdceba05f.tar.bz2 py-rspec-a256b3ba30044573df1f064e7d2d9c3bdceba05f.tar.xz py-rspec-a256b3ba30044573df1f064e7d2d9c3bdceba05f.zip |
-rw-r--r-- | colors.py | 16 | ||||
-rw-r--r-- | matchers.py | 58 | ||||
-rw-r--r-- | rspec.py | 57 |
3 files changed, 131 insertions, 0 deletions
diff --git a/colors.py b/colors.py new file mode 100644 index 0000000..792d86f --- /dev/null +++ b/colors.py | |||
@@ -0,0 +1,16 @@ | |||
1 | def _wrap_with(code): | ||
2 | |||
3 | def inner(text, bold=False): | ||
4 | c = code | ||
5 | if bold: | ||
6 | c = "1;%s" % c | ||
7 | return "\033[%sm%s\033[0m" % (c, text) | ||
8 | return inner | ||
9 | |||
10 | red = _wrap_with('31') | ||
11 | green = _wrap_with('32') | ||
12 | yellow = _wrap_with('33') | ||
13 | blue = _wrap_with('34') | ||
14 | magenta = _wrap_with('35') | ||
15 | cyan = _wrap_with('36') | ||
16 | white = _wrap_with('37') | ||
diff --git a/matchers.py b/matchers.py new file mode 100644 index 0000000..e17d651 --- /dev/null +++ b/matchers.py | |||
@@ -0,0 +1,58 @@ | |||
1 | def equal(expected): | ||
2 | def tester(function, *args, **kwargs): | ||
3 | actual = function(*args, **kwargs) | ||
4 | assert actual == expected, "%r != %r" % (actual, expected) | ||
5 | |||
6 | return tester | ||
7 | |||
8 | |||
9 | def be_none(): | ||
10 | def tester(function, *args, **kwargs): | ||
11 | actual = function(*args, **kwargs) | ||
12 | assert actual is None, "%r is not None" % actual | ||
13 | |||
14 | return tester | ||
15 | |||
16 | |||
17 | def be_true(): | ||
18 | def tester(function, *args, **kwargs): | ||
19 | actual = function(*args, **kwargs) | ||
20 | assert actual is True, "%r is not True" % actual | ||
21 | |||
22 | return tester | ||
23 | |||
24 | |||
25 | def be_false(): | ||
26 | def tester(function, *args, **kwargs): | ||
27 | actual = function(*args, **kwargs) | ||
28 | assert actual is False, "%r is not False" % actual | ||
29 | |||
30 | return tester | ||
31 | |||
32 | |||
33 | def be_a(expected): | ||
34 | def tester(function, *args, **kwargs): | ||
35 | actual = function(*args, **kwargs) | ||
36 | assert isinstance(actual, expected), "%r is not a %r" % (actual, expected) | ||
37 | |||
38 | return tester | ||
39 | |||
40 | |||
41 | def be(expected): | ||
42 | def tester(function, *args, **kwargs): | ||
43 | actual = function(*args, **kwargs) | ||
44 | assert actual is expected, "%r is not %r" % (actual, expected) | ||
45 | |||
46 | return tester | ||
47 | |||
48 | |||
49 | def raise_exception(expected): | ||
50 | def tester(function, *args, **kwargs): | ||
51 | try: | ||
52 | function(*args, **kwargs) | ||
53 | except expected: | ||
54 | pass | ||
55 | else: | ||
56 | raise AssertionError("%s not raised" % expected) | ||
57 | |||
58 | return tester | ||
diff --git a/rspec.py b/rspec.py new file mode 100644 index 0000000..72d0e45 --- /dev/null +++ b/rspec.py | |||
@@ -0,0 +1,57 @@ | |||
1 | import re | ||
2 | import sys | ||
3 | import inspect | ||
4 | from colors import red, green | ||
5 | |||
6 | |||
7 | DEBUG = False | ||
8 | current_function = "" | ||
9 | |||
10 | |||
11 | class 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 | |||
37 | def 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 | |||
48 | def 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) | ||