Skip to content

Instantly share code, notes, and snippets.

@DasIch
Created December 29, 2012 17:05
Show Gist options
  • Save DasIch/4408054 to your computer and use it in GitHub Desktop.
Save DasIch/4408054 to your computer and use it in GitHub Desktop.
# coding: utf-8
import os
import random
import inspect
from functools import partial
from itertools import product
class Context(object):
def __init__(self):
self.fixtures = {}
self.tests = {}
def fixture(self, callable):
self.fixtures[callable.__name__] = Dependent(callable)
def test(self, callable):
self.tests[callable.__name__] = Dependent(callable)
def lookup(self, known, name):
return self.fixtures[name](self, known=known)
def run_tests(self):
for name, test in self.tests.iteritems():
print "testing: %s" % name
try:
list(test(self))
except AssertionError as error:
print error
class Dependent(object):
def __init__(self, callable):
self.callable = callable
@property
def dependencies(self):
return inspect.getargspec(self.callable).args
def lookup(self, known, context, name):
return known.setdefault(name, context.lookup(known, name))
def __call__(self, context, known=None):
if self.dependencies:
if known is None:
known = {}
lookedup_dependencies = map(
partial(self.lookup, known, context),
self.dependencies
)
for args in product(*lookedup_dependencies):
for value in self.callable(*args):
yield value
else:
for value in self.callable():
yield value
context = Context()
@context.fixture
def password():
for _ in xrange(10):
pw = os.urandom(random.randrange(6, 10)).encode("hex")
yield pw
@context.fixture
def pw_hash(password):
h = hash(password)
yield h
@context.test
def test_verify_pw_hash(pw_hash, password):
assert pw_hash == hash(password), "%r != hash(%r)" % (pw_hash, password)
context.run_tests()
@DasIch
Copy link
Author

DasIch commented Dec 29, 2012

Oops. This doesn't work at all. I should have tested this... I will attempt to create a working solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment