Add tests for decorated argspec preservation

Tests are added that show that several of the decorators, when
used on functions, lose the argspec. This causes the documentation
to be generated incorrectly since the args aren't shown for
decorated functions.

The tests show that some of the decorators work as expected.

Change-Id: Id024f5110082a88c30e71e991764320b3aed07aa
This commit is contained in:
Brant Knudson 2016-04-13 15:51:52 -05:00
parent 3165b44e51
commit 1f5816ad02
1 changed files with 52 additions and 5 deletions

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import inspect
import warnings import warnings
import debtcollector import debtcollector
@ -28,6 +29,10 @@ def blip_blop(blip=1, blop=1):
return (blip, blop) return (blip, blop)
def blip_blop_unwrapped(blip=1, blop=1):
return (blip, blop)
@renames.renamed_kwarg('blip', 'blop', category=PendingDeprecationWarning) @renames.renamed_kwarg('blip', 'blop', category=PendingDeprecationWarning)
def blip_blop_2(blip=1, blop=1): def blip_blop_2(blip=1, blop=1):
return (blip, blop) return (blip, blop)
@ -43,6 +48,10 @@ def blip_blop_blip(type='cat'):
return "The %s meowed quietly" % type return "The %s meowed quietly" % type
def blip_blop_blip_unwrapped(type='cat'):
return "The %s meowed quietly" % type
class WoofWoof(object): class WoofWoof(object):
@property @property
def bark(self): def bark(self):
@ -67,14 +76,14 @@ class WoofWoof(object):
class KittyKat(object): class KittyKat(object):
@moves.moved_method('supermeow') @moves.moved_method('supermeow')
def meow(self): def meow(self, volume=11):
return self.supermeow() return self.supermeow(volume)
@moves.moved_method('supermeow', category=PendingDeprecationWarning) @moves.moved_method('supermeow', category=PendingDeprecationWarning)
def maow(self): def maow(self, volume=11):
return self.supermeow() return self.supermeow(volume)
def supermeow(self): def supermeow(self, volume=11):
return 'supermeow' return 'supermeow'
@ -99,6 +108,10 @@ def crimson_lightning(fake_input=None):
return fake_input return fake_input
def crimson_lightning_unwrapped(fake_input=None):
return fake_input
@removals.remove(category=PendingDeprecationWarning) @removals.remove(category=PendingDeprecationWarning)
def crimson_lightning_to_remove(fake_input=None): def crimson_lightning_to_remove(fake_input=None):
return fake_input return fake_input
@ -343,6 +356,11 @@ class MovedMethodTest(test_base.TestCase):
self.assertEqual('supermeow', c.supermeow()) self.assertEqual('supermeow', c.supermeow())
self.assertEqual(0, len(capture)) self.assertEqual(0, len(capture))
def test_keeps_argspec(self):
# FIXME(blk): This should be assertEqual!
self.assertNotEqual(inspect.getargspec(KittyKat.supermeow),
inspect.getargspec(KittyKat.meow))
class RenamedKwargTest(test_base.TestCase): class RenamedKwargTest(test_base.TestCase):
def test_basics(self): def test_basics(self):
@ -397,6 +415,13 @@ class RenamedKwargTest(test_base.TestCase):
self.assertEqual(2, blip_blop_3(blop=2)) self.assertEqual(2, blip_blop_3(blop=2))
self.assertEqual(0, len(capture)) self.assertEqual(0, len(capture))
def test_argspec(self):
# The decorated function keeps its argspec.
# FIXME(bknudson): This isn't working right, should be assertEqual!
self.assertNotEqual(inspect.getargspec(blip_blop_unwrapped),
inspect.getargspec(blip_blop))
class UpdatedArgsTest(test_base.TestCase): class UpdatedArgsTest(test_base.TestCase):
def test_basic(self): def test_basic(self):
@ -415,6 +440,11 @@ class UpdatedArgsTest(test_base.TestCase):
blip_blop_blip(type='kitten')) blip_blop_blip(type='kitten'))
self.assertEqual(0, len(capture)) self.assertEqual(0, len(capture))
def test_argspec_preserved(self):
# FIXME(bknudson): This should be assertEqual!
self.assertNotEqual(inspect.getargspec(blip_blop_blip_unwrapped),
inspect.getargspec(blip_blop_blip))
class RemovalTests(test_base.TestCase): class RemovalTests(test_base.TestCase):
def test_function_args(self): def test_function_args(self):
@ -423,6 +453,12 @@ class RemovalTests(test_base.TestCase):
def test_function_noargs(self): def test_function_noargs(self):
self.assertTrue(red_comet()) self.assertTrue(red_comet())
def test_function_keeps_argspec(self):
# The decorated function keeps its argspec.
self.assertEqual(
inspect.getargspec(crimson_lightning_unwrapped),
inspect.getargspec(crimson_lightning))
def test_deprecated_kwarg(self): def test_deprecated_kwarg(self):
@removals.removed_kwarg('b') @removals.removed_kwarg('b')
@ -441,6 +477,17 @@ class RemovalTests(test_base.TestCase):
self.assertEqual(2, f()) self.assertEqual(2, f())
self.assertEqual(0, len(capture)) self.assertEqual(0, len(capture))
def test_removed_kwarg_keeps_argspec(self):
@removals.removed_kwarg('b')
def f(b=2):
return b
def f_unwrapped(b=2):
return b
self.assertEqual(inspect.getargspec(f_unwrapped),
inspect.getargspec(f))
def test_pending_deprecated_kwarg(self): def test_pending_deprecated_kwarg(self):
@removals.removed_kwarg('b', category=PendingDeprecationWarning) @removals.removed_kwarg('b', category=PendingDeprecationWarning)