Skip to content

Commit

Permalink
case.{Function,Method}TestCase: deobfuscate references to self.arg_repr
Browse files Browse the repository at this point in the history
Previously, both `FunctionTestCase` and `MethodTestCase` created local
`arg` variables in their `__str__` methods. These variables were simply
references to `self.arg_repr`, so this commit deobfuscates this reference
by modifying the `_descriptors` method for each so that *only the test
name* is returned and not the `arg_repr` also (renaming `_descriptors` to
`_descriptor` in the process), and then referring to `self.arg_repr`
directly.

This makes the `__str__` easier to understand because it is immediately
obvious that we're only dealing with the string-representation of the args
and *not* the actual args themselves.
  • Loading branch information
LewisHaley committed Nov 10, 2015
1 parent 0164f01 commit c37ce12
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions nose/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def runTest(self):
def shortDescription(self):
if hasattr(self.test, 'description'):
return self.test.description
func, arg = self._descriptors()
func = self._descriptor()
doc = getattr(func, '__doc__', None)
if not doc:
doc = str(self)
Expand Down Expand Up @@ -277,32 +277,27 @@ def tearDown(self):
try_run(self.test, names)

def __str__(self):
func, arg = self._descriptors()
func = self._descriptor()
if hasattr(func, 'compat_func_name'):
name = func.compat_func_name
else:
name = func.__name__
name = "%s.%s" % (func.__module__, name)
if not arg == '()':
name = "%s%s" % (name, arg)
if not self.arg_repr == '()':
name = "%s%s" % (name, self.arg_repr)
# FIXME need to include the full dir path to disambiguate
# in cases where test module of the same name was seen in
# another directory (old fromDirectory)
return name
__repr__ = __str__

def _descriptors(self):
"""Get the descriptors of the test function: the function and
arguments that will be used to construct the test name. In
most cases, this is the function itself and no arguments. For
tests generated by generator functions, the original
(generator) function and args passed to the generated function
are returned.
def _descriptor(self):
"""Get the descriptor of the test method.
If a `descriptor` was specified for __init__, then it is returned, else
the test method (callable object) is returned.
"""
if self.descriptor:
return self.descriptor, self.arg_repr
else:
return self.test, self.arg_repr
return self.descriptor if self.descriptor is not None else self.test


class MethodTestCase(TestBase):
Expand Down Expand Up @@ -351,16 +346,16 @@ def __init__(self, method, test=None, arg=tuple(), descriptor=None):
TestBase.__init__(self)

def __str__(self):
func, arg = self._descriptors()
func = self._descriptor()
if hasattr(func, 'compat_func_name'):
name = func.compat_func_name
else:
name = func.__name__
name = "%s.%s.%s" % (self.cls.__module__,
self.cls.__name__,
name)
if not arg == '()':
name = "%s%s" % (name, arg)
if not self.arg_repr == '()':
name = "%s%s" % (name, self.arg_repr)
return name
__repr__ = __str__

Expand All @@ -385,15 +380,10 @@ def setUp(self):
def tearDown(self):
try_run(self.inst, ('teardown', 'tearDown'))

def _descriptors(self):
"""Get the descriptors of the test method: the method and
arguments that will be used to construct the test name. In
most cases, this is the method itself and no arguments. For
tests generated by generator methods, the original
(generator) method and args passed to the generated method
or function are returned.
def _descriptor(self):
"""Get the descriptor of the test method.
If a `descriptor` was specified for __init__, then it is returned, else
the test method (callable object) is returned.
"""
if self.descriptor:
return self.descriptor, self.arg_repr
else:
return self.method, self.arg_repr
return self.descriptor if self.descriptor is not None else self.method

0 comments on commit c37ce12

Please # to comment.