Commits

Zero Piraeus  committed 529f93d

Add some tests.

  • Participants
  • Parent commits e028558

Comments (0)

Files changed (5)

File .session.vim

 below split tests/__init__.py
 below split tests/test.py
 below split tests/decorators/__init__.py
+below split tests/decorators/examples.py
 below split tests/decorators/test.py
 below split tests/decorators/test_patch.py
 below split tests/util/__init__.py

File tests/__init__.py

 """Tests for the `ook` package."""
+
+from ook.util import Version
+
+VERSION_ARGS = ("inf", float("inf"), 3, 2.7, "2.6.8", Version(), (3, 1, 5))

File tests/decorators/examples.py

+"""Examples for `tests.decorators`."""
+
+
+def example():
+    """Return a string."""
+    return "undecorated"

File tests/decorators/test_patch.py

 """Tests for the `ook.decorators.patch` function."""
 
+try:
+    reload
+except NameError:
+    from imp import reload  # pylint: disable=E0611,W0622
+
+import itertools
+
 from types import FunctionType
 
-from ook.decorators import patch
+from ook import patch
+from ook.decorators import PY_VERSION
+from ook.util import Version
+
+import tests.decorators.examples
+
+from tests import VERSION_ARGS
+
+
+@patch(itertools, 2.5)
+def combinations(iterable, r):  # pylint: disable=C0103
+    """Return successive r-length combinations of elements in the iterable."""
+    # From <http://docs.python.org/release/2.6/library/itertools.html>.
+    # Yes, I'm using `ook.patch()` to test itself. Meta, no?
+    pool = tuple(iterable)
+    n = len(pool)  # pylint: disable=C0103
+    if r > n:
+        return
+    indices = range(r)
+    yield tuple(pool[i] for i in indices)
+    while True:
+        for i in reversed(range(r)):
+            if indices[i] != i + n - r:
+                break
+        else:
+            return
+        indices[i] += 1  # pylint: disable=W0631
+        for j in range(i + 1, r):  # pylint: disable=W0631
+            indices[j] = indices[j - 1] + 1
+        yield tuple(pool[i] for i in indices)
 
 
 def test_exists():
-    """`patch` function exists"""
+    """`ook.patch` function exists"""
     assert isinstance(patch, FunctionType)
+
+
+def test_undecorated():
+    """`tests.decorators.examples.example()` returns 'undecorated'"""
+    reload(tests.decorators.examples)
+    assert tests.decorators.examples.example() == "undecorated"
+
+
+def test_no_args():
+    """`ook.patch` function replaces existing `example` fuinction"""
+    reload(tests.decorators.examples)
+
+    @patch(tests.decorators.examples)
+    def example():
+        """Do nothing."""
+        pass
+
+    assert tests.decorators.examples.example == example
+    assert tests.decorators.examples.example() is None
+
+
+def check_version_args(*args):
+    """`ook.patch` function respects version arguments"""
+    reload(tests.decorators.examples)
+
+    @patch(tests.decorators.examples, *args)
+    def example():
+        """Return a version argument."""
+        return args
+
+    if PY_VERSION in map(Version, args):
+        assert tests.decorators.examples.example == example
+        assert tests.decorators.examples.example() == args
+    else:
+        assert tests.decorators.examples.example != example
+        assert tests.decorators.examples.example() == "undecorated"
+
+
+def test_version_args():
+    """`ook.patch` function respects groups of version arguments"""
+    for nargs in range(1, len(VERSION_ARGS) + 1):
+        # pylint: disable=E1101
+        for args in itertools.combinations(VERSION_ARGS, nargs):
+            yield (check_version_args,) + tuple(args)

File tests/util/test_version.py

 
 from ook.util import Version
 
-ARGUMENTS = ("inf", float("inf"), 3, 2.7, "2.6.8", Version(), (3, 1, 5))
+from tests import VERSION_ARGS
 
 
 def test_exists():
 
 def test_instantiate():
     """`Version` class can be instantiated"""
-    for version in map(Version, ARGUMENTS):
+    for version in map(Version, VERSION_ARGS):
         yield check_instantiate, version
 
 
 
 def test_repr():
     """`Version` instances have sensible `repr` values"""
-    for version in map(Version, ARGUMENTS):
+    for version in map(Version, VERSION_ARGS):
         yield check_repr, version
 
 
 def test_mmm():
     """`Version` instances have sensible `major`|`minor`|`micro` attributes"""
     attrs = ("major", "minor", "micro")
-    for version in map(Version, ARGUMENTS):
+    for version in map(Version, VERSION_ARGS):
         for index, attr in enumerate(getattr(version, a) for a in attrs):
             yield check_mmm, version, index, attr
 
 
 def test_compare():
     """``Version` instances compare as expected"""
-    for ver1 in map(Version, ARGUMENTS):
-        for ver2 in map(Version, ARGUMENTS):
+    for ver1 in map(Version, VERSION_ARGS):
+        for ver2 in map(Version, VERSION_ARGS):
             for oper in (eq, ge, gt, le, lt, ne):
                 yield check_compare, oper, ver1, ver2
 
 
 def test_compare_unorderable():
     """Comparing `Version` instances against non-`Version`s fails"""
-    for nonversion in ARGUMENTS:
+    for nonversion in VERSION_ARGS:
         if isinstance(nonversion, Version):
             continue
-        for version in map(Version, ARGUMENTS):
+        for version in map(Version, VERSION_ARGS):
             for oper in (ge, gt, le, lt):
                 yield check_compare_unorderable, oper, version, nonversion