parts/django/tests/regressiontests/test_utils/tests.py
changeset 307 c6bca38c1cbf
equal deleted inserted replaced
306:5ff1fc726848 307:c6bca38c1cbf
       
     1 r"""
       
     2 # Some checks of the doctest output normalizer.
       
     3 # Standard doctests do fairly 
       
     4 >>> from django.utils import simplejson
       
     5 >>> from django.utils.xmlutils import SimplerXMLGenerator
       
     6 >>> from StringIO import StringIO
       
     7 
       
     8 >>> def produce_long():
       
     9 ...     return 42L
       
    10 
       
    11 >>> def produce_int():
       
    12 ...     return 42
       
    13 
       
    14 >>> def produce_json():
       
    15 ...     return simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2), 'whiz': 42}])
       
    16 
       
    17 >>> def produce_xml():
       
    18 ...     stream = StringIO()
       
    19 ...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
       
    20 ...     xml.startDocument()
       
    21 ...     xml.startElement("foo", {"aaa" : "1.0", "bbb": "2.0"})
       
    22 ...     xml.startElement("bar", {"ccc" : "3.0"})
       
    23 ...     xml.characters("Hello")
       
    24 ...     xml.endElement("bar")
       
    25 ...     xml.startElement("whiz", {})
       
    26 ...     xml.characters("Goodbye")
       
    27 ...     xml.endElement("whiz")
       
    28 ...     xml.endElement("foo")
       
    29 ...     xml.endDocument()
       
    30 ...     return stream.getvalue()
       
    31 
       
    32 >>> def produce_xml_fragment():
       
    33 ...     stream = StringIO()
       
    34 ...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
       
    35 ...     xml.startElement("foo", {"aaa": "1.0", "bbb": "2.0"})
       
    36 ...     xml.characters("Hello")
       
    37 ...     xml.endElement("foo")
       
    38 ...     xml.startElement("bar", {"ccc": "3.0", "ddd": "4.0"})
       
    39 ...     xml.endElement("bar")
       
    40 ...     return stream.getvalue()
       
    41 
       
    42 # Long values are normalized and are comparable to normal integers ...
       
    43 >>> produce_long()
       
    44 42
       
    45 
       
    46 # ... and vice versa
       
    47 >>> produce_int()
       
    48 42L
       
    49 
       
    50 # JSON output is normalized for field order, so it doesn't matter
       
    51 # which order json dictionary attributes are listed in output
       
    52 >>> produce_json()
       
    53 '["foo", {"bar": ["baz", null, 1.0, 2], "whiz": 42}]'
       
    54 
       
    55 >>> produce_json()
       
    56 '["foo", {"whiz": 42, "bar": ["baz", null, 1.0, 2]}]'
       
    57 
       
    58 # XML output is normalized for attribute order, so it doesn't matter 
       
    59 # which order XML element attributes are listed in output
       
    60 >>> produce_xml()
       
    61 '<?xml version="1.0" encoding="UTF-8"?>\n<foo aaa="1.0" bbb="2.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
       
    62 
       
    63 >>> produce_xml()
       
    64 '<?xml version="1.0" encoding="UTF-8"?>\n<foo bbb="2.0" aaa="1.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
       
    65 
       
    66 >>> produce_xml_fragment()
       
    67 '<foo aaa="1.0" bbb="2.0">Hello</foo><bar ccc="3.0" ddd="4.0"></bar>'
       
    68 
       
    69 >>> produce_xml_fragment()
       
    70 '<foo bbb="2.0" aaa="1.0">Hello</foo><bar ddd="4.0" ccc="3.0"></bar>'
       
    71 
       
    72 """