1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
from functools import wraps
import sys
from forge import Forge
import unittest
class TestCase(unittest.TestCase):
pass
from io import BytesIO as BinaryObjectClass
assert not hasattr(sys.modules[BinaryObjectClass.__module__], "__file__")
class ForgeTestCase(TestCase):
def setUp(self):
super(ForgeTestCase, self).setUp()
self.forge = Forge()
def tearDown(self):
self.assertNoMoreCalls()
self.forge.verify()
self.forge.restore_all_replacements()
self.forge.reset()
super(ForgeTestCase, self).tearDown()
def assertNoMoreCalls(self):
expected = self.forge.queue.get_expected()
self.assertEqual(len(expected), 0, "len=%d != 0, expected_events=%s, queue=%s" % (len(expected),
repr(expected), repr(self.forge.queue)))
class Method(object):
def __init__(self, signature_string):
self.signature_string = signature_string
self.function = self._to_function()
self.name = self.function.__name__
def get_function(self):
return self.function
def _to_function(self):
code = """def %s: raise NotImplementedError()""" % self.signature_string
d = {}
exec(code, {}, d)
if len(d) != 1:
raise RuntimeError("More than one function created")
[returned] = d.values()
return returned
class ClassMethod(Method):
def get_function(self):
return classmethod(super(ClassMethod, self).get_function())
class StaticMethod(Method):
def get_function(self):
return staticmethod(super(StaticMethod, self).get_function())
def build_new_style_class(methods=()):
return type('NewStyleClass', (object,), _get_class_dict(methods))
def build_old_style_class(methods=()):
return build_new_style_class(methods)
def _get_class_dict(methods):
return dict((method.name, method.get_function())
for method in methods)
def resets_forge_at_end(func):
@wraps(func)
def new_func(self, *args, **kwargs):
func(self, *args, **kwargs)
self.assertNoMoreCalls()
self.forge.reset()
return new_func
class Checkpoint(object):
called = False
def trigger(self):
self.called = True
|