def test_mul(self):
     other = mock.MagicMock()
     self.assertEqual(self.proxy * other, self.obj * other)
     self.assertEqual(self.proxy.__mul__(other), self.obj * other)
     self.assertEqual(proxy(4.5) * 2, 9.0)
     self.assertEqual(proxy(5) * 2, 10)
     self.assertEqual(proxy("foo") * 2, "foofoo")
 def test_rmod(self):
     """ Verify that __rmod__ is redirected to the proxiee. """
     other = mock.Mock()
     self.assertEqual(other % self.proxy, other % self.obj)
     self.assertEqual(self.proxy.__rmod__(other), other % self.obj)
     self.assertEqual(4.5 % proxy(2), 0.5)
     self.assertEqual(5 % proxy(2), 1)
 def test_rfloordiv(self):
     """ Verify that __rfloordiv__ is redirected to the proxiee. """
     other = mock.Mock()
     self.assertEqual(other // self.proxy, other // self.obj)
     self.assertEqual(self.proxy.__rfloordiv__(other), other // self.obj)
     self.assertEqual(4.5 // proxy(2), 2)
     self.assertEqual(5 // proxy(2), 2)
 def test_truediv(self):
     other = mock.MagicMock()
     self.assertEqual(operator.truediv(self.proxy, other),
                      operator.truediv(self.obj, other))
     self.assertEqual(self.proxy.__truediv__(other),
                      operator.truediv(self.obj, other))
     self.assertEqual(operator.truediv(proxy(4.5), 2), 2.25)
     self.assertEqual(operator.truediv(proxy(5), 2), 2.5)
 def test_rdivmod(self):
     """ Verify that __rdivmod__ is redirected to the proxiee. """
     other = mock.Mock()
     self.assertEqual(divmod(other, self.proxy), divmod(other, self.obj))
     self.assertEqual(
         self.proxy.__rdivmod__(other), divmod(other, self.obj))
     self.assertEqual(divmod(4.5, proxy(2)), (2, 0.5))
     self.assertEqual(divmod(5, proxy(2)), (2, 1))
 def test_rmul(self):
     """ Verify that __rmul__ is redirected to the proxiee. """
     other = mock.Mock()
     self.assertEqual(other * self.proxy, other * self.obj)
     self.assertEqual(self.proxy.__rmul__(other), other * self.obj)
     self.assertEqual(4.5 * proxy(2), 9.0)
     self.assertEqual(5 * proxy(2), 10)
     self.assertEqual("foo" * proxy(2), "foofoo")
 def test_sub(self):
     other = mock.MagicMock()
     self.assertEqual(self.proxy - other, self.obj - other)
     self.assertEqual(self.proxy.__sub__(other), self.obj - other)
     self.assertEqual(proxy(4.5) - 2, 2.5)
     self.assertEqual(proxy(5) - 2, 3)
     with self.assertRaises(TypeError):
         proxy("foo") - 2
 def test_xor(self):
     other = mock.MagicMock()
     self.assertEqual(self.proxy ^ other, self.obj ^ other)
     self.assertEqual(self.proxy.__xor__(other), self.obj ^ other)
     self.assertEqual(proxy(1) ^ 1, 0)
     self.assertEqual(proxy(1) ^ 0, 1)
     self.assertEqual(proxy(0) ^ 0, 0)
     self.assertEqual(proxy(0) ^ 1, 1)
 def test_delitem(self):
     """ Verify that del[] / __delitem__ is redirected to the proxiee. """
     obj = {'k': 'v'}
     del proxy(obj)['k']
     self.assertEqual(obj, {})
     obj = {'k': 'v'}
     proxy(obj).__delitem__('k')
     self.assertEqual(obj, {})
 def test_rtruediv(self):
     """ Verify that __rtruediv__ is redirected to the proxiee. """
     other = mock.Mock()
     self.assertEqual(operator.truediv(other, self.proxy),
                      operator.truediv(other, self.obj))
     self.assertEqual(self.proxy.__rtruediv__(other),
                      operator.truediv(other, self.obj))
     self.assertEqual(operator.truediv(4.5, proxy(2)), 2.25)
     self.assertEqual(operator.truediv(5, proxy(2)), 2.5)
 def test_rsub(self):
     """ Verify that __rsub__ is redirected to the proxiee. """
     other = mock.Mock()
     self.assertEqual(other - self.proxy, other - self.obj)
     self.assertEqual(self.proxy.__rsub__(other), other - self.obj)
     self.assertEqual(4.5 - proxy(2), 2.5)
     self.assertEqual(5 - proxy(2), 3)
     with self.assertRaises(TypeError):
         "foo" - proxy(2)
 def test_rxor(self):
     """ Verify that __rxor__ is redirected to the proxiee. """
     other = mock.Mock()
     self.assertEqual(other ^ self.proxy, other ^ self.obj)
     self.assertEqual(self.proxy.__rxor__(other), other ^ self.obj)
     self.assertEqual(1 ^ proxy(1), 0)
     self.assertEqual(1 ^ proxy(0), 1)
     self.assertEqual(0 ^ proxy(0), 0)
     self.assertEqual(0 ^ proxy(1), 1)
 def test_add(self):
     other = mock.MagicMock()
     self.assertEqual(self.proxy + other, self.obj + other)
     self.assertEqual(self.proxy.__add__(other), self.obj + other)
     self.assertEqual(proxy(4.5) + 2, 6.5)
     self.assertEqual(proxy(5) + 2, 7)
     with self.assertRaises(TypeError):
         proxy("foo") + 2
     self.assertEqual(proxy("foo") + "bar", "foobar")
 def test_rpow(self):
     """ Verify that __rpow__ is redirected to the proxiee. """
     other = mock.Mock()
     self.assertEqual(other ** self.proxy, other ** self.obj)
     self.assertEqual(self.proxy.__rpow__(other), other ** self.obj)
     self.assertEqual(pow(other, self.proxy), other ** self.obj)
     self.assertEqual(pow(2, proxy(3)), 8)
     with self.assertRaises(TypeError):
         # __rpow__ is not called for the three-argument version of pow()
         self.assertEqual(pow(2, proxy(10), 1000), 24)
 def test_radd(self):
     """ Verify that __radd__ is redirected to the proxiee.  """
     # NOTE: ``other`` is anything other than MagicMock to let
     # the reverse methods do their work. The same rule applies
     # to all the other test_r* methods so it won't be repeated
     # there.
     other = mock.Mock()
     self.assertEqual(other + self.proxy, other + self.obj)
     self.assertEqual(self.proxy.__radd__(other), other + self.obj)
     self.assertEqual(4.5 + proxy(2), 6.5)
     self.assertEqual(5 + proxy(2), 7)
     with self.assertRaises(TypeError):
         2 + proxy("foo")
     self.assertEqual("foo" + proxy("bar"), "foobar")
     self.assertEqual([1, 2, 3] + proxy([4, 5]), [1, 2, 3, 4, 5])
    def test_descriptor_methods(self):
        """ Verify that __{get,set,delete}__ are redirected to the proxiee. """
        property_proxy = proxy(property)

        class C(object):
            _ok = "default"

            @property_proxy
            def ok(self):
                return self._ok

            @ok.setter
            def ok(self, value):
                self._ok = value

            @ok.deleter
            def ok(self):
                del self._ok
        obj = C()
        self.assertEqual(obj._ok, "default")
        # __set__ assigns the new value
        obj.ok = True
        self.assertTrue(obj._ok)
        # __get__ returns the current value
        self.assertTrue(obj.ok)
        # __delete__ removes the current value
        del obj.ok
        self.assertEqual(obj._ok, "default")
 def test_gt(self):
     """ Verify that > /__gt__ is redirected to the proxiee. """
     # NOTE: MagicMock is not ordered so let's just use an integer
     self.obj = 0
     self.proxy = proxy(self.obj)
     self.assertGreater(self.proxy, -1)
     self.assertGreater(self.obj, -1)
 def test_setitem_v1(self):
     """ Verify that []= is redirected to the proxiee. """
     # NOTE: MagicMock doesn't store item assignment
     self.obj = ["old"]
     self.proxy = proxy(self.obj)
     self.proxy[0] = "new"
     self.assertEqual(self.obj[0], "new")
 def test_setitem_v2(self):
     """ Verify that __setitem__ is redirected to the proxiee. """
     # NOTE: MagicMock doesn't store item assignment
     self.obj = ["old"]
     self.proxy = proxy(self.obj)
     self.proxy.__setitem__(0, "value")
     self.assertEqual(self.obj[0], "value")
 def test_lt(self):
     """ Verify that < /__lt__ is redirected to the proxiee. """
     # NOTE: MagicMock is not ordered so let's just use an integer
     self.obj = 0
     self.proxy = proxy(self.obj)
     self.assertLess(self.proxy, 1)
     self.assertLess(self.obj, 1)
 def test_ior__on_int(self):
     """ Verify that |= works on proxy[int]. """
     a = b = proxy(7)
     b |= 8
     self.assertEqual(a, 7)
     self.assertEqual(b, 15)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_irshift__on_int(self):
     """ Verify that >>= works on proxy[int]. """
     a = b = proxy(1024)
     b >>= 10
     self.assertEqual(a, 1024)
     self.assertEqual(b, 1)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_iand__on_int(self):
     """ Verify that &= works on proxy[int]. """
     a = b = proxy(7)
     b &= 4
     self.assertEqual(a, 7)
     self.assertEqual(b, 4)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_imod__on_int(self):
     """ Verify that %= works on proxy[int]. """
     a = b = proxy(7)
     b %= 2
     self.assertEqual(a, 7)
     self.assertEqual(b, 1)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_ipow__on_int(self):
     """ Verify that **= works on proxy[int]. """
     a = b = proxy(7)
     b **= 2
     self.assertEqual(a, 7)
     self.assertEqual(b, 49)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_ifloordiv__on_int(self):
     """ Verify that //= works on proxy[int]. """
     a = b = proxy(7)
     b //= 2
     self.assertEqual(a, 7)
     self.assertEqual(b, 3)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_itruediv__on_int(self):
     """ Verify that (true) /= works on proxy[int]. """
     a = b = proxy(7)
     b = operator.itruediv(b, 2)
     self.assertEqual(a, 7)
     self.assertEqual(b, 3.5)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_imul__on_int(self):
     """ Verify that *= works on proxy[int]. """
     a = b = proxy(4)
     b *= 2
     self.assertEqual(a, 4)
     self.assertEqual(b, 8)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_isub__on_int(self):
     """ Verify that -= works on proxy[int]. """
     a = b = proxy(4)
     b -= 3
     self.assertEqual(a, 4)
     self.assertEqual(b, 1)
     self.assertTrue(issubclass(type(a), proxy))
     self.assertTrue(issubclass(type(b), proxy))
 def test_iadd__on_str__via_dunder(self):
     """ Verify that __iadd__ doesn't exist on proxy[str]. """
     a = "hello"
     with self.assertRaises(AttributeError):
         a.__iadd__(" world")
     b = proxy("hello")
     with self.assertRaises(AttributeError):
         b.__iadd__(" world")