From c37ce12ae1d73c4ba9bcf933a08a6f1d12b081a0 Mon Sep 17 00:00:00 2001 From: Lewis Haley Date: Tue, 10 Nov 2015 16:10:31 +0000 Subject: case.{Function,Method}TestCase: deobfuscate references to self.arg_repr 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. --- nose/case.py | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/nose/case.py b/nose/case.py index e5a407a..c585c2b 100644 --- a/nose/case.py +++ b/nose/case.py @@ -199,7 +199,7 @@ class TestBase(unittest.TestCase): 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) @@ -277,32 +277,27 @@ class FunctionTestCase(TestBase): 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): @@ -351,7 +346,7 @@ class MethodTestCase(TestBase): 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: @@ -359,8 +354,8 @@ class MethodTestCase(TestBase): 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__ @@ -385,15 +380,10 @@ class MethodTestCase(TestBase): 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 -- cgit v1.2.1