From a256b3ba30044573df1f064e7d2d9c3bdceba05f Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Fri, 9 Mar 2012 20:08:09 -0800 Subject: Initial import --- colors.py | 16 ++++++++++++++++ matchers.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ rspec.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 colors.py create mode 100644 matchers.py create mode 100644 rspec.py diff --git a/colors.py b/colors.py new file mode 100644 index 0000000..792d86f --- /dev/null +++ b/colors.py @@ -0,0 +1,16 @@ +def _wrap_with(code): + + def inner(text, bold=False): + c = code + if bold: + c = "1;%s" % c + return "\033[%sm%s\033[0m" % (c, text) + return inner + +red = _wrap_with('31') +green = _wrap_with('32') +yellow = _wrap_with('33') +blue = _wrap_with('34') +magenta = _wrap_with('35') +cyan = _wrap_with('36') +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 @@ +def equal(expected): + def tester(function, *args, **kwargs): + actual = function(*args, **kwargs) + assert actual == expected, "%r != %r" % (actual, expected) + + return tester + + +def be_none(): + def tester(function, *args, **kwargs): + actual = function(*args, **kwargs) + assert actual is None, "%r is not None" % actual + + return tester + + +def be_true(): + def tester(function, *args, **kwargs): + actual = function(*args, **kwargs) + assert actual is True, "%r is not True" % actual + + return tester + + +def be_false(): + def tester(function, *args, **kwargs): + actual = function(*args, **kwargs) + assert actual is False, "%r is not False" % actual + + return tester + + +def be_a(expected): + def tester(function, *args, **kwargs): + actual = function(*args, **kwargs) + assert isinstance(actual, expected), "%r is not a %r" % (actual, expected) + + return tester + + +def be(expected): + def tester(function, *args, **kwargs): + actual = function(*args, **kwargs) + assert actual is expected, "%r is not %r" % (actual, expected) + + return tester + + +def raise_exception(expected): + def tester(function, *args, **kwargs): + try: + function(*args, **kwargs) + except expected: + pass + else: + raise AssertionError("%s not raised" % expected) + + 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 @@ +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) -- cgit v1.2.3