|
1 | | -assert b'foobar'.__eq__(2) == NotImplemented |
2 | | -assert b'foobar'.__ne__(2) == NotImplemented |
3 | | -assert b'foobar'.__gt__(2) == NotImplemented |
4 | | -assert b'foobar'.__ge__(2) == NotImplemented |
5 | | -assert b'foobar'.__lt__(2) == NotImplemented |
6 | | -assert b'foobar'.__le__(2) == NotImplemented |
| 1 | +from testutils import assertRaises |
| 2 | + |
| 3 | +# new |
| 4 | +assert bytes([1, 2, 3]) |
| 5 | +assert bytes((1, 2, 3)) |
| 6 | +assert bytes(range(4)) |
| 7 | +assert bytes(3) |
| 8 | +assert b"bla" |
| 9 | +assert bytes("bla", "utf8") |
| 10 | +with assertRaises(TypeError): |
| 11 | + bytes("bla") |
| 12 | + |
| 13 | + |
| 14 | +a = b"abcd" |
| 15 | +b = b"ab" |
| 16 | +c = b"abcd" |
| 17 | + |
| 18 | +# |
| 19 | +# repr |
| 20 | +assert repr(bytes([0, 1, 2])) == repr(b"\x00\x01\x02") |
| 21 | +assert repr( |
| 22 | + bytes([0, 1, 9, 10, 11, 13, 31, 32, 33, 89, 120, 255]) |
| 23 | + == "b'\\x00\\x01\\t\\n\\x0b\\r\\x1f !Yx\\xff'" |
| 24 | +) |
| 25 | +assert repr(b"abcd") == "b'abcd'" |
| 26 | + |
| 27 | +# len |
| 28 | +assert len(bytes("abcdé", "utf8")) == 6 |
| 29 | + |
| 30 | +# comp |
| 31 | +assert a == b"abcd" |
| 32 | +assert a > b |
| 33 | +assert a >= b |
| 34 | +assert b < a |
| 35 | +assert b <= a |
| 36 | + |
| 37 | +assert b"foobar".__eq__(2) == NotImplemented |
| 38 | +assert b"foobar".__ne__(2) == NotImplemented |
| 39 | +assert b"foobar".__gt__(2) == NotImplemented |
| 40 | +assert b"foobar".__ge__(2) == NotImplemented |
| 41 | +assert b"foobar".__lt__(2) == NotImplemented |
| 42 | +assert b"foobar".__le__(2) == NotImplemented |
| 43 | + |
| 44 | +# hash |
| 45 | +hash(a) == hash(b"abcd") |
| 46 | + |
| 47 | +# iter |
| 48 | +[i for i in b"abcd"] == ["a", "b", "c", "d"] |
| 49 | +assert list(bytes(3)) == [0, 0, 0] |
| 50 | + |
| 51 | +# add |
| 52 | +assert a + b == b"abcdab" |
| 53 | + |
| 54 | +# contains |
| 55 | +assert b"ab" in b"abcd" |
| 56 | +assert b"cd" in b"abcd" |
| 57 | +assert b"abcd" in b"abcd" |
| 58 | +assert b"a" in b"abcd" |
| 59 | +assert b"d" in b"abcd" |
| 60 | +assert b"dc" not in b"abcd" |
| 61 | +assert 97 in b"abcd" |
| 62 | +assert 150 not in b"abcd" |
| 63 | +with assertRaises(ValueError): |
| 64 | + 350 in b"abcd" |
| 65 | + |
| 66 | + |
| 67 | +# getitem |
| 68 | +d = b"abcdefghij" |
| 69 | + |
| 70 | +assert d[1] == 98 |
| 71 | +assert d[-1] == 106 |
| 72 | +assert d[2:6] == b"cdef" |
| 73 | +assert d[-6:] == b"efghij" |
| 74 | +assert d[1:8:2] == b"bdfh" |
| 75 | +assert d[8:1:-2] == b"igec" |
| 76 | + |
| 77 | + |
| 78 | +# is_xx methods |
| 79 | + |
| 80 | +assert bytes(b"1a23").isalnum() |
| 81 | +assert not bytes(b"1%a23").isalnum() |
| 82 | + |
| 83 | +assert bytes(b"abc").isalpha() |
| 84 | +assert not bytes(b"abc1").isalpha() |
| 85 | + |
| 86 | +# travis doesn't like this |
| 87 | +# assert bytes(b'xyz').isascii() |
| 88 | +# assert not bytes([128, 157, 32]).isascii() |
| 89 | + |
| 90 | +assert bytes(b"1234567890").isdigit() |
| 91 | +assert not bytes(b"12ab").isdigit() |
| 92 | + |
| 93 | +l = bytes(b"lower") |
| 94 | +b = bytes(b"UPPER") |
| 95 | + |
| 96 | +assert l.islower() |
| 97 | +assert not l.isupper() |
| 98 | +assert b.isupper() |
| 99 | +assert not bytes(b"Super Friends").islower() |
| 100 | + |
| 101 | +assert bytes(b" \n\t").isspace() |
| 102 | +assert not bytes(b"\td\n").isspace() |
| 103 | + |
| 104 | +assert b.isupper() |
| 105 | +assert not b.islower() |
| 106 | +assert l.islower() |
| 107 | +assert not bytes(b"tuPpEr").isupper() |
| 108 | + |
| 109 | +assert bytes(b"Is Title Case").istitle() |
| 110 | +assert not bytes(b"is Not title casE").istitle() |
| 111 | + |
| 112 | +# upper lower |
| 113 | +l = bytes(b"lower") |
| 114 | +b = bytes(b"UPPER") |
| 115 | +assert l.lower().islower() |
| 116 | +assert b.upper().isupper() |
| 117 | + |
| 118 | +# hex from hex |
| 119 | +assert bytes([0, 1, 9, 23, 90, 234]).hex() == "000109175aea" |
| 120 | + |
| 121 | +bytes.fromhex("62 6c7a 34350a ") == b"blz45\n" |
| 122 | +try: |
| 123 | + bytes.fromhex("62 a 21") |
| 124 | +except ValueError as e: |
| 125 | + str(e) == "non-hexadecimal number found in fromhex() arg at position 4" |
| 126 | +try: |
| 127 | + bytes.fromhex("6Z2") |
| 128 | +except ValueError as e: |
| 129 | + str(e) == "non-hexadecimal number found in fromhex() arg at position 1" |
0 commit comments