diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-02 17:55:46 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-04-02 17:55:46 (GMT) |
commit | e9d4dc192f84d28d03796f18612dc54506e2a74c (patch) | |
tree | 3ed09349a14c65a7595f31c2642bc88e21363585 | |
parent | 61ad42e591ac7c142f3fcb62ca85813c1ffb703c (diff) | |
download | cpython-e9d4dc192f84d28d03796f18612dc54506e2a74c.zip cpython-e9d4dc192f84d28d03796f18612dc54506e2a74c.tar.gz cpython-e9d4dc192f84d28d03796f18612dc54506e2a74c.tar.bz2 |
Issue #2175: Added tests for xml.sax.saxutils.prepare_input_source().
Made test XML files non-ASCII.
-rw-r--r-- | Lib/test/test_sax.py | 61 | ||||
-rw-r--r-- | Lib/test/xmltestdata/test.xml | 4 | ||||
-rw-r--r-- | Lib/test/xmltestdata/test.xml.out | 2 |
3 files changed, 61 insertions, 6 deletions
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py index 86638a2..e314b7c 100644 --- a/Lib/test/test_sax.py +++ b/Lib/test/test_sax.py @@ -9,7 +9,7 @@ except SAXReaderNotAvailable: # don't try to test this module if we cannot create a parser raise ImportError("no XML parsers available") from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \ - XMLFilterBase + XMLFilterBase, prepare_input_source from xml.sax.expatreader import create_parser from xml.sax.handler import feature_namespaces from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl @@ -167,6 +167,60 @@ class SaxutilsTest(unittest.TestCase): p = make_parser(['xml.parsers.no_such_parser']) +class PrepareInputSourceTest(unittest.TestCase): + + def setUp(self): + self.file = support.TESTFN + with open(self.file, "w") as tmp: + tmp.write("This was read from a file.") + + def tearDown(self): + support.unlink(self.file) + + def make_byte_stream(self): + return io.BytesIO(b"This is a byte stream.") + + def checkContent(self, stream, content): + self.assertIsNotNone(stream) + self.assertEqual(stream.read(), content) + stream.close() + + + def test_byte_stream(self): + # If the source is an InputSource that does not have a character + # stream but does have a byte stream, use the byte stream. + src = InputSource(self.file) + src.setByteStream(self.make_byte_stream()) + prep = prepare_input_source(src) + self.assertIsNone(prep.getCharacterStream()) + self.checkContent(prep.getByteStream(), + b"This is a byte stream.") + + def test_system_id(self): + # If the source is an InputSource that has neither a character + # stream nor a byte stream, open the system ID. + src = InputSource(self.file) + prep = prepare_input_source(src) + self.assertIsNone(prep.getCharacterStream()) + self.checkContent(prep.getByteStream(), + b"This was read from a file.") + + def test_string(self): + # If the source is a string, use it as a system ID and open it. + prep = prepare_input_source(self.file) + self.assertIsNone(prep.getCharacterStream()) + self.checkContent(prep.getByteStream(), + b"This was read from a file.") + + def test_binary_file(self): + # If the source is a binary file-like object, use it as a byte + # stream. + prep = prepare_input_source(self.make_byte_stream()) + self.assertIsNone(prep.getCharacterStream()) + self.checkContent(prep.getByteStream(), + b"This is a byte stream.") + + # ===== XMLGenerator start = '<?xml version="1.0" encoding="iso-8859-1"?>\n' @@ -481,7 +535,7 @@ class ExpatReaderTest(XmlTestBase): # ===== XMLReader support - def test_expat_file(self): + def test_expat_binary_file(self): parser = create_parser() result = StringIO() xmlgen = XMLGenerator(result) @@ -660,7 +714,7 @@ class ExpatReaderTest(XmlTestBase): self.assertEqual(result.getvalue(), xml_test_out) - def test_expat_inpsource_stream(self): + def test_expat_inpsource_byte_stream(self): parser = create_parser() result = StringIO() xmlgen = XMLGenerator(result) @@ -896,6 +950,7 @@ class XmlReaderTest(XmlTestBase): def test_main(): run_unittest(MakeParserTest, SaxutilsTest, + PrepareInputSourceTest, StringXmlgenTest, BytesIOXmlgenTest, WriterXmlgenTest, diff --git a/Lib/test/xmltestdata/test.xml b/Lib/test/xmltestdata/test.xml index 9af92fb..92136da 100644 --- a/Lib/test/xmltestdata/test.xml +++ b/Lib/test/xmltestdata/test.xml @@ -1,4 +1,4 @@ -<?xml version="1.0"?> +<?xml version="1.0" encoding="iso-8859-1"?> <HTML xmlns:pp="http://www.isogen.com/paul/post-processor"> <TITLE>Introduction to XSL</TITLE> <H1>Introduction to XSL</H1> @@ -110,6 +110,6 @@ </UL> - +µ </HTML> diff --git a/Lib/test/xmltestdata/test.xml.out b/Lib/test/xmltestdata/test.xml.out index d4ab1ab..f7e9ad2 100644 --- a/Lib/test/xmltestdata/test.xml.out +++ b/Lib/test/xmltestdata/test.xml.out @@ -110,6 +110,6 @@ </UL> - +µ </HTML>
\ No newline at end of file |