Commits

Sergey Schetinin committed ab8f90d

convert multidict doctest into nose-based unittest

  • Participants
  • Parent commits 32a4120

Comments (0)

Files changed (3)

 from mext.test_suite import TestSuite
 suite = TestSuite('tests', coverage=True, pkg='webob')
 
-doctests = ['test_dec', 'test_request', 'test_response', 'multidict']
+doctests = ['test_dec', 'test_request', 'test_response']
 doctests += map('../docs/'.__add__, ['do-it-yourself', 'file-example', 'index', 'reference'])
 map(suite.add_doctest, doctests)
 map(suite.add_nosetest, ['test_request', 'test_response', 'test_multidict', 'test_misc'])

tests/multidict.txt

-
-    >>> from webob.multidict import *
-    >>> d = MultiDict(a=1, b=2)
-    >>> d['a']
-    1
-    >>> d.getall('c')
-    []
-    >>> d.add('a', 2)
-    >>> d['a']
-    2
-    >>> d.getall('a')
-    [1, 2]
-    >>> d['b'] = 4
-    >>> d.getall('b')
-    [4]
-    >>> d.keys()
-    ['a', 'a', 'b']
-    >>> d.items()
-    [('a', 1), ('a', 2), ('b', 4)]
-    >>> d.mixed() == {'a': [1, 2], 'b': 4}
-    True
-    >>> MultiDict([('a', 'b')], c=2)
-    MultiDict([('a', 'b'), ('c', 2)])
-    >>> MultiDict(1,2,3)
-    Traceback (most recent call last):
-      File "<stdin>", line 1, in <module>
-      File "webob/multidict.py", line 29, in __init__
-        "MultiDict can only be called with one positional argument")
-    TypeError: MultiDict can only be called with one positional argument
-    >>> d.getone('a')
-    Traceback (most recent call last):
-      File "<stdin>", line 1, in <module>
-      File "webob/multidict.py", line 113, in getone
-        raise KeyError('Multiple values match %r: %r' % (key, v))
-    KeyError: "Multiple values match 'a': [1, 2]"
-    >>> d.getone('b')
-    4
-    >>> d.getone('g')
-    Traceback (most recent call last):
-      File "<stdin>", line 1, in <module>
-      File "webob/multidict.py", line 111, in getone
-        raise KeyError('Key not found: %r' % key)
-    KeyError: "Key not found: 'g'"
-    >>> MultiDict.view_list(None)
-    Traceback (most recent call last):
-      File "<stdin>", line 1, in <module>
-      File "webob/multidict.py", line 51, in view_list
-        % (cls.__name__, lst))
-    TypeError: MultiDict.view_list(obj) takes only actual list objects, not None
-    >>> d.dict_of_lists()
-    {'a': [1, 2], 'b': [4]}
-    >>> 'b' in d
-    True
-    >>> 'e' in d
-    False
-    >>> d.clear()
-    >>> 'b' in d
-    False
-    >>> d['a'] = 4
-    >>> d.add('a', 5)
-    >>> e = d.copy()
-    >>> 'a' in e
-    True
-    >>> e.clear()
-    >>> e['f'] = 42
-    >>> d.update(e)
-    >>> d
-    MultiDict([('a', 4), ('a', 5), ('f', 42)])
-    >>> f = d.pop('a')
-    >>> f
-    4
-    >>> d['a']
-    5
-    >>> d.pop('g', 42)
-    42
-    >>> d.pop('n')
-    Traceback (most recent call last):
-      File "<stdin>", line 1, in <module>
-      File "webob/multidict.py", line 195, in pop
-        raise KeyError(key)
-    KeyError: 'n'
-    >>> d.pop(4, 2, 3)
-    Traceback (most recent call last):
-      File "<stdin>", line 1, in <module>
-      File "webob/multidict.py", line 186, in pop
-        + repr(1 + len(args))
-    TypeError: pop expected at most 2 arguments, got 3
-    >>> d.setdefault('g', []).append(4)
-    >>> d
-    MultiDict([('a', 5), ('f', 42), ('g', [4])])
-    
-    >>> import cgi
-    >>> fs = cgi.FieldStorage()
-    >>> fs.filename = '\xc3\xb8'
-    >>> plain = MultiDict(key='\xc3\xb8', fs=fs)
-    >>> ua = UnicodeMultiDict(multi=plain, encoding='utf-8')
-    >>> ua.getall('key')
-    [u'\xf8']
-    >>> ua.getall('fs')
-    [FieldStorage(None, u'\xf8', [])]
-    >>> ub = UnicodeMultiDict(multi=ua, encoding='utf-8')
-    >>> ub.getall('key')
-    [u'\xf8']
-    >>> ub.getall('fs')
-    [FieldStorage(None, u'\xf8', [])]

tests/test_misc.py

+import cgi
 from webob import html_escape
-from nose.tools import eq_
+from webob.multidict import *
+from nose.tools import eq_ as eq, assert_raises
 
 
 def test_html_escape():
         (t_esc_Unicode(), '&#233;'),
         (t_esc_UnsafeAttrs(), '&lt;UnsafeAttrs&gt;'),
     ]:
-        eq_(html_escape(v), s)
+        eq(html_escape(v), s)
 
 
 class t_esc_HTML(object):
         return '<UnsafeAttrs>'
 
 
+
+
+def test_multidict():
+    d = MultiDict(a=1, b=2)
+    eq(d['a'], 1)
+    eq(d.getall('c'), [])
+
+    d.add('a', 2)
+    eq(d['a'], 2)
+    eq(d.getall('a'), [1, 2])
+
+    d['b'] = 4
+    eq(d.getall('b'), [4])
+    eq(d.keys(), ['a', 'a', 'b'])
+    eq(d.items(), [('a', 1), ('a', 2), ('b', 4)])
+    eq(d.mixed(), {'a': [1, 2], 'b': 4})
+
+    # test getone
+
+    # KeyError: "Multiple values match 'a': [1, 2]"
+    assert_raises(KeyError, d.getone, 'a')
+    eq(d.getone('b'), 4)
+    # KeyError: "Key not found: 'g'"
+    assert_raises(KeyError, d.getone, 'g')
+
+    eq(d.dict_of_lists(), {'a': [1, 2], 'b': [4]})
+    assert 'b' in d
+    assert 'e' not in d
+    d.clear()
+    assert 'b' not in d
+    d['a'] = 4
+    d.add('a', 5)
+    e = d.copy()
+    assert 'a' in e
+    e.clear()
+    e['f'] = 42
+    d.update(e)
+    eq(d, MultiDict([('a', 4), ('a', 5), ('f', 42)]))
+    f = d.pop('a')
+    eq(f, 4)
+    eq(d['a'], 5)
+
+
+    eq(d.pop('g', 42), 42)
+    assert_raises(KeyError, d.pop, 'n')
+    # TypeError: pop expected at most 2 arguments, got 3
+    assert_raises(TypeError, d.pop, 4, 2, 3)
+    d.setdefault('g', []).append(4)
+    eq(d, MultiDict([('a', 5), ('f', 42), ('g', [4])]))
+
+
+
+def test_multidict_init():
+    d = MultiDict([('a', 'b')], c=2)
+    eq(repr(d), "MultiDict([('a', 'b'), ('c', 2)])")
+    eq(d, MultiDict([('a', 'b')], c=2))
+
+    # TypeError: MultiDict can only be called with one positional argument
+    assert_raises(TypeError, MultiDict, 1, 2, 3)
+
+    # TypeError: MultiDict.view_list(obj) takes only actual list objects, not None
+    assert_raises(TypeError, MultiDict.view_list, None)
+
+
+
+def test_multidict_cgi():
+    fs = cgi.FieldStorage()
+    fs.filename = '\xc3\xb8'
+    plain = MultiDict(key='\xc3\xb8', fs=fs)
+    ua = UnicodeMultiDict(multi=plain, encoding='utf-8')
+    eq(ua.getall('key'), [u'\xf8'])
+    eq(ua.getall('fs'), [cgi.FieldStorage(None, u'\xf8', [])])
+    ub = UnicodeMultiDict(multi=ua, encoding='utf-8')
+    eq(ub.getall('key'), [u'\xf8'])
+    eq(ub.getall('fs'), [cgi.FieldStorage(None, u'\xf8', [])])