# Copyright (C) 2007-2011 Michael Foord & the mock team
# E-mail: fuzzyman AT voidspace DOT org DOT uk
# http://www.voidspace.org.uk/python/mock/

from tests.support import is_instance, unittest2, X

from mock import (
    Mock, MagicMock, NonCallableMagicMock,
    NonCallableMock, patch, create_autospec
)

"""
Note that NonCallableMock and NonCallableMagicMock still have the unused (and
unusable) attributes: return_value, side_effect, call_count, call_args and
call_args_list. These could be removed or raise errors on getting / setting.

They also have the assert_called_with and assert_called_once_with methods.
Removing these would be pointless as fetching them would create a mock
(attribute) that could be called without error.
"""


class TestCallable(unittest2.TestCase):

    def test_non_callable(self):
        for mock in NonCallableMagicMock(), NonCallableMock():
            self.assertRaises(TypeError, mock)
            self.assertFalse(hasattr(mock, '__call__'))


    def test_attributes(self):
        one = NonCallableMock()
        self.assertTrue(issubclass(type(one.one), Mock))

        two = NonCallableMagicMock()
        self.assertTrue(issubclass(type(two.two), MagicMock))


    def test_subclasses(self):
        class MockSub(Mock):
            pass

        one = MockSub()
        self.assertTrue(issubclass(type(one.one), MockSub))

        class MagicSub(MagicMock):
            pass

        two = MagicSub()
        self.assertTrue(issubclass(type(two.two), MagicSub))


    def test_patch_spec(self):
        patcher = patch('%s.X' % __name__, spec=True)
        mock = patcher.start()
        self.addCleanup(patcher.stop)

        instance = mock()
        mock.assert_called_once_with()

        self.assertTrue(is_instance(instance, NonCallableMagicMock))
        self.assertRaises(TypeError, instance)


    def test_patch_spec_instance(self):
        patcher = patch('%s.X' % __name__, spec=X())
        mock = patcher.start()
        self.addCleanup(patcher.stop)

        self.assertTrue(is_instance(mock, NonCallableMagicMock))
        self.assertRaises(TypeError, mock)


    def test_patch_spec_callable_class(self):
        class CallableX(X):
            def __call__(self):
                pass

        patcher = patch('%s.X' % __name__, spec=CallableX)
        mock = patcher.start()
        self.addCleanup(patcher.stop)

        instance = mock()
        mock.assert_called_once_with()

        self.assertTrue(is_instance(instance, MagicMock))
        instance()
        instance.assert_called_once_with()


    def test_create_autopsec(self):
        mock = create_autospec(X)
        instance = mock()
        self.assertRaises(TypeError, instance)

        mock = create_autospec(X())
        self.assertRaises(TypeError, mock)


@@ -191,16 +191,27 @@
 test2, X
+, SomeClass
 %0A%0Afrom m
@@ -2248,32 +2248,339 @@
           pass%0A%0A
+        class Sub(CallableX):%0A            pass%0A%0A        class Multi(SomeClass, Sub):%0A            pass%0A%0A        class OldStyle:%0A            def __call__(self):%0A                pass%0A%0A        class OldStyleSub(OldStyle):%0A            pass%0A%0A        for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub:%0A    
         patcher 
@@ -2615,19 +2615,19 @@
 pec=
-CallableX)%0A
+Klass)%0A    
     
@@ -2653,32 +2653,36 @@
 start()%0A        
+   
+ 
 self.addCleanup(
@@ -2696,32 +2696,36 @@
 .stop)%0A%0A        
+    
 instance = mock(
@@ -2726,32 +2726,36 @@
  mock()%0A        
+ 
+   
 mock.assert_call
@@ -2770,32 +2770,36 @@
 with()%0A%0A        
+  
+  
 self.assertTrue(
@@ -2832,32 +2832,36 @@
 cMock))%0A        
+    
 instance()%0A     
@@ -2855,16 +2855,20 @@
 tance()%0A
+    
         

Test extension