Commits

Konstantine Rybnikov committed d1af45f

add M class

Comments (0)

Files changed (2)

 # -*- coding: utf-8 -*-
 
+import types
+from functools import wraps
+
 from mock import patch
-from functools import wraps
+from mock import MagicMock
 
 
 class DotDict(dict):
         new_kw.update(defaults)
         return patcher(prefix + '.' + name, *args, **new_kw)
     return rv
+
+
+class RVDescriptor(object):
+    def __get__(self, instance, owner):
+        if instance is None:
+            return owner.return_value
+        return instance.return_value
+
+    def __set__(self, instance, value):
+        instance.return_value = value
+
+    def __delete__(self, instance):
+        del instance.return_value
+
+
+class M(MagicMock):
+    """
+    :class:`~MagicMock` with shortcuts. I just couldn't stand the
+    ``.return_value.foo.return_value.bar.return_value`` thing.
+    """
+    def __init__(self, *args, **kw):
+        super(M, self).__init__(*args, **kw)
+
+    def _get_child_mock(self, **kw):
+        return M(**kw)
+
+    #: shortcut for ``.return_value``
+    rv = RVDescriptor()
 
 import unittest
 from unittest import TestCase
-from mock import Mock
+from mock import MagicMock
 from mock import patch
 
 from mockstar import p
 from mockstar import DotDict
 from mockstar import prefixed_p
+from mockstar import M
 
 
 def side_effect_one():
     @p(__name__ + '.side_effect_one')
     @p(__name__ + '.side_effect_two')
     def test_should_mock_to_kw(self, se):
-        self.assertIsInstance(se.side_effect_one, Mock)
-        self.assertIsInstance(se.side_effect_two, Mock)
+        self.assertIsInstance(se.side_effect_one, MagicMock)
+        self.assertIsInstance(se.side_effect_two, MagicMock)
 
     @p(__name__ + '.side_effect_four')
     def test_should_mock_inner_call(self, se):
         self.assertRaises(TypeError, lambda: se.side_effect_five())
 
 
+class TestM(TestCase):
+    def test_should_create_object(self):
+        m = M()
+        m.asd = 'dsa'
+        self.assertEquals(m.asd, 'dsa')
+
+    def test_should_make_couple_rv(self):
+        m = M()
+        m.foo.rv.bar.rv.baz.rv = 28
+        self.assertEquals(m.foo().bar().baz(), 28)
+
+
 ppatch = prefixed_p(__name__)
+# ppatch_autospec_M = prefixed_p(__name__, autospec=True, to_m=True)
 
 
 class TestPrefixedP(TestCase):
     def test_should_prefix_patch_here(self, se):
         self.assertRaises(TypeError, lambda: se.side_effect_five())
 
+    # @ppatch_autospec_M('side_effect_five')
+    # def test_should_return_m_and_raise_typeerror(self, se):
+    #     self.assertRaises(TypeError, lambda: se.side_effect_five())
+    #     se.side_effect_five.rv = 20
+    #     res = se.side_effect_five(10)
+    #     self.assertEquals(res, 20)
+    #     # self.assertIsInstance(m, M)
+
 
 @ppatch('side_effect_one')
 @ppatch('side_effect_two')
 class TestPatchClass(TestCase):
     def test_should_get_se(self, se):
-        self.assertIsInstance(se.side_effect_one, Mock)
-        self.assertIsInstance(se.side_effect_two, Mock)
+        self.assertIsInstance(se.side_effect_one, MagicMock)
+        self.assertIsInstance(se.side_effect_two, MagicMock)
 
     def test_should_also_get_se(self, se):
-        self.assertIsInstance(se.side_effect_one, Mock)
-        self.assertIsInstance(se.side_effect_two, Mock)
+        self.assertIsInstance(se.side_effect_one, MagicMock)
+        self.assertIsInstance(se.side_effect_two, MagicMock)
 
 
 if __name__ == '__main__':