Created
January 13, 2011 00:27
-
-
Save bohde/777168 to your computer and use it in GitHub Desktop.
An implementation vows.js style macros in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Response(object): | |
def __init__(self, status=200): | |
self.status = status | |
class Client: | |
def get(self, path): | |
return Response(200) | |
def post(self, path): | |
return Response(405) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python tests.py | |
.F. | |
====================================================================== | |
FAIL: GET /resources (no key): should respond with a 403 | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File "/home/joshbohde/code/minstrel/vows.py", line 26, in inner | |
func(self, *args) | |
File "tests.py", line 13, in <lambda> | |
return lambda self, response: self.assertEquals(response.status, status) | |
AssertionError: 200 != 403 | |
---------------------------------------------------------------------- | |
Ran 3 tests in 0.000s | |
FAILED (failures=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from vows import Describe | |
from client import Client | |
def responds_with(status): | |
def topic(self, name): | |
req, path = name.split()[:2] | |
client = Client() | |
return getattr(client, req.lower())(path) | |
def assertStatus(status): | |
return lambda self, response: self.assertEquals(response.status, status) | |
return dict([('topic', topic), | |
('should respond with a %s' % (status), assertStatus(status))]) | |
vows = Describe('Sample Test') | |
TestFour = vows.batch({ | |
'GET /': responds_with(200), | |
'POST /': responds_with(405), | |
'GET /resources (no key)': responds_with(403) | |
}) | |
if __name__ == '__main__': | |
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
class Describe: | |
def __init__(self, name, *bases): | |
if not bases: | |
bases = (unittest.TestCase,) | |
self.bases = tuple(bases) | |
self.name = name | |
def batch(self, contexts): | |
def vow(name, context): | |
topic = context.pop('topic') | |
def inner(self): | |
try: | |
args = topic(self, name) | |
except TypeError: | |
args = topic | |
if args is None: | |
args = [] | |
if not isinstance(args, (list, tuple)): | |
args = [args] | |
for story, func in context.items(): | |
func(self, *args) | |
inner.__doc__ = '%s: %s' % (name, ', '.join(story for story in context)) | |
return inner | |
attrs = (('test %s' % name, | |
vow(name, context)) for name, context in contexts.items()) | |
return type(self.name, self.bases, dict(attrs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment