@@ -303,9 +303,97 @@ def testMatchQnames(self):
303303 'foo' , '' , 'bar' ) == False )
304304
305305
306+ class Chars (atom .core .XmlElement ):
307+ _qname = u'{http://example.com/}chars'
308+ y = 'y'
309+ alpha = 'a'
310+
311+
312+ class Strs (atom .core .XmlElement ):
313+ _qname = '{http://example.com/}strs'
314+ chars = [Chars ]
315+ delta = u'd'
316+
317+
318+ def parse (string ):
319+ return atom .core .xml_element_from_string (string , atom .core .XmlElement )
320+
321+
322+ def create (tag , string ):
323+ element = atom .core .XmlElement (text = string )
324+ element ._qname = tag
325+ return element
326+
327+
328+ class CharacterEncodingTest (unittest .TestCase ):
329+
330+ def testUnicodeInputString (self ):
331+ # Test parsing the inner text.
332+ self .assertEqual (parse (u'<x>δ</x>' ).text , u'\u03b4 ' )
333+ self .assertEqual (parse (u'<x>\u03b4 </x>' ).text , u'\u03b4 ' )
334+
335+ # Test output valid XML.
336+ self .assertEqual (parse (u'<x>δ</x>' ).to_string (), '<x>δ</x>' )
337+ self .assertEqual (parse (u'<x>\u03b4 </x>' ).to_string (), '<x>δ</x>' )
338+
339+ # Test setting the inner text and output valid XML.
340+ e = create (u'x' , u'\u03b4 ' )
341+ self .assertEqual (e .to_string (), '<x>δ</x>' )
342+ self .assertEqual (e .text , u'\u03b4 ' )
343+ self .assertTrue (isinstance (e .text , unicode ))
344+ self .assertEqual (create (u'x' , '\xce \xb4 ' .decode ('utf-8' )).to_string (),
345+ '<x>δ</x>' )
346+
347+
348+ def testUtf8InputString (self ):
349+ # Test parsing inner text.
350+ self .assertEqual (parse ('<x>δ</x>' ).text , u'\u03b4 ' )
351+ self .assertEqual (parse (u'<x>\u03b4 </x>' .encode ('utf-8' )).text , u'\u03b4 ' )
352+ self .assertEqual (parse ('<x>\xce \xb4 </x>' ).text , u'\u03b4 ' )
353+
354+ # Test output valid XML.
355+ self .assertEqual (parse ('<x>δ</x>' ).to_string (), '<x>δ</x>' )
356+ self .assertEqual (parse (u'<x>\u03b4 </x>' .encode ('utf-8' )).to_string (),
357+ '<x>δ</x>' )
358+ self .assertEqual (parse ('<x>\xce \xb4 </x>' ).to_string (), '<x>δ</x>' )
359+
360+ # Test setting the inner text and output valid XML.
361+ e = create ('x' , '\xce \xb4 ' )
362+ self .assertEqual (e .to_string (), '<x>δ</x>' )
363+ # Don't change the encoding until the we convert to an XML string.
364+ self .assertEqual (e .text , '\xce \xb4 ' )
365+ self .assertTrue (isinstance (e .text , str ))
366+ self .assertTrue (isinstance (e .to_string (), str ))
367+ self .assertEqual (create ('x' , u'\u03b4 ' .encode ('utf-8' )).to_string (),
368+ '<x>δ</x>' )
369+
370+
371+
372+ def testOtherEncodingOnInputString (self ):
373+ # Test parsing inner text.
374+ self .assertEqual (parse (u'<x>\u03b4 </x>' .encode ('utf-16' )).text , u'\u03b4 ' )
375+
376+ # Test output valid XML.
377+ self .assertEqual (parse (u'<x>\u03b4 </x>' .encode ('utf-16' )).to_string (),
378+ '<x>δ</x>' )
379+
380+ # Test setting the inner text and output valid XML.
381+ e = create ('x' , u'\u03b4 ' .encode ('utf-16' ))
382+ self .assertEqual (e .to_string (encoding = 'utf-16' ), '<x>δ</x>' )
383+ # Don't change the encoding until the we convert to an XML string.
384+ self .assertEqual (e .text , '\xff \xfe \xb4 \x03 ' )
385+ self .assertTrue (isinstance (e .text , str ))
386+ self .assertTrue (isinstance (e .to_string (encoding = 'utf-16' ), str ))
387+ self .assertEqual (
388+ create ('x' , '\xff \xfe \xb4 \x03 ' ).to_string (encoding = 'utf-16' ),
389+ '<x>δ</x>' )
390+
391+
392+
306393def suite ():
307394 return unittest .TestSuite ((unittest .makeSuite (XmlElementTest , 'test' ),
308- unittest .makeSuite (UtilityFunctionTest , 'test' )))
395+ unittest .makeSuite (UtilityFunctionTest , 'test' ),
396+ unittest .makeSuite (CharacterEncodingTest , 'test' ),))
309397
310398
311399if __name__ == '__main__' :
0 commit comments