@@ -377,6 +377,32 @@ def test_fixup(s):
377377# Test builtin codecs
378378print 'Testing builtin codecs...' ,
379379
380+ # UTF-7 specific encoding tests:
381+ utfTests = [(u'A\u2262 \u0391 .' , 'A+ImIDkQ.' ), # RFC2152 example
382+ (u'Hi Mom -\u263a -!' , 'Hi Mom -+Jjo--!' ), # RFC2152 example
383+ (u'\u65E5 \u672C \u8A9E ' , '+ZeVnLIqe-' ), # RFC2152 example
384+ (u'Item 3 is \u00a3 1.' , 'Item 3 is +AKM-1.' ), # RFC2152 example
385+ (u'+' , '+-' ),
386+ (u'+-' , '+--' ),
387+ (u'+?' , '+-?' ),
388+ (u'\?' , '+AFw?' ),
389+ (u'+?' , '+-?' ),
390+ (ur'\\?' , '+AFwAXA?' ),
391+ (ur'\\\?' , '+AFwAXABc?' ),
392+ (ur'++--' , '+-+---' )]
393+
394+ for x ,y in utfTests :
395+ verify ( x .encode ('utf-7' ) == y )
396+
397+ try :
398+ unicode ('+3ADYAA-' , 'utf-7' ) # surrogates not supported
399+ except UnicodeError :
400+ pass
401+ else :
402+ raise TestFailed , "unicode('+3ADYAA-', 'utf-7') failed to raise an exception"
403+
404+ verify (unicode ('+3ADYAA-' , 'utf-7' , 'replace' ) == u'\ufffd ' )
405+
380406# UTF-8 specific encoding tests:
381407verify (u'\u20ac ' .encode ('utf-8' ) == \
382408 '' .join ((chr (0xe2 ), chr (0x82 ), chr (0xac ))) )
@@ -439,6 +465,7 @@ def __str__(self):
439465verify (unicode ('Andr\202 x' ,'ascii' ,'replace' ) == u'Andr\uFFFD x' )
440466
441467verify (u'hello' .encode ('ascii' ) == 'hello' )
468+ verify (u'hello' .encode ('utf-7' ) == 'hello' )
442469verify (u'hello' .encode ('utf-8' ) == 'hello' )
443470verify (u'hello' .encode ('utf8' ) == 'hello' )
444471verify (u'hello' .encode ('utf-16-le' ) == 'h\000 e\000 l\000 l\000 o\000 ' )
@@ -447,7 +474,7 @@ def __str__(self):
447474
448475# Roundtrip safety for BMP (just the first 1024 chars)
449476u = u'' .join (map (unichr , range (1024 )))
450- for encoding in ('utf-8' , 'utf-16' , 'utf-16-le' , 'utf-16-be' ,
477+ for encoding in ('utf-7' , 'utf- 8' , 'utf-16' , 'utf-16-le' , 'utf-16-be' ,
451478 'raw_unicode_escape' , 'unicode_escape' , 'unicode_internal' ):
452479 verify (unicode (u .encode (encoding ),encoding ) == u )
453480
0 commit comments