def test(base, create): def foo(self): return "foo" def _get_bar(self): return self._bar def _set_bar(self, value): self._bar = value def _del_bar(self): del self._bar bar = property(_get_bar, _set_bar, _del_bar) T = type("T", (base,), dict(foo=foo, bar=bar)) m = MockerExt() t = create(T) m.property(t, "bar") t.bar = "xyz" t.bar >> "abc" del t.bar with m: assert t.foo() == "foo" t.bar = "xyz" b = t.bar assert b == "abc", "got %r" % b del t.bar assert isinstance(t, T) assert T.bar is bar
def test(base, create): def foo(self): return "foo" def _get_bar(self): return self._bar def _set_bar(self, value): self._bar = value def _del_bar(self): del self._bar bar = property(_get_bar, _set_bar, _del_bar) T = type("T", (base, ), dict(foo=foo, bar=bar)) m = MockerExt() t = create(T) m.property(t, "bar") t.bar = "xyz" t.bar >> "abc" del t.bar with m: assert t.foo() == "foo" t.bar = "xyz" b = t.bar assert b == "abc", "got %r" % b del t.bar assert isinstance(t, T) assert T.bar is bar
def test(args, msg): class Test(object): pass def func(arg): pass t = Test() t.func = func m = MockerExt() m.replace(t, "func")(*args) def check(err): assert msg in str(err), "{!r} not in {!r}".format(msg, err) with m, assert_raises(AssertionError, msg=check): t.func(*args)
def test_MockerExt_property_attr(): from Foundation import NSObject class Obj(object): def __init__(self): self.foo = 0 obj = Obj() m = MockerExt() m.property(obj, "foo") obj.foo >> "xyz" obj.foo = "abc" obj.foo >> "def" with m: assert obj.foo == "xyz" obj.foo = "abc" assert obj.foo == "def" assert isinstance(obj, Obj) assert not hasattr(Obj, "foo")
def test_MockerExt_property_value_mock(): from Foundation import NSObject class Obj(object): def __init__(self): self.foo = 0 obj = Obj() m = MockerExt() foo = m.property(obj, "foo") foo.value >> "xyz" foo.value = "abc" foo.value >> "def" del foo.value with m: assert obj.foo == "xyz" obj.foo = "abc" assert obj.foo == "def" del obj.foo assert isinstance(obj, Obj) assert not hasattr(Obj, "foo")
def test_MockerExt_property_multiple_instances(): def eq(a, b): assert a == b, "%r != %r" % (a, b) class T(object): @property def bar(self): return self._bar def setup(m, num): t = T() m.property(t, "bar") t.bar >> num return t m = MockerExt() t1 = setup(m, 1) t2 = setup(m, 2) with m: eq(t1.bar, 1) eq(t2.bar, 2)