Пример #1
0
    def test_read_content_ufs(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        url = "https://raw.githubusercontent.com/sdpython/pyquickhelper/master/src/pyquickhelper/ipythonhelper/magic_parser.py"
        content = read_content_ufs(url, encoding="utf8")
        assert "MagicCommandParser" in content
        assert isinstance(content, str  # unicode#
                          )

        typstr = str  # unicode#

        file = typstr(__file__)
        file_, ext = os.path.splitext(file)
        if ext != ".py":
            file = file_ + ".py"
        content = read_content_ufs(file, encoding="utf8")
        assert "TestDownloadContent" in content

        content2 = read_content_ufs(content, encoding="utf8")
        self.assertEqual(content2, content)

        st = StringIO(content)
        content2 = read_content_ufs(st, encoding="utf8")
        self.assertEqual(content2, content)

        if sys.version_info[0] > 2:
            by = BytesIO(content.encode("utf8"))
            content2 = read_content_ufs(by, encoding="utf8")
            self.assertEqual(content2, content)
Пример #2
0
    def test_read_content_ufs(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        url = "https://raw.githubusercontent.com/sdpython/pyquickhelper/master/src/pyquickhelper/ipythonhelper/magic_parser.py"
        content = read_content_ufs(url, encoding="utf8")
        assert "MagicCommandParser" in content
        assert isinstance(content, str  # unicode#
                          )

        typstr = str  # unicode#

        file = typstr(__file__)
        content = read_content_ufs(file, encoding="utf8")
        assert "TestDownloadContent" in content

        content2 = read_content_ufs(content, encoding="utf8")
        assert content2 == content

        st = io.StringIO(content)
        content2 = read_content_ufs(st, encoding="utf8")
        assert content2 == content

        by = io.BytesIO(content.encode("utf8"))
        content2 = read_content_ufs(by, encoding="utf8")
        assert content2 == content
Пример #3
0
    def test_read_content(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")

        this = os.path.abspath(__file__)
        ext = os.path.splitext(this)[-1]
        if ext != ".py":
            this = this.replace(ext, ".py")

        content1 = read_content_ufs(this)
        content2, source = read_content_ufs(this, add_source=True)
        self.assertEqual(content1, content2)
        if sys.version_info[0] == 2:
            warnings.warn("source is not reliable on Python 2.7")
        else:
            self.assertEqual(source, "r")
        content0 = content1

        content3, source = read_content_ufs(content1, add_source=True)
        self.assertEqual(content1, content3)
        if sys.version_info[0] != 2:
            self.assertEqual(source, "s")

        content4, source = read_content_ufs(
            StringIO(content1), add_source=True)
        self.assertEqual(content4, content1)
        if sys.version_info[0] != 2:
            self.assertEqual(source, "S")

        content4, source = read_content_ufs(
            BytesIO(content1.encode("utf-8")), add_source=True)
        self.assertEqual(content4, content1)
        if sys.version_info[0] != 2:
            self.assertEqual(source, "SB")

        # asbytes
        if sys.version_info[0] == 2:
            warnings.warn(
                "read_content_ufs not tested for bytes on Python 2.7")
            return

        content1 = read_content_ufs(this, asbytes=True)
        content2, source = read_content_ufs(
            this, add_source=True, asbytes=True)
        self.assertEqual(content1, content2)
        self.assertEqual(source, "rb")
        self.assertEqual(type(content1), bytes)

        content3, source = read_content_ufs(
            content1, add_source=True, asbytes=True)
        self.assertEqual(content1, content3)
        self.assertEqual(source, "b")

        content4, source = read_content_ufs(
            StringIO(content0), add_source=True, asbytes=True)
        self.assertEqual(source, "Sb")
        content1 = content1.replace(b'\r', b'')
        if content4 != content1:
            raise Exception("\n{0}\n{1}".format(content4, content1))

        content4, source = read_content_ufs(
            BytesIO(content0.encode("utf-8")), add_source=True, asbytes=True)
        if content4 != content1:
            raise Exception("\n{0}\n{1}".format(content4, content1))
        self.assertEqual(source, "SBb")