# -*- coding: utf-8 -*-
"""
werkzeug.testsuite.debug
~~~~~~~~~~~~~~~~~~~~~~~~
Tests some debug utilities.
:copyright: (c) 2013 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import unittest
import sys
import re
from werkzeug.testsuite import WerkzeugTestCase
from werkzeug.debug.repr import debug_repr, DebugReprGenerator, \
dump, helper
from werkzeug.debug.console import HTMLStringO
from werkzeug._compat import PY2
class DebugReprTestCase(WerkzeugTestCase):
def test_basic_repr(self):
self.assert_equal(debug_repr([]), u'[]')
self.assert_equal(debug_repr([1, 2]),
u'[1, 2]')
self.assert_equal(debug_repr([1, 'test']),
u'[1, \'test\']')
self.assert_equal(debug_repr([None]),
u'[None]')
def test_sequence_repr(self):
self.assert_equal(debug_repr(list(range(20))), (
u'[0, 1, '
u'2, 3, '
u'4, 5, '
u'6, 7, '
u'8, '
u'9, 10, '
u'11, 12, '
u'13, 14, '
u'15, 16, '
u'17, 18, '
u'19]'
))
def test_mapping_repr(self):
self.assert_equal(debug_repr({}), u'{}')
self.assert_equal(debug_repr({'foo': 42}),
u'{\'foo\''
u': 42'
u'}')
self.assert_equal(debug_repr(dict(zip(range(10), [None] * 10))),
u'{0: None, 1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None, 9: None}')
self.assert_equal(
debug_repr((1, 'zwei', u'drei')),
u'(1, \''
u'zwei\', %s\'drei\')' % ('u' if PY2 else ''))
def test_custom_repr(self):
class Foo(object):
def __repr__(self):
return ''
self.assert_equal(debug_repr(Foo()),
'<Foo 42>')
def test_list_subclass_repr(self):
class MyList(list):
pass
self.assert_equal(
debug_repr(MyList([1, 2])),
u'werkzeug.testsuite.debug.MyList(['
u'1, 2])')
def test_regex_repr(self):
self.assert_equal(debug_repr(re.compile(r'foo\d')),
u're.compile(r\'foo\\d\')')
#XXX: no raw string here cause of a syntax bug in py3.3
self.assert_equal(debug_repr(re.compile(u'foo\\d')),
u're.compile(%sr\'foo\\d\')' %
('u' if PY2 else ''))
def test_set_repr(self):
self.assert_equal(debug_repr(frozenset('x')),
u'frozenset([\'x\'])')
self.assert_equal(debug_repr(set('x')),
u'set([\'x\'])')
def test_recursive_repr(self):
a = [1]
a.append(a)
self.assert_equal(debug_repr(a),
u'[1, [...]]')
def test_broken_repr(self):
class Foo(object):
def __repr__(self):
raise Exception('broken!')
self.assert_equal(
debug_repr(Foo()),
u'<broken repr (Exception: '
u'broken!)>')
class Foo(object):
x = 42
y = 23
def __init__(self):
self.z = 15
class DebugHelpersTestCase(WerkzeugTestCase):
def test_object_dumping(self):
drg = DebugReprGenerator()
out = drg.dump_object(Foo())
assert re.search('Details for werkzeug.testsuite.debug.Foo object at', out)
assert re.search('x.*42(?s)', out)
assert re.search(' | y.*23(?s)', out)
assert re.search(' | z.*15(?s)', out)
out = drg.dump_object({'x': 42, 'y': 23})
assert re.search('Contents of', out)
assert re.search(' | x.*42(?s)', out)
assert re.search(' | y.*23(?s)', out)
out = drg.dump_object({'x': 42, 'y': 23, 23: 11})
assert not re.search('Contents of', out)
out = drg.dump_locals({'x': 42, 'y': 23})
assert re.search('Local variables in frame', out)
assert re.search(' | x.*42(?s)', out)
assert re.search(' | y.*23(?s)', out)
def test_debug_dump(self):
old = sys.stdout
sys.stdout = HTMLStringO()
try:
dump([1, 2, 3])
x = sys.stdout.reset()
dump()
y = sys.stdout.reset()
finally:
sys.stdout = old
self.assert_in('Details for list object at', x)
self.assert_in('1', x)
self.assert_in('Local variables in frame', y)
self.assert_in(' | x', y)
self.assert_in(' | old', y)
def test_debug_help(self):
old = sys.stdout
sys.stdout = HTMLStringO()
try:
helper([1, 2, 3])
x = sys.stdout.reset()
finally:
sys.stdout = old
self.assert_in('Help on list object', x)
self.assert_in('__delitem__', x)
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(DebugReprTestCase))
suite.addTest(unittest.makeSuite(DebugHelpersTestCase))
return suite
|