From 46d6658d4e3b79bf6fb8d88e15e1e3595b0a4fa8 Mon Sep 17 00:00:00 2001 From: Soham Sahasrabudhe <91265382+sohams5002@users.noreply.github.com> Date: Tue, 1 Dec 2026 18:04:23 +0530 Subject: [PATCH] Add unit tests for _punycode to_ascii/to_unicode Add tests for punycode encoding and decoding functions. --- tests/test_api/test_punycode.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_api/test_punycode.py diff --git a/tests/test_api/test_punycode.py b/tests/test_api/test_punycode.py new file mode 100644 index 00000000..46ba4c78 --- /dev/null +++ b/tests/test_api/test_punycode.py @@ -0,0 +1,28 @@ +from markdown_it import _punycode + + +def test_to_ascii_encodes_non_ascii_labels(): + assert _punycode.to_ascii("bücher.de") == "xn--bcher-kva.de" + # ASCII labels are passed through unchanged. + assert _punycode.to_ascii("example.com") == "example.com" + + +def test_to_unicode_decodes_xn_prefix_labels(): + assert _punycode.to_unicode("xn--bcher-kva.de") == "bücher.de" + # Labels without the xn-- prefix are passed through unchanged. + assert _punycode.to_unicode("example.com") == "example.com" + + +def test_email_addresses_only_map_the_domain(): + # The local part is left intact, only the domain is punycoded. + assert _punycode.to_ascii("user@bücher.de") == "user@xn--bcher-kva.de" + assert _punycode.to_unicode("user@xn--bcher-kva.de") == "user@bücher.de" + + +def test_alternative_dot_separators(): + # U+3002, U+FF0E and U+FF61 are treated as label separators. + assert _punycode.to_ascii("www\u3002bücher\uff0ede") == "www.xn--bcher-kva.de" + + +def test_encode_decode_round_trip(): + assert _punycode.decode(_punycode.encode("bücher")) == "bücher"