|
1 | 1 | # coding: utf-8 |
2 | 2 | import codecs |
3 | 3 |
|
4 | | -from unicodec.bom_encoding import find_bom_encoding |
| 4 | +from unicodec.bom_encoding import BOM_ENCODINGS, find_bom_encoding |
| 5 | +from unicodec.main import decode_content |
5 | 6 |
|
6 | 7 |
|
7 | | -def test_bom_preservation_while_decoding(): |
8 | | - # type: () -> None |
| 8 | +def test_decode_utf8_preserves_bom(): # type: () -> None |
9 | 9 | # fmt: off |
10 | 10 | msg = codecs.BOM_UTF8 + u"привет".encode("utf-8") |
11 | 11 | assert msg.decode("utf-8") == codecs.BOM_UTF8.decode("utf-8") + u"привет" |
12 | 12 | # fmt: on |
13 | 13 |
|
14 | 14 |
|
15 | | -def test_find_bom_encoding_empty_data(): |
16 | | - # type: () -> None |
17 | | - assert find_bom_encoding(b"") is None |
| 15 | +def test_find_bom_encoding_empty_data(): # type: () -> None |
| 16 | + assert find_bom_encoding(b"") == (None, None) |
18 | 17 |
|
19 | 18 |
|
20 | | -def test_find_bom_encoding_nonbom_data(): |
21 | | - # type: () -> None |
22 | | - assert find_bom_encoding(b"asdf") is None |
| 19 | +def test_find_bom_encoding_nonbom_data(): # type: () -> None |
| 20 | + assert find_bom_encoding(b"asdf") == (None, None) |
23 | 21 |
|
24 | 22 |
|
25 | | -def test_find_bom_encoding_utf8_bom(): |
26 | | - # type: () -> None |
27 | | - assert find_bom_encoding(codecs.BOM_UTF8 + b"asdf") == "utf-8" |
| 23 | +def test_find_bom_encoding_utf8_bom(): # type: () -> None |
| 24 | + assert find_bom_encoding(codecs.BOM_UTF8 + b"asdf") == (codecs.BOM_UTF8, "utf-8") |
28 | 25 |
|
29 | 26 |
|
30 | | -def test_find_bom_encoding_utf16_be_bom(): |
31 | | - # type: () -> None |
32 | | - assert find_bom_encoding(codecs.BOM_UTF16_BE + b"asdf") == "utf-16-be" |
| 27 | +def test_find_bom_encoding_utf16_be_bom(): # type: () -> None |
| 28 | + assert find_bom_encoding(codecs.BOM_UTF16_BE + b"asdf") == ( |
| 29 | + codecs.BOM_UTF16_BE, |
| 30 | + "utf-16-be", |
| 31 | + ) |
33 | 32 |
|
34 | 33 |
|
35 | | -def test_find_bom_encoding_utf32_le_bom(): |
36 | | - # type: () -> None |
37 | | - assert find_bom_encoding(codecs.BOM_UTF32_LE + b"asdf") == "utf-32-le" |
| 34 | +def test_find_bom_encoding_utf32_le_bom(): # type: () -> None |
| 35 | + assert find_bom_encoding(codecs.BOM_UTF32_LE + b"asdf") == ( |
| 36 | + codecs.BOM_UTF32_LE, |
| 37 | + "utf-32-le", |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_bom_is_stripped(): # type: () -> None |
| 42 | + # fmt: off |
| 43 | + msg = u"жук" |
| 44 | + # fmt: on |
| 45 | + for bom, enc in BOM_ENCODINGS: |
| 46 | + msg_enc = bom + msg.encode(enc) |
| 47 | + msg_dec = decode_content(msg_enc) |
| 48 | + assert msg_dec == msg |
0 commit comments