Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lentil
Created April 18, 2011 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lentil/926496 to your computer and use it in GitHub Desktop.
Save lentil/926496 to your computer and use it in GitHub Desktop.
from nose.tools import eq_
from patch import Patch
from unittest import TestCase
class SampleOld:
SETTING = None
def __init__(self, msg):
self.msg = msg
def hello(self):
return self.msg
def goodbye(self):
return 'goodbye'
class SampleNew(object):
SETTING = None
def __init__(self, msg):
self.msg = msg
def hello(self):
return self.msg
def goodbye(self):
return 'goodbye'
def outer_hello(self):
return self.msg * 3
class TestPatch(TestCase):
def shout(self):
return self.msg.upper()
def test_patch_new_instance(self):
s = SampleNew('hello')
eq_('hello', s.hello())
with Patch(s, hello=outer_hello):
eq_('hellohellohello', s.hello())
eq_('hi', SampleNew('hi').hello())
with Patch(s, hello=self.shout):
eq_('HELLO', s.hello())
eq_('hello', s.hello())
def test_patch_old_instance(self):
s = SampleOld('hello')
eq_('hello', s.hello())
with Patch(s, hello=outer_hello):
eq_('hellohellohello', s.hello())
eq_('hi', SampleOld('hi').hello())
with Patch(s, hello=self.shout):
eq_('HELLO', s.hello())
eq_('hello', s.hello())
def test_patch_new_class(self):
s = SampleNew('hello')
eq_('hello', s.hello())
with Patch(SampleNew, hello=outer_hello):
eq_('hellohellohello', s.hello())
eq_('hihihi', SampleNew('hi').hello())
with Patch(s, hello=self.shout):
eq_('HELLO', s.hello())
eq_('hello', s.hello())
def test_patch_old_class(self):
s = SampleOld('hello')
eq_('hello', s.hello())
with Patch(SampleOld, hello=outer_hello):
eq_('hellohellohello', s.hello())
eq_('hihihi', SampleOld('hi').hello())
with Patch(s, hello=self.shout):
eq_('HELLO', s.hello())
eq_('hello', s.hello())
def test_blank_out_methods_new(self):
s = SampleNew('hello')
eq_('hello', s.hello())
with Patch(SampleNew, hello=None):
eq_(None, s.hello())
eq_('hello', s.hello())
def test_blank_out_methods_old(self):
s = SampleOld('hello')
eq_('hello', s.hello())
with Patch(SampleOld, hello=None):
eq_(None, s.hello())
eq_('hello', s.hello())
def test_restore_from_null_property_new(self):
with Patch(SampleNew, SETTING='stuff'):
eq_('stuff', SampleNew.SETTING)
eq_(None, SampleNew.SETTING)
def test_restore_from_null_property_old(self):
with Patch(SampleOld, SETTING='stuff'):
eq_('stuff', SampleOld.SETTING)
eq_(None, SampleOld.SETTING)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment