|
9 | 9 | import functools |
10 | 10 | import html |
11 | 11 | import io |
| 12 | +import locale |
12 | 13 | import operator |
13 | 14 | import pickle |
14 | 15 | import sys |
@@ -756,6 +757,128 @@ def test_writestring(self): |
756 | 757 | elem = ET.fromstring("<html><body>text</body></html>") |
757 | 758 | self.assertEqual(ET.tostring(elem), b'<html><body>text</body></html>') |
758 | 759 |
|
| 760 | + def test_tostring_default_namespace(self): |
| 761 | + elem = ET.XML('<body xmlns="http://effbot.org/ns"><tag/></body>') |
| 762 | + self.assertEqual( |
| 763 | + ET.tostring(elem, encoding='unicode'), |
| 764 | + '<ns0:body xmlns:ns0="http://effbot.org/ns"><ns0:tag /></ns0:body>' |
| 765 | + ) |
| 766 | + self.assertEqual( |
| 767 | + ET.tostring(elem, encoding='unicode', default_namespace='http://effbot.org/ns'), |
| 768 | + '<body xmlns="http://effbot.org/ns"><tag /></body>' |
| 769 | + ) |
| 770 | + |
| 771 | + def test_tostring_default_namespace_different_namespace(self): |
| 772 | + elem = ET.XML('<body xmlns="http://effbot.org/ns"><tag/></body>') |
| 773 | + self.assertEqual( |
| 774 | + ET.tostring(elem, encoding='unicode', default_namespace='foobar'), |
| 775 | + '<ns1:body xmlns="foobar" xmlns:ns1="http://effbot.org/ns"><ns1:tag /></ns1:body>' |
| 776 | + ) |
| 777 | + |
| 778 | + def test_tostring_default_namespace_original_no_namespace(self): |
| 779 | + elem = ET.XML('<body><tag/></body>') |
| 780 | + EXPECTED_MSG = '^cannot use non-qualified names with default_namespace option$' |
| 781 | + with self.assertRaisesRegex(ValueError, EXPECTED_MSG): |
| 782 | + ET.tostring(elem, encoding='unicode', default_namespace='foobar') |
| 783 | + |
| 784 | + def test_tostring_no_xml_declaration(self): |
| 785 | + elem = ET.XML('<body><tag/></body>') |
| 786 | + self.assertEqual( |
| 787 | + ET.tostring(elem, encoding='unicode'), |
| 788 | + '<body><tag /></body>' |
| 789 | + ) |
| 790 | + |
| 791 | + def test_tostring_xml_declaration(self): |
| 792 | + elem = ET.XML('<body><tag/></body>') |
| 793 | + self.assertEqual( |
| 794 | + ET.tostring(elem, encoding='utf8', xml_declaration=True), |
| 795 | + b"<?xml version='1.0' encoding='utf8'?>\n<body><tag /></body>" |
| 796 | + ) |
| 797 | + |
| 798 | + def test_tostring_xml_declaration_unicode_encoding(self): |
| 799 | + elem = ET.XML('<body><tag/></body>') |
| 800 | + preferredencoding = locale.getpreferredencoding() |
| 801 | + self.assertEqual( |
| 802 | + f"<?xml version='1.0' encoding='{preferredencoding}'?>\n<body><tag /></body>", |
| 803 | + ET.tostring(elem, encoding='unicode', xml_declaration=True) |
| 804 | + ) |
| 805 | + |
| 806 | + def test_tostring_xml_declaration_cases(self): |
| 807 | + elem = ET.XML('<body><tag>ø</tag></body>') |
| 808 | + preferredencoding = locale.getpreferredencoding() |
| 809 | + TESTCASES = [ |
| 810 | + # (expected_retval, encoding, xml_declaration) |
| 811 | + # ... xml_declaration = None |
| 812 | + (b'<body><tag>ø</tag></body>', None, None), |
| 813 | + (b'<body><tag>\xc3\xb8</tag></body>', 'UTF-8', None), |
| 814 | + (b'<body><tag>ø</tag></body>', 'US-ASCII', None), |
| 815 | + (b"<?xml version='1.0' encoding='ISO-8859-1'?>\n" |
| 816 | + b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', None), |
| 817 | + ('<body><tag>ø</tag></body>', 'unicode', None), |
| 818 | + |
| 819 | + # ... xml_declaration = False |
| 820 | + (b"<body><tag>ø</tag></body>", None, False), |
| 821 | + (b"<body><tag>\xc3\xb8</tag></body>", 'UTF-8', False), |
| 822 | + (b"<body><tag>ø</tag></body>", 'US-ASCII', False), |
| 823 | + (b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', False), |
| 824 | + ("<body><tag>ø</tag></body>", 'unicode', False), |
| 825 | + |
| 826 | + # ... xml_declaration = True |
| 827 | + (b"<?xml version='1.0' encoding='us-ascii'?>\n" |
| 828 | + b"<body><tag>ø</tag></body>", None, True), |
| 829 | + (b"<?xml version='1.0' encoding='UTF-8'?>\n" |
| 830 | + b"<body><tag>\xc3\xb8</tag></body>", 'UTF-8', True), |
| 831 | + (b"<?xml version='1.0' encoding='US-ASCII'?>\n" |
| 832 | + b"<body><tag>ø</tag></body>", 'US-ASCII', True), |
| 833 | + (b"<?xml version='1.0' encoding='ISO-8859-1'?>\n" |
| 834 | + b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', True), |
| 835 | + (f"<?xml version='1.0' encoding='{preferredencoding}'?>\n" |
| 836 | + "<body><tag>ø</tag></body>", 'unicode', True), |
| 837 | + |
| 838 | + ] |
| 839 | + for expected_retval, encoding, xml_declaration in TESTCASES: |
| 840 | + with self.subTest(f'encoding={encoding} ' |
| 841 | + f'xml_declaration={xml_declaration}'): |
| 842 | + self.assertEqual( |
| 843 | + ET.tostring( |
| 844 | + elem, |
| 845 | + encoding=encoding, |
| 846 | + xml_declaration=xml_declaration |
| 847 | + ), |
| 848 | + expected_retval |
| 849 | + ) |
| 850 | + |
| 851 | + def test_tostringlist_default_namespace(self): |
| 852 | + elem = ET.XML('<body xmlns="http://effbot.org/ns"><tag/></body>') |
| 853 | + self.assertEqual( |
| 854 | + ''.join(ET.tostringlist(elem, encoding='unicode')), |
| 855 | + '<ns0:body xmlns:ns0="http://effbot.org/ns"><ns0:tag /></ns0:body>' |
| 856 | + ) |
| 857 | + self.assertEqual( |
| 858 | + ''.join(ET.tostringlist(elem, encoding='unicode', default_namespace='http://effbot.org/ns')), |
| 859 | + '<body xmlns="http://effbot.org/ns"><tag /></body>' |
| 860 | + ) |
| 861 | + |
| 862 | + def test_tostringlist_xml_declaration(self): |
| 863 | + elem = ET.XML('<body><tag/></body>') |
| 864 | + self.assertEqual( |
| 865 | + ''.join(ET.tostringlist(elem, encoding='unicode')), |
| 866 | + '<body><tag /></body>' |
| 867 | + ) |
| 868 | + self.assertEqual( |
| 869 | + b''.join(ET.tostringlist(elem, xml_declaration=True)), |
| 870 | + b"<?xml version='1.0' encoding='us-ascii'?>\n<body><tag /></body>" |
| 871 | + ) |
| 872 | + |
| 873 | + preferredencoding = locale.getpreferredencoding() |
| 874 | + stringlist = ET.tostringlist(elem, encoding='unicode', xml_declaration=True) |
| 875 | + self.assertEqual( |
| 876 | + ''.join(stringlist), |
| 877 | + f"<?xml version='1.0' encoding='{preferredencoding}'?>\n<body><tag /></body>" |
| 878 | + ) |
| 879 | + self.assertRegex(stringlist[0], r"^<\?xml version='1.0' encoding='.+'?>") |
| 880 | + self.assertEqual(['<body', '>', '<tag', ' />', '</body>'], stringlist[1:]) |
| 881 | + |
759 | 882 | def test_encoding(self): |
760 | 883 | def check(encoding, body=''): |
761 | 884 | xml = ("<?xml version='1.0' encoding='%s'?><xml>%s</xml>" % |
|
0 commit comments