<commit_before>from __future__ import with_statement
import os
import unittest

from robot.utils.asserts import assert_equals, assert_raises, assert_true
from robot.utils.xmlsource import XmlSource
from robot.errors import DataError


class TestXmlSource(unittest.TestCase):

    def test_creating_from_filename(self):
        source = XmlSource(__file__)
        with source as src:
            assert_equals(src, __file__)
        self._verify_string_representation(source, __file__)

    def test_xml_like_string_is_opened(self):
        xml = '<tag>content</tag>'
        source = XmlSource(xml)
        with source as src:
            assert_equals(src.read(), xml)
        self._verify_string_representation(source, '<in-memory file>')

    def test_opened_file_object_can_be_used(self):
        fname = os.path.splitext(__file__)[0] + '.py'
        source = XmlSource(open(fname))
        with source as src:
            assert_true(src.read().startswith('import'))
        assert_true(src.closed is False)
        self._verify_string_representation(source, fname)

    def test_filename_is_validated(self):
        def use(src):
            with src as s:
                pass
        assert_raises(DataError, use, XmlSource('nonex.xml'))

    def _verify_string_representation(self, source, expected):
        assert_equals(str(source), expected)
<commit_msg>Fixzed, part II<commit_after>from __future__ import with_statement
import os
import unittest

from robot.utils.asserts import assert_equals, assert_raises, assert_true
from robot.utils.xmlsource import XmlSource
from robot.errors import DataError


class TestXmlSource(unittest.TestCase):

    def test_creating_from_filename(self):
        source = XmlSource(__file__)
        with source as src:
            assert_equals(src, __file__)
        self._verify_string_representation(source, __file__)

    def test_xml_like_string_is_opened(self):
        xml = '<tag>content</tag>'
        source = XmlSource(xml)
        with source as src:
            assert_equals(src.read(), xml)
        self._verify_string_representation(source, '<in-memory file>')

    def test_opened_file_object_can_be_used(self):
        fname = os.path.splitext(__file__)[0] + '.py'
        source = XmlSource(open(fname))
        with source as src:
            assert_true(src.read().startswith('from __future__'))
        assert_true(src.closed is False)
        self._verify_string_representation(source, fname)

    def test_filename_is_validated(self):
        def use(src):
            with src as s:
                pass
        assert_raises(DataError, use, XmlSource('nonex.xml'))

    def _verify_string_representation(self, source, expected):
        assert_equals(str(source), expected)

{'fraction_non_alphanumeric': 0.08846013833272662, 'fraction_numerical': 0.0007280669821623589, 'mean_word_length': 3.227692307692308, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4414726', 'n_tokens_mistral': 824, 'n_tokens_neox': 787, 'n_words': 175}