You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ma...@apache.org on 2013/02/04 03:24:02 UTC

svn commit: r1442010 [7/29] - in /incubator/ambari/branches/branch-1.2: ./ ambari-agent/ ambari-agent/conf/unix/ ambari-agent/src/examples/ ambari-agent/src/main/puppet/modules/hdp-ganglia/files/ ambari-agent/src/main/puppet/modules/hdp-ganglia/manifes...

Added: incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/_testwith.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/_testwith.py?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/_testwith.py (added)
+++ incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/_testwith.py Mon Feb  4 02:23:55 2013
@@ -0,0 +1,181 @@
+# Copyright (C) 2007-2012 Michael Foord & the mock team
+# E-mail: fuzzyman AT voidspace DOT org DOT uk
+# http://www.voidspace.org.uk/python/mock/
+
+from __future__ import with_statement
+
+from tests.support import unittest2, is_instance
+
+from mock import MagicMock, Mock, patch, sentinel, mock_open, call
+
+from tests.support_with import catch_warnings, nested
+
+something  = sentinel.Something
+something_else  = sentinel.SomethingElse
+
+
+
+class WithTest(unittest2.TestCase):
+
+    def test_with_statement(self):
+        with patch('tests._testwith.something', sentinel.Something2):
+            self.assertEqual(something, sentinel.Something2, "unpatched")
+        self.assertEqual(something, sentinel.Something)
+
+
+    def test_with_statement_exception(self):
+        try:
+            with patch('tests._testwith.something', sentinel.Something2):
+                self.assertEqual(something, sentinel.Something2, "unpatched")
+                raise Exception('pow')
+        except Exception:
+            pass
+        else:
+            self.fail("patch swallowed exception")
+        self.assertEqual(something, sentinel.Something)
+
+
+    def test_with_statement_as(self):
+        with patch('tests._testwith.something') as mock_something:
+            self.assertEqual(something, mock_something, "unpatched")
+            self.assertTrue(is_instance(mock_something, MagicMock),
+                            "patching wrong type")
+        self.assertEqual(something, sentinel.Something)
+
+
+    def test_patch_object_with_statement(self):
+        class Foo(object):
+            something = 'foo'
+        original = Foo.something
+        with patch.object(Foo, 'something'):
+            self.assertNotEqual(Foo.something, original, "unpatched")
+        self.assertEqual(Foo.something, original)
+
+
+    def test_with_statement_nested(self):
+        with catch_warnings(record=True):
+            # nested is deprecated in Python 2.7
+            with nested(patch('tests._testwith.something'),
+                    patch('tests._testwith.something_else')) as (mock_something, mock_something_else):
+                self.assertEqual(something, mock_something, "unpatched")
+                self.assertEqual(something_else, mock_something_else,
+                                 "unpatched")
+        self.assertEqual(something, sentinel.Something)
+        self.assertEqual(something_else, sentinel.SomethingElse)
+
+
+    def test_with_statement_specified(self):
+        with patch('tests._testwith.something', sentinel.Patched) as mock_something:
+            self.assertEqual(something, mock_something, "unpatched")
+            self.assertEqual(mock_something, sentinel.Patched, "wrong patch")
+        self.assertEqual(something, sentinel.Something)
+
+
+    def testContextManagerMocking(self):
+        mock = Mock()
+        mock.__enter__ = Mock()
+        mock.__exit__ = Mock()
+        mock.__exit__.return_value = False
+
+        with mock as m:
+            self.assertEqual(m, mock.__enter__.return_value)
+        mock.__enter__.assert_called_with()
+        mock.__exit__.assert_called_with(None, None, None)
+
+
+    def test_context_manager_with_magic_mock(self):
+        mock = MagicMock()
+
+        with self.assertRaises(TypeError):
+            with mock:
+                'foo' + 3
+        mock.__enter__.assert_called_with()
+        self.assertTrue(mock.__exit__.called)
+
+
+    def test_with_statement_same_attribute(self):
+        with patch('tests._testwith.something', sentinel.Patched) as mock_something:
+            self.assertEqual(something, mock_something, "unpatched")
+
+            with patch('tests._testwith.something') as mock_again:
+                self.assertEqual(something, mock_again, "unpatched")
+
+            self.assertEqual(something, mock_something,
+                             "restored with wrong instance")
+
+        self.assertEqual(something, sentinel.Something, "not restored")
+
+
+    def test_with_statement_imbricated(self):
+        with patch('tests._testwith.something') as mock_something:
+            self.assertEqual(something, mock_something, "unpatched")
+
+            with patch('tests._testwith.something_else') as mock_something_else:
+                self.assertEqual(something_else, mock_something_else,
+                                 "unpatched")
+
+        self.assertEqual(something, sentinel.Something)
+        self.assertEqual(something_else, sentinel.SomethingElse)
+
+
+    def test_dict_context_manager(self):
+        foo = {}
+        with patch.dict(foo, {'a': 'b'}):
+            self.assertEqual(foo, {'a': 'b'})
+        self.assertEqual(foo, {})
+
+        with self.assertRaises(NameError):
+            with patch.dict(foo, {'a': 'b'}):
+                self.assertEqual(foo, {'a': 'b'})
+                raise NameError('Konrad')
+
+        self.assertEqual(foo, {})
+
+
+
+class TestMockOpen(unittest2.TestCase):
+
+    def test_mock_open(self):
+        mock = mock_open()
+        with patch('%s.open' % __name__, mock, create=True) as patched:
+            self.assertIs(patched, mock)
+            open('foo')
+
+        mock.assert_called_once_with('foo')
+
+
+    def test_mock_open_context_manager(self):
+        mock = mock_open()
+        handle = mock.return_value
+        with patch('%s.open' % __name__, mock, create=True):
+            with open('foo') as f:
+                f.read()
+
+        expected_calls = [call('foo'), call().__enter__(), call().read(),
+                          call().__exit__(None, None, None)]
+        self.assertEqual(mock.mock_calls, expected_calls)
+        self.assertIs(f, handle)
+
+
+    def test_explicit_mock(self):
+        mock = MagicMock()
+        mock_open(mock)
+
+        with patch('%s.open' % __name__, mock, create=True) as patched:
+            self.assertIs(patched, mock)
+            open('foo')
+
+        mock.assert_called_once_with('foo')
+
+
+    def test_read_data(self):
+        mock = mock_open(read_data='foo')
+        with patch('%s.open' % __name__, mock, create=True):
+            h = open('bar')
+            result = h.read()
+
+        self.assertEqual(result, 'foo')
+
+
+if __name__ == '__main__':
+    unittest2.main()

Added: incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/support.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/support.py?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/support.py (added)
+++ incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/support.py Mon Feb  4 02:23:55 2013
@@ -0,0 +1,41 @@
+import sys
+
+info = sys.version_info
+if info[:3] >= (3, 2, 0):
+    # for Python 3.2 ordinary unittest is fine
+    import unittest as unittest2
+else:
+    import unittest2
+
+
+try:
+    callable = callable
+except NameError:
+    def callable(obj):
+        return hasattr(obj, '__call__')
+
+
+inPy3k = sys.version_info[0] == 3
+with_available = sys.version_info[:2] >= (2, 5)
+
+
+def is_instance(obj, klass):
+    """Version of is_instance that doesn't access __class__"""
+    return issubclass(type(obj), klass)
+
+
+class SomeClass(object):
+    class_attribute = None
+
+    def wibble(self):
+        pass
+
+
+class X(object):
+    pass
+
+try:
+    next = next
+except NameError:
+    def next(obj):
+        return obj.next()

Added: incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/support_with.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/support_with.py?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/support_with.py (added)
+++ incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/support_with.py Mon Feb  4 02:23:55 2013
@@ -0,0 +1,93 @@
+from __future__ import with_statement
+
+import sys
+
+__all__ = ['nested', 'catch_warnings', 'examine_warnings']
+
+
+try:
+    from contextlib import nested
+except ImportError:
+    from contextlib import contextmanager
+    @contextmanager
+    def nested(*managers):
+        exits = []
+        vars = []
+        exc = (None, None, None)
+        try:
+            for mgr in managers:
+                exit = mgr.__exit__
+                enter = mgr.__enter__
+                vars.append(enter())
+                exits.append(exit)
+            yield vars
+        except:
+            exc = sys.exc_info()
+        finally:
+            while exits:
+                exit = exits.pop()
+                try:
+                    if exit(*exc):
+                        exc = (None, None, None)
+                except:
+                    exc = sys.exc_info()
+            if exc != (None, None, None):
+                raise exc[1]
+
+# copied from Python 2.6
+try:
+    from warnings import catch_warnings
+except ImportError:
+    class catch_warnings(object):
+        def __init__(self, record=False, module=None):
+            self._record = record
+            self._module = sys.modules['warnings']
+            self._entered = False
+
+        def __repr__(self):
+            args = []
+            if self._record:
+                args.append("record=True")
+            name = type(self).__name__
+            return "%s(%s)" % (name, ", ".join(args))
+
+        def __enter__(self):
+            if self._entered:
+                raise RuntimeError("Cannot enter %r twice" % self)
+            self._entered = True
+            self._filters = self._module.filters
+            self._module.filters = self._filters[:]
+            self._showwarning = self._module.showwarning
+            if self._record:
+                log = []
+                def showwarning(*args, **kwargs):
+                    log.append(WarningMessage(*args, **kwargs))
+                self._module.showwarning = showwarning
+                return log
+            else:
+                return None
+
+        def __exit__(self, *exc_info):
+            if not self._entered:
+                raise RuntimeError("Cannot exit %r without entering first" % self)
+            self._module.filters = self._filters
+            self._module.showwarning = self._showwarning
+
+    class WarningMessage(object):
+        _WARNING_DETAILS = ("message", "category", "filename", "lineno", "file",
+                            "line")
+        def __init__(self, message, category, filename, lineno, file=None,
+                        line=None):
+            local_values = locals()
+            for attr in self._WARNING_DETAILS:
+                setattr(self, attr, local_values[attr])
+            self._category_name = None
+            if category.__name__:
+                self._category_name = category.__name__
+
+
+def examine_warnings(func):
+    def wrapper():
+        with catch_warnings(record=True) as ws:
+            func(ws)
+    return wrapper

Added: incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testcallable.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testcallable.py?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testcallable.py (added)
+++ incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testcallable.py Mon Feb  4 02:23:55 2013
@@ -0,0 +1,158 @@
+# Copyright (C) 2007-2012 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, SomeClass
+
+from mock import (
+    Mock, MagicMock, NonCallableMagicMock,
+    NonCallableMock, patch, create_autospec,
+    CallableMixin
+)
+
+
+
+class TestCallable(unittest2.TestCase):
+
+    def assertNotCallable(self, mock):
+        self.assertTrue(is_instance(mock, NonCallableMagicMock))
+        self.assertFalse(is_instance(mock, CallableMixin))
+
+
+    def test_non_callable(self):
+        for mock in NonCallableMagicMock(), NonCallableMock():
+            self.assertRaises(TypeError, mock)
+            self.assertFalse(hasattr(mock, '__call__'))
+            self.assertIn(mock.__class__.__name__, repr(mock))
+
+
+    def test_heirarchy(self):
+        self.assertTrue(issubclass(MagicMock, Mock))
+        self.assertTrue(issubclass(NonCallableMagicMock, NonCallableMock))
+
+
+    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.assertNotCallable(instance)
+        self.assertRaises(TypeError, instance)
+
+
+    def test_patch_spec_set(self):
+        patcher = patch('%s.X' % __name__, spec_set=True)
+        mock = patcher.start()
+        self.addCleanup(patcher.stop)
+
+        instance = mock()
+        mock.assert_called_once_with()
+
+        self.assertNotCallable(instance)
+        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.assertNotCallable(mock)
+        self.assertRaises(TypeError, mock)
+
+
+    def test_patch_spec_set_instance(self):
+        patcher = patch('%s.X' % __name__, spec_set=X())
+        mock = patcher.start()
+        self.addCleanup(patcher.stop)
+
+        self.assertNotCallable(mock)
+        self.assertRaises(TypeError, mock)
+
+
+    def test_patch_spec_callable_class(self):
+        class CallableX(X):
+            def __call__(self):
+                pass
+
+        class Sub(CallableX):
+            pass
+
+        class Multi(SomeClass, Sub):
+            pass
+
+        class OldStyle:
+            def __call__(self):
+                pass
+
+        class OldStyleSub(OldStyle):
+            pass
+
+        for arg in 'spec', 'spec_set':
+            for Klass in CallableX, Sub, Multi, OldStyle, OldStyleSub:
+                patcher = patch('%s.X' % __name__, **{arg: Klass})
+                mock = patcher.start()
+
+                try:
+                    instance = mock()
+                    mock.assert_called_once_with()
+
+                    self.assertTrue(is_instance(instance, MagicMock))
+                    # inherited spec
+                    self.assertRaises(AttributeError, getattr, instance,
+                                      'foobarbaz')
+
+                    result = instance()
+                    # instance is callable, result has no spec
+                    instance.assert_called_once_with()
+
+                    result(3, 2, 1)
+                    result.assert_called_once_with(3, 2, 1)
+                    result.foo(3, 2, 1)
+                    result.foo.assert_called_once_with(3, 2, 1)
+                finally:
+                    patcher.stop()
+
+
+    def test_create_autopsec(self):
+        mock = create_autospec(X)
+        instance = mock()
+        self.assertRaises(TypeError, instance)
+
+        mock = create_autospec(X())
+        self.assertRaises(TypeError, mock)
+
+
+    def test_create_autospec_instance(self):
+        mock = create_autospec(SomeClass, instance=True)
+
+        self.assertRaises(TypeError, mock)
+        mock.wibble()
+        mock.wibble.assert_called_once_with()
+
+        self.assertRaises(TypeError, mock.wibble, 'some',  'args')

Added: incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testhelpers.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testhelpers.py?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testhelpers.py (added)
+++ incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testhelpers.py Mon Feb  4 02:23:55 2013
@@ -0,0 +1,940 @@
+# Copyright (C) 2007-2012 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 unittest2, inPy3k
+
+from mock import (
+    call, _Call, create_autospec, MagicMock,
+    Mock, ANY, _CallList, patch, PropertyMock
+)
+
+from datetime import datetime
+
+class SomeClass(object):
+    def one(self, a, b):
+        pass
+    def two(self):
+        pass
+    def three(self, a=None):
+        pass
+
+
+
+class AnyTest(unittest2.TestCase):
+
+    def test_any(self):
+        self.assertEqual(ANY, object())
+
+        mock = Mock()
+        mock(ANY)
+        mock.assert_called_with(ANY)
+
+        mock = Mock()
+        mock(foo=ANY)
+        mock.assert_called_with(foo=ANY)
+
+    def test_repr(self):
+        self.assertEqual(repr(ANY), '<ANY>')
+        self.assertEqual(str(ANY), '<ANY>')
+
+
+    def test_any_and_datetime(self):
+        mock = Mock()
+        mock(datetime.now(), foo=datetime.now())
+
+        mock.assert_called_with(ANY, foo=ANY)
+
+
+    def test_any_mock_calls_comparison_order(self):
+        mock = Mock()
+        d = datetime.now()
+        class Foo(object):
+            def __eq__(self, other):
+                return False
+            def __ne__(self, other):
+                return True
+
+        for d in datetime.now(), Foo():
+            mock.reset_mock()
+
+            mock(d, foo=d, bar=d)
+            mock.method(d, zinga=d, alpha=d)
+            mock().method(a1=d, z99=d)
+
+            expected = [
+                call(ANY, foo=ANY, bar=ANY),
+                call.method(ANY, zinga=ANY, alpha=ANY),
+                call(), call().method(a1=ANY, z99=ANY)
+            ]
+            self.assertEqual(expected, mock.mock_calls)
+            self.assertEqual(mock.mock_calls, expected)
+
+
+
+class CallTest(unittest2.TestCase):
+
+    def test_call_with_call(self):
+        kall = _Call()
+        self.assertEqual(kall, _Call())
+        self.assertEqual(kall, _Call(('',)))
+        self.assertEqual(kall, _Call(((),)))
+        self.assertEqual(kall, _Call(({},)))
+        self.assertEqual(kall, _Call(('', ())))
+        self.assertEqual(kall, _Call(('', {})))
+        self.assertEqual(kall, _Call(('', (), {})))
+        self.assertEqual(kall, _Call(('foo',)))
+        self.assertEqual(kall, _Call(('bar', ())))
+        self.assertEqual(kall, _Call(('baz', {})))
+        self.assertEqual(kall, _Call(('spam', (), {})))
+
+        kall = _Call(((1, 2, 3),))
+        self.assertEqual(kall, _Call(((1, 2, 3),)))
+        self.assertEqual(kall, _Call(('', (1, 2, 3))))
+        self.assertEqual(kall, _Call(((1, 2, 3), {})))
+        self.assertEqual(kall, _Call(('', (1, 2, 3), {})))
+
+        kall = _Call(((1, 2, 4),))
+        self.assertNotEqual(kall, _Call(('', (1, 2, 3))))
+        self.assertNotEqual(kall, _Call(('', (1, 2, 3), {})))
+
+        kall = _Call(('foo', (1, 2, 4),))
+        self.assertNotEqual(kall, _Call(('', (1, 2, 4))))
+        self.assertNotEqual(kall, _Call(('', (1, 2, 4), {})))
+        self.assertNotEqual(kall, _Call(('bar', (1, 2, 4))))
+        self.assertNotEqual(kall, _Call(('bar', (1, 2, 4), {})))
+
+        kall = _Call(({'a': 3},))
+        self.assertEqual(kall, _Call(('', (), {'a': 3})))
+        self.assertEqual(kall, _Call(('', {'a': 3})))
+        self.assertEqual(kall, _Call(((), {'a': 3})))
+        self.assertEqual(kall, _Call(({'a': 3},)))
+
+
+    def test_empty__Call(self):
+        args = _Call()
+
+        self.assertEqual(args, ())
+        self.assertEqual(args, ('foo',))
+        self.assertEqual(args, ((),))
+        self.assertEqual(args, ('foo', ()))
+        self.assertEqual(args, ('foo',(), {}))
+        self.assertEqual(args, ('foo', {}))
+        self.assertEqual(args, ({},))
+
+
+    def test_named_empty_call(self):
+        args = _Call(('foo', (), {}))
+
+        self.assertEqual(args, ('foo',))
+        self.assertEqual(args, ('foo', ()))
+        self.assertEqual(args, ('foo',(), {}))
+        self.assertEqual(args, ('foo', {}))
+
+        self.assertNotEqual(args, ((),))
+        self.assertNotEqual(args, ())
+        self.assertNotEqual(args, ({},))
+        self.assertNotEqual(args, ('bar',))
+        self.assertNotEqual(args, ('bar', ()))
+        self.assertNotEqual(args, ('bar', {}))
+
+
+    def test_call_with_args(self):
+        args = _Call(((1, 2, 3), {}))
+
+        self.assertEqual(args, ((1, 2, 3),))
+        self.assertEqual(args, ('foo', (1, 2, 3)))
+        self.assertEqual(args, ('foo', (1, 2, 3), {}))
+        self.assertEqual(args, ((1, 2, 3), {}))
+
+
+    def test_named_call_with_args(self):
+        args = _Call(('foo', (1, 2, 3), {}))
+
+        self.assertEqual(args, ('foo', (1, 2, 3)))
+        self.assertEqual(args, ('foo', (1, 2, 3), {}))
+
+        self.assertNotEqual(args, ((1, 2, 3),))
+        self.assertNotEqual(args, ((1, 2, 3), {}))
+
+
+    def test_call_with_kwargs(self):
+        args = _Call(((), dict(a=3, b=4)))
+
+        self.assertEqual(args, (dict(a=3, b=4),))
+        self.assertEqual(args, ('foo', dict(a=3, b=4)))
+        self.assertEqual(args, ('foo', (), dict(a=3, b=4)))
+        self.assertEqual(args, ((), dict(a=3, b=4)))
+
+
+    def test_named_call_with_kwargs(self):
+        args = _Call(('foo', (), dict(a=3, b=4)))
+
+        self.assertEqual(args, ('foo', dict(a=3, b=4)))
+        self.assertEqual(args, ('foo', (), dict(a=3, b=4)))
+
+        self.assertNotEqual(args, (dict(a=3, b=4),))
+        self.assertNotEqual(args, ((), dict(a=3, b=4)))
+
+
+    def test_call_with_args_call_empty_name(self):
+        args = _Call(((1, 2, 3), {}))
+        self.assertEqual(args, call(1, 2, 3))
+        self.assertEqual(call(1, 2, 3), args)
+        self.assertTrue(call(1, 2, 3) in [args])
+
+
+    def test_call_ne(self):
+        self.assertNotEqual(_Call(((1, 2, 3),)), call(1, 2))
+        self.assertFalse(_Call(((1, 2, 3),)) != call(1, 2, 3))
+        self.assertTrue(_Call(((1, 2), {})) != call(1, 2, 3))
+
+
+    def test_call_non_tuples(self):
+        kall = _Call(((1, 2, 3),))
+        for value in 1, None, self, int:
+            self.assertNotEqual(kall, value)
+            self.assertFalse(kall == value)
+
+
+    def test_repr(self):
+        self.assertEqual(repr(_Call()), 'call()')
+        self.assertEqual(repr(_Call(('foo',))), 'call.foo()')
+
+        self.assertEqual(repr(_Call(((1, 2, 3), {'a': 'b'}))),
+                         "call(1, 2, 3, a='b')")
+        self.assertEqual(repr(_Call(('bar', (1, 2, 3), {'a': 'b'}))),
+                         "call.bar(1, 2, 3, a='b')")
+
+        self.assertEqual(repr(call), 'call')
+        self.assertEqual(str(call), 'call')
+
+        self.assertEqual(repr(call()), 'call()')
+        self.assertEqual(repr(call(1)), 'call(1)')
+        self.assertEqual(repr(call(zz='thing')), "call(zz='thing')")
+
+        self.assertEqual(repr(call().foo), 'call().foo')
+        self.assertEqual(repr(call(1).foo.bar(a=3).bing),
+                         'call().foo.bar().bing')
+        self.assertEqual(
+            repr(call().foo(1, 2, a=3)),
+            "call().foo(1, 2, a=3)"
+        )
+        self.assertEqual(repr(call()()), "call()()")
+        self.assertEqual(repr(call(1)(2)), "call()(2)")
+        self.assertEqual(
+            repr(call()().bar().baz.beep(1)),
+            "call()().bar().baz.beep(1)"
+        )
+
+
+    def test_call(self):
+        self.assertEqual(call(), ('', (), {}))
+        self.assertEqual(call('foo', 'bar', one=3, two=4),
+                         ('', ('foo', 'bar'), {'one': 3, 'two': 4}))
+
+        mock = Mock()
+        mock(1, 2, 3)
+        mock(a=3, b=6)
+        self.assertEqual(mock.call_args_list,
+                         [call(1, 2, 3), call(a=3, b=6)])
+
+    def test_attribute_call(self):
+        self.assertEqual(call.foo(1), ('foo', (1,), {}))
+        self.assertEqual(call.bar.baz(fish='eggs'),
+                         ('bar.baz', (), {'fish': 'eggs'}))
+
+        mock = Mock()
+        mock.foo(1, 2 ,3)
+        mock.bar.baz(a=3, b=6)
+        self.assertEqual(mock.method_calls,
+                         [call.foo(1, 2, 3), call.bar.baz(a=3, b=6)])
+
+
+    def test_extended_call(self):
+        result = call(1).foo(2).bar(3, a=4)
+        self.assertEqual(result, ('().foo().bar', (3,), dict(a=4)))
+
+        mock = MagicMock()
+        mock(1, 2, a=3, b=4)
+        self.assertEqual(mock.call_args, call(1, 2, a=3, b=4))
+        self.assertNotEqual(mock.call_args, call(1, 2, 3))
+
+        self.assertEqual(mock.call_args_list, [call(1, 2, a=3, b=4)])
+        self.assertEqual(mock.mock_calls, [call(1, 2, a=3, b=4)])
+
+        mock = MagicMock()
+        mock.foo(1).bar()().baz.beep(a=6)
+
+        last_call = call.foo(1).bar()().baz.beep(a=6)
+        self.assertEqual(mock.mock_calls[-1], last_call)
+        self.assertEqual(mock.mock_calls, last_call.call_list())
+
+
+    def test_call_list(self):
+        mock = MagicMock()
+        mock(1)
+        self.assertEqual(call(1).call_list(), mock.mock_calls)
+
+        mock = MagicMock()
+        mock(1).method(2)
+        self.assertEqual(call(1).method(2).call_list(),
+                         mock.mock_calls)
+
+        mock = MagicMock()
+        mock(1).method(2)(3)
+        self.assertEqual(call(1).method(2)(3).call_list(),
+                         mock.mock_calls)
+
+        mock = MagicMock()
+        int(mock(1).method(2)(3).foo.bar.baz(4)(5))
+        kall = call(1).method(2)(3).foo.bar.baz(4)(5).__int__()
+        self.assertEqual(kall.call_list(), mock.mock_calls)
+
+
+    def test_call_any(self):
+        self.assertEqual(call, ANY)
+
+        m = MagicMock()
+        int(m)
+        self.assertEqual(m.mock_calls, [ANY])
+        self.assertEqual([ANY], m.mock_calls)
+
+
+    def test_two_args_call(self):
+        args = _Call(((1, 2), {'a': 3}), two=True)
+        self.assertEqual(len(args), 2)
+        self.assertEqual(args[0], (1, 2))
+        self.assertEqual(args[1], {'a': 3})
+
+        other_args = _Call(((1, 2), {'a': 3}))
+        self.assertEqual(args, other_args)
+
+
+class SpecSignatureTest(unittest2.TestCase):
+
+    def _check_someclass_mock(self, mock):
+        self.assertRaises(AttributeError, getattr, mock, 'foo')
+        mock.one(1, 2)
+        mock.one.assert_called_with(1, 2)
+        self.assertRaises(AssertionError,
+                          mock.one.assert_called_with, 3, 4)
+        self.assertRaises(TypeError, mock.one, 1)
+
+        mock.two()
+        mock.two.assert_called_with()
+        self.assertRaises(AssertionError,
+                          mock.two.assert_called_with, 3)
+        self.assertRaises(TypeError, mock.two, 1)
+
+        mock.three()
+        mock.three.assert_called_with()
+        self.assertRaises(AssertionError,
+                          mock.three.assert_called_with, 3)
+        self.assertRaises(TypeError, mock.three, 3, 2)
+
+        mock.three(1)
+        mock.three.assert_called_with(1)
+
+        mock.three(a=1)
+        mock.three.assert_called_with(a=1)
+
+
+    def test_basic(self):
+        for spec in (SomeClass, SomeClass()):
+            mock = create_autospec(spec)
+            self._check_someclass_mock(mock)
+
+
+    def test_create_autospec_return_value(self):
+        def f():
+            pass
+        mock = create_autospec(f, return_value='foo')
+        self.assertEqual(mock(), 'foo')
+
+        class Foo(object):
+            pass
+
+        mock = create_autospec(Foo, return_value='foo')
+        self.assertEqual(mock(), 'foo')
+
+
+    def test_autospec_reset_mock(self):
+        m = create_autospec(int)
+        int(m)
+        m.reset_mock()
+        self.assertEqual(m.__int__.call_count, 0)
+
+
+    def test_mocking_unbound_methods(self):
+        class Foo(object):
+            def foo(self, foo):
+                pass
+        p = patch.object(Foo, 'foo')
+        mock_foo = p.start()
+        Foo().foo(1)
+
+        mock_foo.assert_called_with(1)
+
+
+    @unittest2.expectedFailure
+    def test_create_autospec_unbound_methods(self):
+        # see issue 128
+        class Foo(object):
+            def foo(self):
+                pass
+
+        klass = create_autospec(Foo)
+        instance = klass()
+        self.assertRaises(TypeError, instance.foo, 1)
+
+        # Note: no type checking on the "self" parameter
+        klass.foo(1)
+        klass.foo.assert_called_with(1)
+        self.assertRaises(TypeError, klass.foo)
+
+
+    def test_create_autospec_keyword_arguments(self):
+        class Foo(object):
+            a = 3
+        m = create_autospec(Foo, a='3')
+        self.assertEqual(m.a, '3')
+
+    @unittest2.skipUnless(inPy3k, "Keyword only arguments Python 3 specific")
+    def test_create_autospec_keyword_only_arguments(self):
+        func_def = "def foo(a, *, b=None):\n    pass\n"
+        namespace = {}
+        exec (func_def, namespace)
+        foo = namespace['foo']
+
+        m = create_autospec(foo)
+        m(1)
+        m.assert_called_with(1)
+        self.assertRaises(TypeError, m, 1, 2)
+
+        m(2, b=3)
+        m.assert_called_with(2, b=3)
+
+    def test_function_as_instance_attribute(self):
+        obj = SomeClass()
+        def f(a):
+            pass
+        obj.f = f
+
+        mock = create_autospec(obj)
+        mock.f('bing')
+        mock.f.assert_called_with('bing')
+
+
+    def test_spec_as_list(self):
+        # because spec as a list of strings in the mock constructor means
+        # something very different we treat a list instance as the type.
+        mock = create_autospec([])
+        mock.append('foo')
+        mock.append.assert_called_with('foo')
+
+        self.assertRaises(AttributeError, getattr, mock, 'foo')
+
+        class Foo(object):
+            foo = []
+
+        mock = create_autospec(Foo)
+        mock.foo.append(3)
+        mock.foo.append.assert_called_with(3)
+        self.assertRaises(AttributeError, getattr, mock.foo, 'foo')
+
+
+    def test_attributes(self):
+        class Sub(SomeClass):
+            attr = SomeClass()
+
+        sub_mock = create_autospec(Sub)
+
+        for mock in (sub_mock, sub_mock.attr):
+            self._check_someclass_mock(mock)
+
+
+    def test_builtin_functions_types(self):
+        # we could replace builtin functions / methods with a function
+        # with *args / **kwargs signature. Using the builtin method type
+        # as a spec seems to work fairly well though.
+        class BuiltinSubclass(list):
+            def bar(self, arg):
+                pass
+            sorted = sorted
+            attr = {}
+
+        mock = create_autospec(BuiltinSubclass)
+        mock.append(3)
+        mock.append.assert_called_with(3)
+        self.assertRaises(AttributeError, getattr, mock.append, 'foo')
+
+        mock.bar('foo')
+        mock.bar.assert_called_with('foo')
+        self.assertRaises(TypeError, mock.bar, 'foo', 'bar')
+        self.assertRaises(AttributeError, getattr, mock.bar, 'foo')
+
+        mock.sorted([1, 2])
+        mock.sorted.assert_called_with([1, 2])
+        self.assertRaises(AttributeError, getattr, mock.sorted, 'foo')
+
+        mock.attr.pop(3)
+        mock.attr.pop.assert_called_with(3)
+        self.assertRaises(AttributeError, getattr, mock.attr, 'foo')
+
+
+    def test_method_calls(self):
+        class Sub(SomeClass):
+            attr = SomeClass()
+
+        mock = create_autospec(Sub)
+        mock.one(1, 2)
+        mock.two()
+        mock.three(3)
+
+        expected = [call.one(1, 2), call.two(), call.three(3)]
+        self.assertEqual(mock.method_calls, expected)
+
+        mock.attr.one(1, 2)
+        mock.attr.two()
+        mock.attr.three(3)
+
+        expected.extend(
+            [call.attr.one(1, 2), call.attr.two(), call.attr.three(3)]
+        )
+        self.assertEqual(mock.method_calls, expected)
+
+
+    def test_magic_methods(self):
+        class BuiltinSubclass(list):
+            attr = {}
+
+        mock = create_autospec(BuiltinSubclass)
+        self.assertEqual(list(mock), [])
+        self.assertRaises(TypeError, int, mock)
+        self.assertRaises(TypeError, int, mock.attr)
+        self.assertEqual(list(mock), [])
+
+        self.assertIsInstance(mock['foo'], MagicMock)
+        self.assertIsInstance(mock.attr['foo'], MagicMock)
+
+
+    def test_spec_set(self):
+        class Sub(SomeClass):
+            attr = SomeClass()
+
+        for spec in (Sub, Sub()):
+            mock = create_autospec(spec, spec_set=True)
+            self._check_someclass_mock(mock)
+
+            self.assertRaises(AttributeError, setattr, mock, 'foo', 'bar')
+            self.assertRaises(AttributeError, setattr, mock.attr, 'foo', 'bar')
+
+
+    def test_descriptors(self):
+        class Foo(object):
+            @classmethod
+            def f(cls, a, b):
+                pass
+            @staticmethod
+            def g(a, b):
+                pass
+
+        class Bar(Foo):
+            pass
+
+        class Baz(SomeClass, Bar):
+            pass
+
+        for spec in (Foo, Foo(), Bar, Bar(), Baz, Baz()):
+            mock = create_autospec(spec)
+            mock.f(1, 2)
+            mock.f.assert_called_once_with(1, 2)
+
+            mock.g(3, 4)
+            mock.g.assert_called_once_with(3, 4)
+
+
+    @unittest2.skipIf(inPy3k, "No old style classes in Python 3")
+    def test_old_style_classes(self):
+        class Foo:
+            def f(self, a, b):
+                pass
+
+        class Bar(Foo):
+            g = Foo()
+
+        for spec in (Foo, Foo(), Bar, Bar()):
+            mock = create_autospec(spec)
+            mock.f(1, 2)
+            mock.f.assert_called_once_with(1, 2)
+
+            self.assertRaises(AttributeError, getattr, mock, 'foo')
+            self.assertRaises(AttributeError, getattr, mock.f, 'foo')
+
+        mock.g.f(1, 2)
+        mock.g.f.assert_called_once_with(1, 2)
+        self.assertRaises(AttributeError, getattr, mock.g, 'foo')
+
+
+    def test_recursive(self):
+        class A(object):
+            def a(self):
+                pass
+            foo = 'foo bar baz'
+            bar = foo
+
+        A.B = A
+        mock = create_autospec(A)
+
+        mock()
+        self.assertFalse(mock.B.called)
+
+        mock.a()
+        mock.B.a()
+        self.assertEqual(mock.method_calls, [call.a(), call.B.a()])
+
+        self.assertIs(A.foo, A.bar)
+        self.assertIsNot(mock.foo, mock.bar)
+        mock.foo.lower()
+        self.assertRaises(AssertionError, mock.bar.lower.assert_called_with)
+
+
+    def test_spec_inheritance_for_classes(self):
+        class Foo(object):
+            def a(self):
+                pass
+            class Bar(object):
+                def f(self):
+                    pass
+
+        class_mock = create_autospec(Foo)
+
+        self.assertIsNot(class_mock, class_mock())
+
+        for this_mock in class_mock, class_mock():
+            this_mock.a()
+            this_mock.a.assert_called_with()
+            self.assertRaises(TypeError, this_mock.a, 'foo')
+            self.assertRaises(AttributeError, getattr, this_mock, 'b')
+
+        instance_mock = create_autospec(Foo())
+        instance_mock.a()
+        instance_mock.a.assert_called_with()
+        self.assertRaises(TypeError, instance_mock.a, 'foo')
+        self.assertRaises(AttributeError, getattr, instance_mock, 'b')
+
+        # The return value isn't isn't callable
+        self.assertRaises(TypeError, instance_mock)
+
+        instance_mock.Bar.f()
+        instance_mock.Bar.f.assert_called_with()
+        self.assertRaises(AttributeError, getattr, instance_mock.Bar, 'g')
+
+        instance_mock.Bar().f()
+        instance_mock.Bar().f.assert_called_with()
+        self.assertRaises(AttributeError, getattr, instance_mock.Bar(), 'g')
+
+
+    def test_inherit(self):
+        class Foo(object):
+            a = 3
+
+        Foo.Foo = Foo
+
+        # class
+        mock = create_autospec(Foo)
+        instance = mock()
+        self.assertRaises(AttributeError, getattr, instance, 'b')
+
+        attr_instance = mock.Foo()
+        self.assertRaises(AttributeError, getattr, attr_instance, 'b')
+
+        # instance
+        mock = create_autospec(Foo())
+        self.assertRaises(AttributeError, getattr, mock, 'b')
+        self.assertRaises(TypeError, mock)
+
+        # attribute instance
+        call_result = mock.Foo()
+        self.assertRaises(AttributeError, getattr, call_result, 'b')
+
+
+    def test_builtins(self):
+        # used to fail with infinite recursion
+        create_autospec(1)
+
+        create_autospec(int)
+        create_autospec('foo')
+        create_autospec(str)
+        create_autospec({})
+        create_autospec(dict)
+        create_autospec([])
+        create_autospec(list)
+        create_autospec(set())
+        create_autospec(set)
+        create_autospec(1.0)
+        create_autospec(float)
+        create_autospec(1j)
+        create_autospec(complex)
+        create_autospec(False)
+        create_autospec(True)
+
+
+    def test_function(self):
+        def f(a, b):
+            pass
+
+        mock = create_autospec(f)
+        self.assertRaises(TypeError, mock)
+        mock(1, 2)
+        mock.assert_called_with(1, 2)
+
+        f.f = f
+        mock = create_autospec(f)
+        self.assertRaises(TypeError, mock.f)
+        mock.f(3, 4)
+        mock.f.assert_called_with(3, 4)
+
+
+    def test_skip_attributeerrors(self):
+        class Raiser(object):
+            def __get__(self, obj, type=None):
+                if obj is None:
+                    raise AttributeError('Can only be accessed via an instance')
+
+        class RaiserClass(object):
+            raiser = Raiser()
+
+            @staticmethod
+            def existing(a, b):
+                return a + b
+
+        s = create_autospec(RaiserClass)
+        self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3))
+        s.existing(1, 2)
+        self.assertRaises(AttributeError, lambda: s.nonexisting)
+
+        # check we can fetch the raiser attribute and it has no spec
+        obj = s.raiser
+        obj.foo, obj.bar
+
+
+    def test_signature_class(self):
+        class Foo(object):
+            def __init__(self, a, b=3):
+                pass
+
+        mock = create_autospec(Foo)
+
+        self.assertRaises(TypeError, mock)
+        mock(1)
+        mock.assert_called_once_with(1)
+
+        mock(4, 5)
+        mock.assert_called_with(4, 5)
+
+
+    @unittest2.skipIf(inPy3k, 'no old style classes in Python 3')
+    def test_signature_old_style_class(self):
+        class Foo:
+            def __init__(self, a, b=3):
+                pass
+
+        mock = create_autospec(Foo)
+
+        self.assertRaises(TypeError, mock)
+        mock(1)
+        mock.assert_called_once_with(1)
+
+        mock(4, 5)
+        mock.assert_called_with(4, 5)
+
+
+    def test_class_with_no_init(self):
+        # this used to raise an exception
+        # due to trying to get a signature from object.__init__
+        class Foo(object):
+            pass
+        create_autospec(Foo)
+
+
+    @unittest2.skipIf(inPy3k, 'no old style classes in Python 3')
+    def test_old_style_class_with_no_init(self):
+        # this used to raise an exception
+        # due to Foo.__init__ raising an AttributeError
+        class Foo:
+            pass
+        create_autospec(Foo)
+
+
+    def test_signature_callable(self):
+        class Callable(object):
+            def __init__(self):
+                pass
+            def __call__(self, a):
+                pass
+
+        mock = create_autospec(Callable)
+        mock()
+        mock.assert_called_once_with()
+        self.assertRaises(TypeError, mock, 'a')
+
+        instance = mock()
+        self.assertRaises(TypeError, instance)
+        instance(a='a')
+        instance.assert_called_once_with(a='a')
+        instance('a')
+        instance.assert_called_with('a')
+
+        mock = create_autospec(Callable())
+        mock(a='a')
+        mock.assert_called_once_with(a='a')
+        self.assertRaises(TypeError, mock)
+        mock('a')
+        mock.assert_called_with('a')
+
+
+    def test_signature_noncallable(self):
+        class NonCallable(object):
+            def __init__(self):
+                pass
+
+        mock = create_autospec(NonCallable)
+        instance = mock()
+        mock.assert_called_once_with()
+        self.assertRaises(TypeError, mock, 'a')
+        self.assertRaises(TypeError, instance)
+        self.assertRaises(TypeError, instance, 'a')
+
+        mock = create_autospec(NonCallable())
+        self.assertRaises(TypeError, mock)
+        self.assertRaises(TypeError, mock, 'a')
+
+
+    def test_create_autospec_none(self):
+        class Foo(object):
+            bar = None
+
+        mock = create_autospec(Foo)
+        none = mock.bar
+        self.assertNotIsInstance(none, type(None))
+
+        none.foo()
+        none.foo.assert_called_once_with()
+
+
+    def test_autospec_functions_with_self_in_odd_place(self):
+        class Foo(object):
+            def f(a, self):
+                pass
+
+        a = create_autospec(Foo)
+        a.f(self=10)
+        a.f.assert_called_with(self=10)
+
+
+    def test_autospec_property(self):
+        class Foo(object):
+            @property
+            def foo(self):
+                return 3
+
+        foo = create_autospec(Foo)
+        mock_property = foo.foo
+
+        # no spec on properties
+        self.assertTrue(isinstance(mock_property, MagicMock))
+        mock_property(1, 2, 3)
+        mock_property.abc(4, 5, 6)
+        mock_property.assert_called_once_with(1, 2, 3)
+        mock_property.abc.assert_called_once_with(4, 5, 6)
+
+
+    def test_autospec_slots(self):
+        class Foo(object):
+            __slots__ = ['a']
+
+        foo = create_autospec(Foo)
+        mock_slot = foo.a
+
+        # no spec on slots
+        mock_slot(1, 2, 3)
+        mock_slot.abc(4, 5, 6)
+        mock_slot.assert_called_once_with(1, 2, 3)
+        mock_slot.abc.assert_called_once_with(4, 5, 6)
+
+
+class TestCallList(unittest2.TestCase):
+
+    def test_args_list_contains_call_list(self):
+        mock = Mock()
+        self.assertIsInstance(mock.call_args_list, _CallList)
+
+        mock(1, 2)
+        mock(a=3)
+        mock(3, 4)
+        mock(b=6)
+
+        for kall in call(1, 2), call(a=3), call(3, 4), call(b=6):
+            self.assertTrue(kall in mock.call_args_list)
+
+        calls = [call(a=3), call(3, 4)]
+        self.assertTrue(calls in mock.call_args_list)
+        calls = [call(1, 2), call(a=3)]
+        self.assertTrue(calls in mock.call_args_list)
+        calls = [call(3, 4), call(b=6)]
+        self.assertTrue(calls in mock.call_args_list)
+        calls = [call(3, 4)]
+        self.assertTrue(calls in mock.call_args_list)
+
+        self.assertFalse(call('fish') in mock.call_args_list)
+        self.assertFalse([call('fish')] in mock.call_args_list)
+
+
+    def test_call_list_str(self):
+        mock = Mock()
+        mock(1, 2)
+        mock.foo(a=3)
+        mock.foo.bar().baz('fish', cat='dog')
+
+        expected = (
+            "[call(1, 2),\n"
+            " call.foo(a=3),\n"
+            " call.foo.bar(),\n"
+            " call.foo.bar().baz('fish', cat='dog')]"
+        )
+        self.assertEqual(str(mock.mock_calls), expected)
+
+
+    def test_propertymock(self):
+        p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock)
+        mock = p.start()
+        try:
+            SomeClass.one
+            mock.assert_called_once_with()
+
+            s = SomeClass()
+            s.one
+            mock.assert_called_with()
+            self.assertEqual(mock.mock_calls, [call(), call()])
+
+            s.one = 3
+            self.assertEqual(mock.mock_calls, [call(), call(), call(3)])
+        finally:
+            p.stop()
+
+
+    def test_propertymock_returnvalue(self):
+        m = MagicMock()
+        p = PropertyMock()
+        type(m).foo = p
+
+        returned = m.foo
+        p.assert_called_once_with()
+        self.assertIsInstance(returned, MagicMock)
+        self.assertNotIsInstance(returned, PropertyMock)
+
+
+if __name__ == '__main__':
+    unittest2.main()

Added: incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testmagicmethods.py
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testmagicmethods.py?rev=1442010&view=auto
==============================================================================
--- incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testmagicmethods.py (added)
+++ incubator/ambari/branches/branch-1.2/ambari-common/src/test/python/mock/tests/testmagicmethods.py Mon Feb  4 02:23:55 2013
@@ -0,0 +1,486 @@
+# Copyright (C) 2007-2012 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 unittest2, inPy3k
+
+try:
+    unicode
+except NameError:
+    # Python 3
+    unicode = str
+    long = int
+
+import inspect
+import sys
+from mock import Mock, MagicMock, _magics
+
+
+
+class TestMockingMagicMethods(unittest2.TestCase):
+
+    def test_deleting_magic_methods(self):
+        mock = Mock()
+        self.assertFalse(hasattr(mock, '__getitem__'))
+
+        mock.__getitem__ = Mock()
+        self.assertTrue(hasattr(mock, '__getitem__'))
+
+        del mock.__getitem__
+        self.assertFalse(hasattr(mock, '__getitem__'))
+
+
+    def test_magicmock_del(self):
+        mock = MagicMock()
+        # before using getitem
+        del mock.__getitem__
+        self.assertRaises(TypeError, lambda: mock['foo'])
+
+        mock = MagicMock()
+        # this time use it first
+        mock['foo']
+        del mock.__getitem__
+        self.assertRaises(TypeError, lambda: mock['foo'])
+
+
+    def test_magic_method_wrapping(self):
+        mock = Mock()
+        def f(self, name):
+            return self, 'fish'
+
+        mock.__getitem__ = f
+        self.assertFalse(mock.__getitem__ is f)
+        self.assertEqual(mock['foo'], (mock, 'fish'))
+        self.assertEqual(mock.__getitem__('foo'), (mock, 'fish'))
+
+        mock.__getitem__ = mock
+        self.assertTrue(mock.__getitem__ is mock)
+
+
+    def test_magic_methods_isolated_between_mocks(self):
+        mock1 = Mock()
+        mock2 = Mock()
+
+        mock1.__iter__ = Mock(return_value=iter([]))
+        self.assertEqual(list(mock1), [])
+        self.assertRaises(TypeError, lambda: list(mock2))
+
+
+    def test_repr(self):
+        mock = Mock()
+        self.assertEqual(repr(mock), "<Mock id='%s'>" % id(mock))
+        mock.__repr__ = lambda s: 'foo'
+        self.assertEqual(repr(mock), 'foo')
+
+
+    def test_str(self):
+        mock = Mock()
+        self.assertEqual(str(mock), object.__str__(mock))
+        mock.__str__ = lambda s: 'foo'
+        self.assertEqual(str(mock), 'foo')
+
+
+    @unittest2.skipIf(inPy3k, "no unicode in Python 3")
+    def test_unicode(self):
+        mock = Mock()
+        self.assertEqual(unicode(mock), unicode(str(mock)))
+
+        mock.__unicode__ = lambda s: unicode('foo')
+        self.assertEqual(unicode(mock), unicode('foo'))
+
+
+    def test_dict_methods(self):
+        mock = Mock()
+
+        self.assertRaises(TypeError, lambda: mock['foo'])
+        def _del():
+            del mock['foo']
+        def _set():
+            mock['foo'] = 3
+        self.assertRaises(TypeError, _del)
+        self.assertRaises(TypeError, _set)
+
+        _dict = {}
+        def getitem(s, name):
+            return _dict[name]
+        def setitem(s, name, value):
+            _dict[name] = value
+        def delitem(s, name):
+            del _dict[name]
+
+        mock.__setitem__ = setitem
+        mock.__getitem__ = getitem
+        mock.__delitem__ = delitem
+
+        self.assertRaises(KeyError, lambda: mock['foo'])
+        mock['foo'] = 'bar'
+        self.assertEqual(_dict, {'foo': 'bar'})
+        self.assertEqual(mock['foo'], 'bar')
+        del mock['foo']
+        self.assertEqual(_dict, {})
+
+
+    def test_numeric(self):
+        original = mock = Mock()
+        mock.value = 0
+
+        self.assertRaises(TypeError, lambda: mock + 3)
+
+        def add(self, other):
+            mock.value += other
+            return self
+        mock.__add__ = add
+        self.assertEqual(mock + 3, mock)
+        self.assertEqual(mock.value, 3)
+
+        del mock.__add__
+        def iadd(mock):
+            mock += 3
+        self.assertRaises(TypeError, iadd, mock)
+        mock.__iadd__ = add
+        mock += 6
+        self.assertEqual(mock, original)
+        self.assertEqual(mock.value, 9)
+
+        self.assertRaises(TypeError, lambda: 3 + mock)
+        mock.__radd__ = add
+        self.assertEqual(7 + mock, mock)
+        self.assertEqual(mock.value, 16)
+
+
+    @unittest2.skipIf(inPy3k, 'no truediv in Python 3')
+    def test_truediv(self):
+        mock = MagicMock()
+        mock.__truediv__.return_value = 6
+
+        context = {'mock': mock}
+        code = 'from __future__ import division\nresult = mock / 7\n'
+        exec(code, context)
+        self.assertEqual(context['result'], 6)
+
+        mock.__rtruediv__.return_value = 3
+        code = 'from __future__ import division\nresult = 2 / mock\n'
+        exec(code, context)
+        self.assertEqual(context['result'], 3)
+
+
+    @unittest2.skipIf(not inPy3k, 'truediv is available in Python 2')
+    def test_no_truediv(self):
+        self.assertRaises(
+            AttributeError, getattr, MagicMock(), '__truediv__'
+        )
+        self.assertRaises(
+            AttributeError, getattr, MagicMock(), '__rtruediv__'
+        )
+
+
+    def test_hash(self):
+        mock = Mock()
+        # test delegation
+        self.assertEqual(hash(mock), Mock.__hash__(mock))
+
+        def _hash(s):
+            return 3
+        mock.__hash__ = _hash
+        self.assertEqual(hash(mock), 3)
+
+
+    def test_nonzero(self):
+        m = Mock()
+        self.assertTrue(bool(m))
+
+        nonzero = lambda s: False
+        if not inPy3k:
+            m.__nonzero__ = nonzero
+        else:
+            m.__bool__ = nonzero
+
+        self.assertFalse(bool(m))
+
+
+    def test_comparison(self):
+        # note: this test fails with Jython 2.5.1 due to a Jython bug
+        #       it is fixed in jython 2.5.2
+        if not inPy3k:
+            # incomparable in Python 3
+            self. assertEqual(Mock() < 3, object() < 3)
+            self. assertEqual(Mock() > 3, object() > 3)
+            self. assertEqual(Mock() <= 3, object() <= 3)
+            self. assertEqual(Mock() >= 3, object() >= 3)
+        else:
+            self.assertRaises(TypeError, lambda: MagicMock() < object())
+            self.assertRaises(TypeError, lambda: object() < MagicMock())
+            self.assertRaises(TypeError, lambda: MagicMock() < MagicMock())
+            self.assertRaises(TypeError, lambda: MagicMock() > object())
+            self.assertRaises(TypeError, lambda: object() > MagicMock())
+            self.assertRaises(TypeError, lambda: MagicMock() > MagicMock())
+            self.assertRaises(TypeError, lambda: MagicMock() <= object())
+            self.assertRaises(TypeError, lambda: object() <= MagicMock())
+            self.assertRaises(TypeError, lambda: MagicMock() <= MagicMock())
+            self.assertRaises(TypeError, lambda: MagicMock() >= object())
+            self.assertRaises(TypeError, lambda: object() >= MagicMock())
+            self.assertRaises(TypeError, lambda: MagicMock() >= MagicMock())
+
+        mock = Mock()
+        def comp(s, o):
+            return True
+        mock.__lt__ = mock.__gt__ = mock.__le__ = mock.__ge__ = comp
+        self. assertTrue(mock < 3)
+        self. assertTrue(mock > 3)
+        self. assertTrue(mock <= 3)
+        self. assertTrue(mock >= 3)
+
+
+    def test_equality(self):
+        for mock in Mock(), MagicMock():
+            self.assertEqual(mock == mock, True)
+            self.assertIsInstance(mock == mock, bool)
+            self.assertEqual(mock != mock, False)
+            self.assertIsInstance(mock != mock, bool)
+            self.assertEqual(mock == object(), False)
+            self.assertEqual(mock != object(), True)
+
+            def eq(self, other):
+                return other == 3
+            mock.__eq__ = eq
+            self.assertTrue(mock == 3)
+            self.assertFalse(mock == 4)
+
+            def ne(self, other):
+                return other == 3
+            mock.__ne__ = ne
+            self.assertTrue(mock != 3)
+            self.assertFalse(mock != 4)
+
+        mock = MagicMock()
+        mock.__eq__.return_value = True
+        self.assertIsInstance(mock == 3, bool)
+        self.assertEqual(mock == 3, True)
+
+        mock.__ne__.return_value = False
+        self.assertIsInstance(mock != 3, bool)
+        self.assertEqual(mock != 3, False)
+
+
+    def test_len_contains_iter(self):
+        mock = Mock()
+
+        self.assertRaises(TypeError, len, mock)
+        self.assertRaises(TypeError, iter, mock)
+        self.assertRaises(TypeError, lambda: 'foo' in mock)
+
+        mock.__len__ = lambda s: 6
+        self.assertEqual(len(mock), 6)
+
+        mock.__contains__ = lambda s, o: o == 3
+        self.assertTrue(3 in mock)
+        self.assertFalse(6 in mock)
+
+        mock.__iter__ = lambda s: iter('foobarbaz')
+        self.assertEqual(list(mock), list('foobarbaz'))
+
+
+    def test_magicmock(self):
+        mock = MagicMock()
+
+        mock.__iter__.return_value = iter([1, 2, 3])
+        self.assertEqual(list(mock), [1, 2, 3])
+
+        name = '__nonzero__'
+        other = '__bool__'
+        if inPy3k:
+            name, other = other, name
+        getattr(mock, name).return_value = False
+        self.assertFalse(hasattr(mock, other))
+        self.assertFalse(bool(mock))
+
+        for entry in _magics:
+            self.assertTrue(hasattr(mock, entry))
+        self.assertFalse(hasattr(mock, '__imaginery__'))
+
+
+    def test_magic_mock_equality(self):
+        mock = MagicMock()
+        self.assertIsInstance(mock == object(), bool)
+        self.assertIsInstance(mock != object(), bool)
+
+        self.assertEqual(mock == object(), False)
+        self.assertEqual(mock != object(), True)
+        self.assertEqual(mock == mock, True)
+        self.assertEqual(mock != mock, False)
+
+
+    def test_magicmock_defaults(self):
+        mock = MagicMock()
+        self.assertEqual(int(mock), 1)
+        self.assertEqual(complex(mock), 1j)
+        self.assertEqual(float(mock), 1.0)
+        self.assertEqual(long(mock), long(1))
+        self.assertNotIn(object(), mock)
+        self.assertEqual(len(mock), 0)
+        self.assertEqual(list(mock), [])
+        self.assertEqual(hash(mock), object.__hash__(mock))
+        self.assertEqual(str(mock), object.__str__(mock))
+        self.assertEqual(unicode(mock), object.__str__(mock))
+        self.assertIsInstance(unicode(mock), unicode)
+        self.assertTrue(bool(mock))
+        if not inPy3k:
+            self.assertEqual(oct(mock), '1')
+        else:
+            # in Python 3 oct and hex use __index__
+            # so these tests are for __index__ in py3k
+            self.assertEqual(oct(mock), '0o1')
+        self.assertEqual(hex(mock), '0x1')
+        # how to test __sizeof__ ?
+
+
+    @unittest2.skipIf(inPy3k, "no __cmp__ in Python 3")
+    def test_non_default_magic_methods(self):
+        mock = MagicMock()
+        self.assertRaises(AttributeError, lambda: mock.__cmp__)
+
+        mock = Mock()
+        mock.__cmp__ = lambda s, o: 0
+
+        self.assertEqual(mock, object())
+
+
+    def test_magic_methods_and_spec(self):
+        class Iterable(object):
+            def __iter__(self):
+                pass
+
+        mock = Mock(spec=Iterable)
+        self.assertRaises(AttributeError, lambda: mock.__iter__)
+
+        mock.__iter__ = Mock(return_value=iter([]))
+        self.assertEqual(list(mock), [])
+
+        class NonIterable(object):
+            pass
+        mock = Mock(spec=NonIterable)
+        self.assertRaises(AttributeError, lambda: mock.__iter__)
+
+        def set_int():
+            mock.__int__ = Mock(return_value=iter([]))
+        self.assertRaises(AttributeError, set_int)
+
+        mock = MagicMock(spec=Iterable)
+        self.assertEqual(list(mock), [])
+        self.assertRaises(AttributeError, set_int)
+
+
+    def test_magic_methods_and_spec_set(self):
+        class Iterable(object):
+            def __iter__(self):
+                pass
+
+        mock = Mock(spec_set=Iterable)
+        self.assertRaises(AttributeError, lambda: mock.__iter__)
+
+        mock.__iter__ = Mock(return_value=iter([]))
+        self.assertEqual(list(mock), [])
+
+        class NonIterable(object):
+            pass
+        mock = Mock(spec_set=NonIterable)
+        self.assertRaises(AttributeError, lambda: mock.__iter__)
+
+        def set_int():
+            mock.__int__ = Mock(return_value=iter([]))
+        self.assertRaises(AttributeError, set_int)
+
+        mock = MagicMock(spec_set=Iterable)
+        self.assertEqual(list(mock), [])
+        self.assertRaises(AttributeError, set_int)
+
+
+    def test_setting_unsupported_magic_method(self):
+        mock = MagicMock()
+        def set_setattr():
+            mock.__setattr__ = lambda self, name: None
+        self.assertRaisesRegexp(AttributeError,
+            "Attempting to set unsupported magic method '__setattr__'.",
+            set_setattr
+        )
+
+
+    def test_attributes_and_return_value(self):
+        mock = MagicMock()
+        attr = mock.foo
+        def _get_type(obj):
+            # the type of every mock (or magicmock) is a custom subclass
+            # so the real type is the second in the mro
+            return type(obj).__mro__[1]
+        self.assertEqual(_get_type(attr), MagicMock)
+
+        returned = mock()
+        self.assertEqual(_get_type(returned), MagicMock)
+
+
+    def test_magic_methods_are_magic_mocks(self):
+        mock = MagicMock()
+        self.assertIsInstance(mock.__getitem__, MagicMock)
+
+        mock[1][2].__getitem__.return_value = 3
+        self.assertEqual(mock[1][2][3], 3)
+
+
+    def test_magic_method_reset_mock(self):
+        mock = MagicMock()
+        str(mock)
+        self.assertTrue(mock.__str__.called)
+        mock.reset_mock()
+        self.assertFalse(mock.__str__.called)
+
+
+    @unittest2.skipUnless(sys.version_info[:2] >= (2, 6),
+                          "__dir__ not available until Python 2.6 or later")
+    def test_dir(self):
+        # overriding the default implementation
+        for mock in Mock(), MagicMock():
+            def _dir(self):
+                return ['foo']
+            mock.__dir__ = _dir
+            self.assertEqual(dir(mock), ['foo'])
+
+
+    @unittest2.skipIf('PyPy' in sys.version, "This fails differently on pypy")
+    def test_bound_methods(self):
+        m = Mock()
+
+        # XXXX should this be an expected failure instead?
+
+        # this seems like it should work, but is hard to do without introducing
+        # other api inconsistencies. Failure message could be better though.
+        m.__iter__ = [3].__iter__
+        self.assertRaises(TypeError, iter, m)
+
+
+    def test_magic_method_type(self):
+        class Foo(MagicMock):
+            pass
+
+        foo = Foo()
+        self.assertIsInstance(foo.__int__, Foo)
+
+
+    def test_descriptor_from_class(self):
+        m = MagicMock()
+        type(m).__str__.return_value = 'foo'
+        self.assertEqual(str(m), 'foo')
+
+
+    def test_iterable_as_iter_return_value(self):
+        m = MagicMock()
+        m.__iter__.return_value = [1, 2, 3]
+        self.assertEqual(list(m), [1, 2, 3])
+        self.assertEqual(list(m), [1, 2, 3])
+
+        m.__iter__.return_value = iter([4, 5, 6])
+        self.assertEqual(list(m), [4, 5, 6])
+        self.assertEqual(list(m), [])
+
+
+if __name__ == '__main__':
+    unittest2.main()