<s> import sys <EOL> from xmltodict import parse , unparse , OrderedDict <EOL> try : <EOL> import unittest2 as unittest <EOL> except ImportError : <EOL> import unittest <EOL> import re <EOL> import collections <EOL> from textwrap import dedent <EOL> IS_JYTHON = sys . platform . startswith ( '<STR_LIT>' ) <EOL> _HEADER_RE = re . compile ( r'<STR_LIT>' ) <EOL> def _strip ( fullxml ) : <EOL> return _HEADER_RE . sub ( '<STR_LIT>' , fullxml ) <EOL> class DictToXMLTestCase ( unittest . TestCase ) : <EOL> def test_root ( self ) : <EOL> obj = { '<STR_LIT:a>' : None } <EOL> self . assertEqual ( obj , parse ( unparse ( obj ) ) ) <EOL> self . assertEqual ( unparse ( obj ) , unparse ( parse ( unparse ( obj ) ) ) ) <EOL> def test_simple_cdata ( self ) : <EOL> obj = { '<STR_LIT:a>' : '<STR_LIT:b>' } <EOL> self . assertEqual ( obj , parse ( unparse ( obj ) ) ) <EOL> self . assertEqual ( unparse ( obj ) , unparse ( parse ( unparse ( obj ) ) ) ) <EOL> def test_cdata ( self ) : <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT>' : '<STR_LIT:y>' } } <EOL> self . assertEqual ( obj , parse ( unparse ( obj ) , force_cdata = True ) ) <EOL> self . assertEqual ( unparse ( obj ) , unparse ( parse ( unparse ( obj ) ) ) ) <EOL> def test_attrib ( self ) : <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT>' : '<STR_LIT:x>' } } <EOL> self . assertEqual ( obj , parse ( unparse ( obj ) ) ) <EOL> self . assertEqual ( unparse ( obj ) , unparse ( parse ( unparse ( obj ) ) ) ) <EOL> def test_attrib_and_cdata ( self ) : <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT>' : '<STR_LIT:x>' , '<STR_LIT>' : '<STR_LIT:y>' } } <EOL> self . assertEqual ( obj , parse ( unparse ( obj ) ) ) <EOL> self . assertEqual ( unparse ( obj ) , unparse ( parse ( unparse ( obj ) ) ) ) <EOL> def test_list ( self ) : <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT:b>' : [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] } } <EOL> self . assertEqual ( obj , parse ( unparse ( obj ) ) ) <EOL> self . assertEqual ( unparse ( obj ) , unparse ( parse ( unparse ( obj ) ) ) ) <EOL> def test_generator ( self ) : <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT:b>' : [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] } } <EOL> def lazy_obj ( ) : <EOL> return { '<STR_LIT:a>' : { '<STR_LIT:b>' : ( i for i in ( '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ) ) } } <EOL> self . assertEqual ( obj , parse ( unparse ( lazy_obj ( ) ) ) ) <EOL> self . assertEqual ( unparse ( lazy_obj ( ) ) , <EOL> unparse ( parse ( unparse ( lazy_obj ( ) ) ) ) ) <EOL> def test_no_root ( self ) : <EOL> self . assertRaises ( ValueError , unparse , { } ) <EOL> def test_multiple_roots ( self ) : <EOL> self . assertRaises ( ValueError , unparse , { '<STR_LIT:a>' : '<STR_LIT:1>' , '<STR_LIT:b>' : '<STR_LIT:2>' } ) <EOL> self . assertRaises ( ValueError , unparse , { '<STR_LIT:a>' : [ '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ] } ) <EOL> def test_no_root_nofulldoc ( self ) : <EOL> self . assertEqual ( unparse ( { } , full_document = False ) , '<STR_LIT>' ) <EOL> def test_multiple_roots_nofulldoc ( self ) : <EOL> obj = OrderedDict ( ( ( '<STR_LIT:a>' , <NUM_LIT:1> ) , ( '<STR_LIT:b>' , <NUM_LIT:2> ) ) ) <EOL> xml = unparse ( obj , full_document = False ) <EOL> self . assertEqual ( xml , '<STR_LIT>' ) <EOL> obj = { '<STR_LIT:a>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] } <EOL> xml = unparse ( obj , full_document = False ) <EOL> self . assertEqual ( xml , '<STR_LIT>' ) <EOL> def test_nested ( self ) : <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT:b>' : '<STR_LIT:1>' , '<STR_LIT:c>' : '<STR_LIT:2>' } } <EOL> self . assertEqual ( obj , parse ( unparse ( obj ) ) ) <EOL> self . assertEqual ( unparse ( obj ) , unparse ( parse ( unparse ( obj ) ) ) ) <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT:b>' : { '<STR_LIT:c>' : { '<STR_LIT>' : '<STR_LIT:x>' , '<STR_LIT>' : '<STR_LIT:y>' } } } } <EOL> self . assertEqual ( obj , parse ( unparse ( obj ) ) ) <EOL> self . assertEqual ( unparse ( obj ) , unparse ( parse ( unparse ( obj ) ) ) ) <EOL> def test_semistructured ( self ) : <EOL> xml = '<STR_LIT>' <EOL> self . assertEqual ( _strip ( unparse ( parse ( xml ) ) ) , <EOL> '<STR_LIT>' ) <EOL> def test_preprocessor ( self ) : <EOL> obj = { '<STR_LIT:a>' : OrderedDict ( ( ( '<STR_LIT>' , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) ) ) } <EOL> def p ( key , value ) : <EOL> try : <EOL> key , _ = key . split ( '<STR_LIT::>' ) <EOL> except ValueError : <EOL> pass <EOL> return key , value <EOL> self . assertEqual ( _strip ( unparse ( obj , preprocessor = p ) ) , <EOL> '<STR_LIT>' ) <EOL> def test_preprocessor_skipkey ( self ) : <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT:b>' : <NUM_LIT:1> , '<STR_LIT:c>' : <NUM_LIT:2> } } <EOL> def p ( key , value ) : <EOL> if key == '<STR_LIT:b>' : <EOL> return None <EOL> return key , value <EOL> self . assertEqual ( _strip ( unparse ( obj , preprocessor = p ) ) , <EOL> '<STR_LIT>' ) <EOL> if not IS_JYTHON : <EOL> def test_attr_order_roundtrip ( self ) : <EOL> xml = '<STR_LIT>' <EOL> self . assertEqual ( xml , _strip ( unparse ( parse ( xml ) ) ) ) <EOL> def test_pretty_print ( self ) : <EOL> obj = { '<STR_LIT:a>' : OrderedDict ( ( <EOL> ( '<STR_LIT:b>' , [ { '<STR_LIT:c>' : [ <NUM_LIT:1> , <NUM_LIT:2> ] } , <NUM_LIT:3> ] ) , <EOL> ( '<STR_LIT:x>' , '<STR_LIT:y>' ) , <EOL> ) ) } <EOL> newl = '<STR_LIT:\n>' <EOL> indent = '<STR_LIT>' <EOL> xml = dedent ( '''<STR_LIT>''' ) <EOL> self . assertEqual ( xml , unparse ( obj , pretty = True , <EOL> newl = newl , indent = indent ) ) <EOL> def test_encoding ( self ) : <EOL> try : <EOL> value = unichr ( <NUM_LIT> ) <EOL> except NameError : <EOL> value = chr ( <NUM_LIT> ) <EOL> obj = { '<STR_LIT:a>' : value } <EOL> utf8doc = unparse ( obj , encoding = '<STR_LIT:utf-8>' ) <EOL> latin1doc = unparse ( obj , encoding = '<STR_LIT>' ) <EOL> self . assertEqual ( parse ( utf8doc ) , parse ( latin1doc ) ) <EOL> self . assertEqual ( parse ( utf8doc ) , obj ) <EOL> def test_fulldoc ( self ) : <EOL> xml_declaration_re = re . compile ( <EOL> '<STR_LIT>' + re . escape ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( xml_declaration_re . match ( unparse ( { '<STR_LIT:a>' : <NUM_LIT:1> } ) ) ) <EOL> self . assertFalse ( <EOL> xml_declaration_re . match ( unparse ( { '<STR_LIT:a>' : <NUM_LIT:1> } , full_document = False ) ) ) <EOL> def test_non_string_value ( self ) : <EOL> obj = { '<STR_LIT:a>' : <NUM_LIT:1> } <EOL> self . assertEqual ( '<STR_LIT>' , _strip ( unparse ( obj ) ) ) <EOL> def test_non_string_attr ( self ) : <EOL> obj = { '<STR_LIT:a>' : { '<STR_LIT>' : <NUM_LIT:1> } } <EOL> self . assertEqual ( '<STR_LIT>' , _strip ( unparse ( obj ) ) ) </s>
