[Piglit] [PATCH 3/7] grouptools_tests.py: Drop some very complex testing mechanisms

Dylan Baker baker.dylan.c at gmail.com
Thu Mar 12 15:42:06 PDT 2015


These mechanisms were very much of the vein, "I found this cool new
idea", and as often as not, they're bad ideas. This one especially.

This reduces the number of tests, but really doesn't reduce coverage,
since most of what is covered by these tests will disappear in the
following patches of this series.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/tests/grouptools_tests.py | 115 ++++--------------------------------
 1 file changed, 12 insertions(+), 103 deletions(-)

diff --git a/framework/tests/grouptools_tests.py b/framework/tests/grouptools_tests.py
index de5aaba..a6a89a5 100644
--- a/framework/tests/grouptools_tests.py
+++ b/framework/tests/grouptools_tests.py
@@ -21,7 +21,6 @@
 """Module with tests for grouptools."""
 
 from __future__ import print_function
-import inspect
 
 import nose.tools as nt
 
@@ -29,100 +28,6 @@ import framework.grouptools as grouptools
 import framework.tests.utils as utils
 
 
-def description(desc):
-    """Set description on a bound method.
-
-    This is an ugly little mother that does something awful, it sets the
-    description attribute on a bound method. This allows a bound method to be
-    passed from a test generator with a description.
-
-    The string will be formated passing self.__dict__ to str.vformat, so any
-    values that should be filled in should have the same names as the
-    attributes of the class
-
-    """
-    def wrapper(func):
-        func.description = desc
-        return func
-    return wrapper
-
-
-class _SharedFunctionTest(object):  # pylint: disable=too-few-public-methods
-    """Magic test class."""
-    def __init__(self, name, function, input_, expected, explode=False):
-        # pylint: disable=too-many-arguments
-        self.input = input_
-        self.expected = expected
-        self.name = name
-        self.function = function
-        self.explode = explode
-
-    def __iter__(self):
-        """Iterate over the test methods of this class.
-
-        Like the description wrapper this is ugly, it iterates over the bound
-        methods of the class looking for those who's names start with test_,
-        then it uses getatter to get the coresponding method, then formats that
-        description string of that method with vformat, and finally yields the
-        test.
-
-        """
-        # Not exactly sure why x() needs to be called, but it does.
-        wrapper = lambda x: x()
-
-        for name, test in inspect.getmembers(self, inspect.ismethod):
-            if name.startswith('test_'):
-                wrapper.description = test.description.format(**self.__dict__)
-                yield wrapper, test
-
-
-class _GroupToolsTest(_SharedFunctionTest):
-    """Not so simple class for running the same tests on multiple callables.
-
-    Provides a set of test methods that rely on instance attributes to do the
-    testing, meaning setting a few attributes creates a complete test.
-
-    Arguments:
-    input --    data to pass to the function
-    expected -- the result that is expected
-    name --     the name of the function being tested
-    function -- the function to test
-    explode --  if the function takes multiple arguments they must be passed as
-                container, if explode is set to True then the container will be
-                exploded when passed in
-
-    """
-    @description("grouptools.{name}: works")
-    def test_functionality(self):
-        """Test that the functionality of the function."""
-        if not self.explode:
-            nt.assert_equal(self.function(self.input), self.expected)
-        else:
-            nt.assert_equal(self.function(*self.input), self.expected)
-
-    @description("grouptools.{name}: doesn't accept a leading /")
-    @nt.raises(AssertionError)
-    def test_assertion_slash(self):
-        """Test that a leading / is an error."""
-        if isinstance(self.input, (str, unicode)):
-            self.function('/' + self.input)
-        elif not self.explode:
-            self.function(['/' + i for i in self.input])
-        else:
-            self.function(*['/' + i for i in self.input])
-
-    @description("grouptools.{name}: doesn't accept \\ in groups")
-    @nt.raises(AssertionError)
-    def test_assertion_backslash(self):
-        """Test that \\ in a group is an error."""
-        if isinstance(self.input, (str, unicode)):
-            self.function(self.input.replace('/', '\\'))
-        elif not self.explode:
-            self.function(i.replace('/', '\\') for i in self.input)
-        else:
-            self.function(*[i.replace('/', '\\') for i in self.input])
-
-
 @utils.nose_generator
 def generate_tests():
     """Generate tests for the groups tools module.
@@ -130,7 +35,7 @@ def generate_tests():
     This cannot test all corners of the more complicated members.
 
     """
-    tests = [
+    data = [
         ('testname', grouptools.testname, grouptools.join('g1', 'g2', 't1'),
          't1'),
         ('groupname', grouptools.groupname, grouptools.join('g1', 'g2', 't1'),
@@ -140,17 +45,21 @@ def generate_tests():
         ('commonprefix', grouptools.commonprefix,
          [grouptools.join('g1', 'g2', '1'), grouptools.join('g1', 'g2', '2')],
          grouptools.join('g1', 'g2', '')),
-        ('join', grouptools.join, [grouptools.join('g1', 'g2'), 't1'],
-         grouptools.join('g1', 'g2', 't1'), True),
         ('split', grouptools.split, grouptools.join('g1', 'g2', 't1'),
          ['g1', 'g2', 't1']),
     ]
 
-    for args in tests:
-        test_class = _GroupToolsTest(*args)
-        # In python3 we could use 'yield from'
-        for wrapper, test in test_class:
-            yield wrapper, test
+    test = lambda f, i, e: nt.assert_equal(f(i), e)
+
+    for name, func, args, expect in data:
+        test.description = 'grouptools.{}: works'.format(name)
+        yield test, func, args, expect
+
+
+def test_grouptools_join():
+    """grouptools.join: works correctly."""
+    # XXX: this hardcoded / needs to be fixed
+    nt.assert_equal(grouptools.join('g1', 'g2'), 'g1/g2')
 
 
 def test_split_input_empty():
-- 
2.3.1



More information about the Piglit mailing list