[pymox] r75 committed - Patch for Issue #47, provided by gps@google.com

3 views
Skip to first unread message

codesite...@google.com

unread,
Nov 16, 2012, 2:18:06 PM11/16/12
to mox-d...@googlegroups.com
Revision: 75
Author: smiddlek
Date: Fri Nov 16 11:17:52 2012
Log: Patch for Issue #47, provided by g...@google.com



http://code.google.com/p/pymox/source/detail?r=75

Modified:
/trunk/mox.py
/trunk/mox_test.py
/trunk/mox_test_helper.py

=======================================
--- /trunk/mox.py Mon Nov 12 10:59:51 2012
+++ /trunk/mox.py Fri Nov 16 11:17:52 2012
@@ -61,6 +61,10 @@
my_mox.VerifyAll()
"""

+try:
+ import abc
+except ImportError:
+ abc = None # Python 2.5 and earlier
from collections import deque
import difflib
import inspect
@@ -260,6 +264,8 @@

# A list of types that may be stubbed out with a MockObjectFactory.
_USE_MOCK_FACTORY = [types.ClassType, types.ObjectType, types.TypeType]
+ if abc:
+ _USE_MOCK_FACTORY.append(abc.ABCMeta)

def __init__(self):
"""Initialize a new Mox."""
=======================================
--- /trunk/mox_test.py Mon Nov 12 10:59:51 2012
+++ /trunk/mox_test.py Fri Nov 16 11:17:52 2012
@@ -19,6 +19,7 @@
import cStringIO
import unittest
import re
+import sys

import mox

@@ -1898,6 +1899,39 @@
self.assertEquals('mock', actual_one)
self.assertEquals('called mock', actual_two)

+ try:
+ import abc
+ # I'd use the unittest skipping decorators for this but I want to
support
+ # older versions of Python that don't have them.
+ def testStubOutClass_ABCMeta(self):
+ self.mox.StubOutClassWithMocks(mox_test_helper,
+ 'CallableSubclassOfMyDictABC')
+ mock_foo = mox_test_helper.CallableSubclassOfMyDictABC(foo='!mock
bar')
+ mock_foo['foo'].AndReturn('mock bar')
+ mock_spam = mox_test_helper.CallableSubclassOfMyDictABC(spam='!mock
eggs')
+ mock_spam('beans').AndReturn('called mock')
+
+ self.mox.ReplayAll()
+
+ foo = mox_test_helper.CallableSubclassOfMyDictABC(foo='!mock bar')
+ actual_foo_bar = foo['foo']
+
+ spam = mox_test_helper.CallableSubclassOfMyDictABC(spam='!mock eggs')
+ actual_spam = spam('beans')
+
+ self.mox.VerifyAll()
+ self.mox.UnsetStubs()
+
+ # Verify the correct mocks were returned
+ self.assertEquals(mock_foo, foo)
+ self.assertEquals(mock_spam, spam)
+
+ # Verify
+ self.assertEquals('mock bar', actual_foo_bar)
+ self.assertEquals('called mock', actual_spam)
+ except ImportError:
+ print >>sys.stderr, "testStubOutClass_ABCMeta. ... Skipped - no abc
module"
+
def testStubOutClass_NotAClass(self):
self.assertRaises(TypeError, self.mox.StubOutClassWithMocks,
mox_test_helper, 'MyTestFunction')
=======================================
--- /trunk/mox_test_helper.py Fri Sep 3 10:00:55 2010
+++ /trunk/mox_test_helper.py Fri Nov 16 11:17:52 2012
@@ -118,6 +118,25 @@
return 'Not mock'


+try:
+ import abc
+
+ class MyDictABC(object):
+ __metaclass__ = abc.ABCMeta
+
+ MyDictABC.register(dict)
+
+ class CallableSubclassOfMyDictABC(MyDictABC):
+
+ def __call__(self, one):
+ return 'Not mock'
+
+ def __getitem__(self, key, default=None):
+ return 'Not mock'
+except ImportError:
+ pass # Python 2.5 or earlier
+
+
def MyTestFunction(one, two, nine=None):
pass

Reply all
Reply to author
Forward
0 new messages