예제 #1
0
 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
예제 #2
0
    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
예제 #3
0
 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)
예제 #4
0
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")
예제 #5
0
    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)
예제 #6
0
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")
예제 #7
0
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")
예제 #8
0
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")
예제 #9
0
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)