Commits

mitsuhiko  committed e3060c3

Added some tests for the local object.

  • Participants
  • Parent commits cc1b2bc

Comments (0)

Files changed (5)

File tests/test_debug.py

     :license: BSD license.
 """
 import re
-from werkzeug.debug.repr import debug_repr, DebugReprGenerator
+import sys
+from werkzeug.debug.repr import debug_repr, DebugReprGenerator, dump, helper
+from werkzeug.debug.console import HTMLStringO
 
 
 def test_debug_repr():
         u'<span class="module">test_debug.</span>MyList([' \
         u'<span class="number">1</span>, <span class="number">2</span>])'
 
+    assert debug_repr(re.compile(r'foo\d')) == \
+        u're.compile(<span class="string regex">r\'foo\\d\'</span>)'
+    assert debug_repr(re.compile(ur'foo\d')) == \
+        u're.compile(<span class="string regex">ur\'foo\\d\'</span>)'
+
+    assert debug_repr(frozenset('x')) == \
+        u'frozenset([<span class="string">\'x\'</span>])'
+    assert debug_repr(set('x')) == \
+        u'set([<span class="string">\'x\'</span>])'
+
+    a = [1]
+    a.append(a)
+    assert debug_repr(a) == u'[<span class="number">1</span>, [...]]'
+
+    class Foo(object):
+        def __repr__(self):
+            1/0
+
+    assert debug_repr(Foo()) == \
+        u'<span class="brokenrepr">&lt;broken repr (ZeroDivisionError: ' \
+        u'integer division or modulo by zero)&gt;</span>'
+
 
 def test_object_dumping():
     """Test debug object dumping to HTML"""
     assert re.search('Local variables in frame', out)
     assert re.search('<th>x</th>.*<span class="number">42</span>(?s)', out)
     assert re.search('<th>y</th>.*<span class="number">23</span>(?s)', out)
+
+
+def test_debug_dump():
+    """Test debug dump"""
+    old = sys.stdout
+    sys.stdout = HTMLStringO()
+    try:
+        dump([1, 2, 3])
+        x = sys.stdout.reset()
+        dump()
+        y = sys.stdout.reset()
+    finally:
+        sys.stdout = old
+
+    assert 'Details for list object at' in x
+    assert '<span class="number">1</span>' in x
+    assert 'Local variables in frame' in y
+    assert '<th>x</th>' in y
+    assert '<th>old</th>' in y
+
+
+def test_debug_help():
+    """Test debug help"""
+    old = sys.stdout
+    sys.stdout = HTMLStringO()
+    try:
+        helper([1, 2, 3])
+        x = sys.stdout.reset()
+    finally:
+        sys.stdout = old
+
+    assert 'Help on list object' in x
+    assert '__delitem__' in x

File tests/test_local.py

+# -*- coding: utf-8 -*-
+"""
+    werkzeug.local test
+    ~~~~~~~~~~~~~~~~~~~
+
+    :copyright: (c) 2010 by the Werkzeug Team, see AUTHORS for more details.
+    :license: BSD license.
+"""
+import time
+from threading import Thread
+
+from nose.tools import assert_raises
+
+from werkzeug import Local, LocalManager
+
+
+def test_basic_local():
+    """Basic local object support"""
+    l = Local()
+    l.foo = 0
+    values = []
+    def value_setter(idx):
+        time.sleep(0.01 * idx)
+        l.foo = idx
+        time.sleep(0.02)
+        values.append(l.foo)
+    threads = [Thread(target=value_setter, args=(x,))
+               for x in [1, 2, 3]]
+    for thread in threads:
+        thread.start()
+    time.sleep(0.2)
+    assert sorted(values) == [1, 2, 3]
+
+    def delfoo():
+        del l.foo
+    delfoo()
+    assert_raises(AttributeError, lambda: l.foo)
+    assert_raises(AttributeError, delfoo)

File werkzeug/debug/repr.py

 from traceback import format_exception_only
 try:
     from collections import deque
-except ImportError:
+except ImportError: # pragma: no cover
     deque = None
 from werkzeug.utils import escape
 from werkzeug.debug.utils import render_template
     """
 
     def __call__(self, topic=None):
-        sys.stdout._write(self.get_help(topic))
-
-    def get_help(self, topic):
         title = text = None
         if topic is not None:
             import pydoc
             if len(paragraphs) > 1:
                 title = paragraphs[0]
                 text = '\n\n'.join(paragraphs[1:])
-            else:
+            else: # pragma: no cover
                 title = 'Help'
                 text = paragraphs[0]
-        return render_template('help_command.html', title=title, text=text)
+        rv = render_template('help_command.html', title=title, text=text)
+        sys.stdout._write(rv)
 
 helper = _Helper()
 
     def fallback_repr(self):
         try:
             info = ''.join(format_exception_only(*sys.exc_info()[:2]))
-        except:
+        except: # pragma: no cover
             info = '?'
         return u'<span class="brokenrepr">&lt;broken repr (%s)&gt;' \
                u'</span>' % escape(info.decode('utf-8', 'ignore').strip())

File werkzeug/local.py

     from py.magic import greenlet
     get_current_greenlet = greenlet.getcurrent
     del greenlet
-except:
+except: # pragma: no cover
     # catch all, py.* fails with so many different errors.
     get_current_greenlet = int
 try:
     from thread import get_ident as get_current_thread, allocate_lock
-except ImportError:
+except ImportError: # pragma: no cover
     from dummy_thread import get_ident as get_current_thread, allocate_lock
 
 from werkzeug.wsgi import ClosingIterator
 # get the best ident function.  if greenlets are not installed we can
 # safely just use the builtin thread function and save a python methodcall
 # and the cost of calculating a hash.
-if get_current_greenlet is int:
+if get_current_greenlet is int: # pragma: no cover
     get_ident = get_current_thread
 else:
     get_ident = lambda: (get_current_thread(), get_current_greenlet())

File werkzeug/posixemulation.py

 
 
 can_rename_open_file = False
-if os.name == 'nt':
+if os.name == 'nt': # pragma: no cover
     _rename = lambda src, dst: False
     _rename_atomic = lambda src, dst: False