| | 542 | |
| | 543 | |
| | 544 | |
| | 545 | class chr_Tests(unittest.SynchronousTestCase): |
| | 546 | """ |
| | 547 | Tests for L{compat.chr_}. |
| | 548 | |
| | 549 | L{compat.chr_} should emulate the Python 2.x built-in chr which had |
| | 550 | these key characteristics: |
| | 551 | -Accepted an integer in [0, 255]. |
| | 552 | -Returned a bytestring containing the character encoded to that |
| | 553 | integer under ASCII. |
| | 554 | -Raised a ValueError in response to any integer outside that range. |
| | 555 | -Raised a TypeError in response to any other input. |
| | 556 | """ |
| | 557 | # A sampling of ASCII characters, which chr_ should accept. |
| | 558 | _ASCIIEncodedValues = [0, 1, 103, 104, 126, 127, 128, 254, 255] |
| | 559 | _ASCIICharacters = [b"\x00", b"\x01", b"g", b"h", b"~", b"\x7f", b"\x80", b"\xfe", b"\xff"] |
| | 560 | # And here is a sampling of things that chr_ should NOT accept. |
| | 561 | _negativeIntegers = [-10, -1] |
| | 562 | _integersOver255 = [256, 257, 500, 1000, 10000, 60000, 65534, 65535] |
| | 563 | _integersOver65535 = [65536, 65537, 100000] |
| | 564 | _bytestringASCIICharacters = [b"a", b"\x15"] |
| | 565 | _UnicodeCharacters = [u"b", u"\xeff", u"\xfffe", u"\xffff"] |
| | 566 | _UnicodeCharactersOver65535 = [u"\x10000", u"\x10001"] |
| | 567 | _nonIntegersNotCharacters = [2.3, dict()] |
| | 568 | |
| | 569 | def test_chr_Accepts(self): |
| | 570 | """ |
| | 571 | L{compat.chr_} accepts integers in [0, 255] and returns the |
| | 572 | corresponding character in an ASCII-encoded bytestring. |
| | 573 | """ |
| | 574 | for (encodedValue, bytestringCharacter) in zip( |
| | 575 | self._ASCIIEncodedValues, self._ASCIICharacters): |
| | 576 | # Deliberately leak any exceptions; failing the test with ERROR. |
| | 577 | ret = chr_(encodedValue) |
| | 578 | self.assertIsInstance(ret, bytes) |
| | 579 | self.assertEqual(ret, bytestringCharacter) |
| | 580 | |
| | 581 | def _assert_chr_Rejects(self, invalidInput, exceptionToRejectWith): |
| | 582 | """Convenience function. map assertRaises across invalidInput.""" |
| | 583 | for x in invalidInput: |
| | 584 | self.assertRaises(exceptionToRejectWith, chr_, x) |
| | 585 | |
| | 586 | def test_chr_RejectsNegativeIntegers(self): |
| | 587 | """ |
| | 588 | L{compat.chr_} rejects negative integers with ValueError. |
| | 589 | """ |
| | 590 | self._assert_chr_Rejects(self._negativeIntegers, ValueError) |
| | 591 | |
| | 592 | def test_chr_RejectsIntegersOver255(self): |
| | 593 | """ |
| | 594 | L{compat.chr_} rejects integers over 255 with ValueError. |
| | 595 | """ |
| | 596 | self._assert_chr_Rejects(self._integersOver255, ValueError) |
| | 597 | |
| | 598 | def test_chr_RejectsIntegersOver65535(self): |
| | 599 | """ |
| | 600 | L{compat.chr_} rejects integers over 65535 with ValueError. |
| | 601 | """ |
| | 602 | self._assert_chr_Rejects(self._integersOver65535, ValueError) |
| | 603 | |
| | 604 | def test_chr_RejectsBytestringASCIICharacters(self): |
| | 605 | """ |
| | 606 | L{compat.chr_} rejects ASCII-encoded bytestrings with TypeError. |
| | 607 | """ |
| | 608 | self._assert_chr_Rejects(self._bytestringASCIICharacters, TypeError) |
| | 609 | |
| | 610 | def test_chr_RejectsUnicodeCharacters(self): |
| | 611 | """ |
| | 612 | L{compat.chr_} rejects Unicode strings with TypeError. |
| | 613 | """ |
| | 614 | self._assert_chr_Rejects(self._UnicodeCharacters, TypeError) |
| | 615 | |
| | 616 | def test_chr_RejectsUnicodeCharactersOver65535(self): |
| | 617 | """ |
| | 618 | L{compat.chr_} rejects Unicode strings with codepoints over 65535 with |
| | 619 | TypeError. |
| | 620 | """ |
| | 621 | self._assert_chr_Rejects(self._UnicodeCharactersOver65535, TypeError) |
| | 622 | |
| | 623 | def test_chr_RejectsNonIntegersNonCharacters(self): |
| | 624 | """ |
| | 625 | L{compat.chr_} rejects any other odd thing you give it, like a dict, |
| | 626 | with TypeError. |
| | 627 | """ |
| | 628 | self._assert_chr_Rejects(self._nonIntegersNotCharacters, TypeError) |