def _test_string(self, xml): source = ETSource(xml) with source as src: assert_equals(src.read().decode('UTF-8'), xml) self._verify_string_representation(source, '<in-memory file>') assert_true(source._opened.closed) with ETSource(xml) as src: assert_equals(ET.parse(src).getroot().tag, 'tag')
def _test_string(self, xml, encoding='UTF-8'): source = ETSource(xml) with source as src: content = src.read() expected = xml if isinstance(xml, bytes) else xml.encode(encoding) assert_equal(content, expected) self._verify_string_representation(source, '<in-memory file>') assert_true(source._opened.closed) with ETSource(xml) as src: assert_equal(ET.parse(src).getroot().tag, 'tag')
def _test_string(self, xml): source = ETSource(xml) with source as src: content = src.read() if not IRONPYTHON_WITH_BROKEN_ETREE: content = content.decode('UTF-8') assert_equal(content, xml) self._verify_string_representation(source, '<in-memory file>') assert_true(source._opened.closed) with ETSource(xml) as src: assert_equal(ET.parse(src).getroot().tag, 'tag')
def test_opened_file_object(self): source = ETSource(open(PATH)) with source as src: assert_true(src.read().startswith(STARTSWITH)) assert_true(src.closed is False) self._verify_string_representation(source, PATH) assert_true(source._opened is None)
def test_xml_string(self): xml = '<tag>content</tag>' source = ETSource(xml) with source as src: assert_equals(src.read(), xml) self._verify_string_representation(source, '<in-memory file>') assert_true(source._opened.closed)
def test_opened_file_object(self): with open(PATH) as f: source = ETSource(f) with source as src: assert_true(src.read().startswith('import os')) assert_true(src is f) assert_true(src.closed is False) self._verify_string_representation(source, PATH) assert_true(source._opened is None) assert_true(src is f) assert_true(src.closed is True)
def test_path_to_file(self): source = ETSource(PATH) with source as src: if IRONPYTHON: assert_equals(src, PATH) else: assert_true(src.read().startswith(STARTSWITH.encode())) self._verify_string_representation(source, PATH) if IRONPYTHON: assert_true(source._opened is None) else: assert_true(source._opened.closed)
def test_non_ascii_string_repr(self): self._verify_string_representation(ETSource(u'\xe4'), u'\xe4')
def test_path_is_validated(self): def use(src): with src: pass assert_raises(IOError, use, ETSource('nonex.xml'))
def test_path_to_file(self): source = ETSource(PATH) with source as src: assert_equals(src, PATH) self._verify_string_representation(source, PATH) assert_true(source._opened is None)
def _test_path(self, path, string_repr=None, expected=None): source = ETSource(path) with source as src: assert_equal(src, expected or path) self._verify_string_representation(source, string_repr or path) assert_true(source._opened is None)