Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea4250d

Browse files
committed
Add comments and remove duplicate tests.
1 parent 2c71ad3 commit ea4250d

1 file changed

Lines changed: 34 additions & 26 deletions

File tree

Lib/test/test_codeccallbacks.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def check_exceptionobjectargs(self, exctype, args, msg):
222222
# Test UnicodeError subclasses: construction, attribute assignment and __str__ conversion
223223
# check with one missing argument
224224
self.assertRaises(TypeError, exctype, *args[:-1])
225-
# check with one missing argument
225+
# check with one argument too much
226226
self.assertRaises(TypeError, exctype, *(args + ["too much"]))
227227
# check with one argument of the wrong type
228228
wrongargs = [ "spam", u"eggs", 42, 1.0, None ]
@@ -238,6 +238,8 @@ def check_exceptionobjectargs(self, exctype, args, msg):
238238
else:
239239
callargs.append(args[i])
240240
self.assertRaises(TypeError, exctype, *callargs)
241+
242+
# check with the correct number and type of arguments
241243
exc = exctype(*args)
242244
self.assertEquals(str(exc), msg)
243245

@@ -283,34 +285,40 @@ def test_unicodetranslateerror(self):
283285
)
284286

285287
def test_badandgoodstrictexceptions(self):
288+
# "strict" complains about a non-exception passed in
286289
self.assertRaises(
287290
TypeError,
288291
codecs.strict_errors,
289292
42
290293
)
294+
# "strict" complains about the wrong exception type
291295
self.assertRaises(
292296
Exception,
293297
codecs.strict_errors,
294298
Exception("ouch")
295299
)
296300

301+
# If the correct exception is passed in, "strict" raises it
297302
self.assertRaises(
298303
UnicodeEncodeError,
299304
codecs.strict_errors,
300305
UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")
301306
)
302307

303308
def test_badandgoodignoreexceptions(self):
309+
# "ignore" complains about a non-exception passed in
304310
self.assertRaises(
305311
TypeError,
306312
codecs.ignore_errors,
307313
42
308314
)
315+
# "ignore" complains about the wrong exception type
309316
self.assertRaises(
310317
TypeError,
311318
codecs.ignore_errors,
312319
UnicodeError("ouch")
313320
)
321+
# If the correct exception is passed in, "ignore" returns an empty replacement
314322
self.assertEquals(
315323
codecs.ignore_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
316324
(u"", 1)
@@ -325,16 +333,19 @@ def test_badandgoodignoreexceptions(self):
325333
)
326334

327335
def test_badandgoodreplaceexceptions(self):
336+
# "replace" complains about a non-exception passed in
328337
self.assertRaises(
329338
TypeError,
330339
codecs.replace_errors,
331340
42
332341
)
342+
# "replace" complains about the wrong exception type
333343
self.assertRaises(
334344
TypeError,
335345
codecs.replace_errors,
336346
UnicodeError("ouch")
337347
)
348+
# With the correct exception, "ignore" returns an empty replacement
338349
self.assertEquals(
339350
codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
340351
(u"?", 1)
@@ -349,25 +360,19 @@ def test_badandgoodreplaceexceptions(self):
349360
)
350361

351362
def test_badandgoodxmlcharrefreplaceexceptions(self):
363+
# "xmlcharrefreplace" complains about a non-exception passed in
352364
self.assertRaises(
353365
TypeError,
354366
codecs.xmlcharrefreplace_errors,
355367
42
356368
)
369+
# "xmlcharrefreplace" complains about the wrong exception types
357370
self.assertRaises(
358371
TypeError,
359372
codecs.xmlcharrefreplace_errors,
360373
UnicodeError("ouch")
361374
)
362-
self.assertEquals(
363-
codecs.xmlcharrefreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
364-
(u"&#%d;" % 0x3042, 1)
365-
)
366-
self.assertRaises(
367-
TypeError,
368-
codecs.xmlcharrefreplace_errors,
369-
UnicodeError("ouch")
370-
)
375+
# "xmlcharrefreplace" can only be used for encoding
371376
self.assertRaises(
372377
TypeError,
373378
codecs.xmlcharrefreplace_errors,
@@ -378,18 +383,37 @@ def test_badandgoodxmlcharrefreplaceexceptions(self):
378383
codecs.xmlcharrefreplace_errors,
379384
UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
380385
)
386+
# Use the correct exception
387+
self.assertEquals(
388+
codecs.xmlcharrefreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
389+
(u"&#%d;" % 0x3042, 1)
390+
)
381391

382392
def test_badandgoodbackslashreplaceexceptions(self):
393+
# "backslashreplace" complains about a non-exception passed in
383394
self.assertRaises(
384395
TypeError,
385396
codecs.backslashreplace_errors,
386397
42
387398
)
399+
# "backslashreplace" complains about the wrong exception types
388400
self.assertRaises(
389401
TypeError,
390402
codecs.backslashreplace_errors,
391403
UnicodeError("ouch")
392404
)
405+
# "backslashreplace" can only be used for encoding
406+
self.assertRaises(
407+
TypeError,
408+
codecs.backslashreplace_errors,
409+
UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
410+
)
411+
self.assertRaises(
412+
TypeError,
413+
codecs.backslashreplace_errors,
414+
UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
415+
)
416+
# Use the correct exception
393417
self.assertEquals(
394418
codecs.backslashreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
395419
(u"\\u3042", 1)
@@ -420,22 +444,6 @@ def test_badandgoodbackslashreplaceexceptions(self):
420444
(u"\\U0010ffff", 1)
421445
)
422446

423-
self.assertRaises(
424-
TypeError,
425-
codecs.backslashreplace_errors,
426-
UnicodeError("ouch")
427-
)
428-
self.assertRaises(
429-
TypeError,
430-
codecs.backslashreplace_errors,
431-
UnicodeDecodeError("ascii", "\xff", 0, 1, "ouch")
432-
)
433-
self.assertRaises(
434-
TypeError,
435-
codecs.backslashreplace_errors,
436-
UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
437-
)
438-
439447
def test_badhandlerresults(self):
440448
results = ( 42, u"foo", (1,2,3), (u"foo", 1, 3), (u"foo", None), (u"foo",), ("foo", 1, 3), ("foo", None), ("foo",) )
441449
encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15")

0 commit comments

Comments
 (0)