Diff of /branches/1.1/turbogears/tests/test_util.py [000000] .. [r4329] Maximize Restore

  Switch to side-by-side view

--- a
+++ b/branches/1.1/turbogears/tests/test_util.py
@@ -0,0 +1,78 @@
+
+import sys
+from StringIO import StringIO
+
+from turbogears import util
+
+
+def test_deprecated():
+
+    @util.deprecated('this is old')
+    def some_old_function(x, y):
+        """some old function"""
+        return x + y
+
+    assert "some old function" in some_old_function.__doc__
+    some_old_function.__name__ == 'some_old_function'
+
+    stderr, sys.stderr = sys.stderr, StringIO()
+    try:
+        assert some_old_function(1, 2) == 3
+    finally:
+        stderr, sys.stderr = sys.stderr, stderr
+    assert 'DeprecationWarning: this is old' in stderr.getvalue()
+    stderr.close()
+
+
+def test_call_on_stack():
+
+    def recurse(level):
+        if level:
+            return recurse(level-1)
+        return util.call_on_stack('recurse', dict())
+
+    assert not recurse(0)
+    assert recurse(1)
+
+    def recurse(level, arg=None):
+        if level:
+            return recurse(level-1, arg)
+        return util.call_on_stack('recurse', dict(arg='true'), 3)
+
+    assert not recurse(3, 'true')
+    assert recurse(4, 'true')
+    assert not recurse(4, 'false')
+
+
+def test_remove_keys():
+
+    assert util.remove_keys(dict(x=1, y=2, z=3), 'xz') == dict(y=2)
+
+
+def test_has_arg():
+
+    def fun(x, z=2):
+        pass
+
+    assert util.has_arg(fun, 'x')
+    assert not util.has_arg(fun, 'y')
+    assert util.has_arg(fun, 'z')
+
+
+def test_arg_index():
+
+    def fun(a, b, y=3, z=4):
+        pass
+
+    assert util.arg_index(fun, 'a') == 0
+    assert util.arg_index(fun, 'b') == 1
+    assert util.arg_index(fun, 'c') is None
+    assert util.arg_index(fun, 'y') == 2
+    assert util.arg_index(fun, 'z') == 3
+
+
+def test_fixentities():
+    assert util.fixentities('Chip & Chap') \
+        == 'Chip & Chap'
+    assert util.fixentities('<&quot;&lt;&copy;&gt;&quot;>') \
+        == '<&quot;&lt;&#169;&gt;&quot;>'