File: test_tidy.py

package info (click to toggle)
utidylib 0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 216 kB
  • sloc: python: 428; makefile: 148; sh: 41
file content (157 lines) | stat: -rw-r--r-- 5,403 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from __future__ import annotations

import io
import os
import pathlib
import unittest

import tidy
import tidy.lib

DATA_STORAGE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_data")


class TidyTestCase(unittest.TestCase):
    input1 = "<html><script>1>2</script>"
    input2 = "<html>\n" + "<p>asdkfjhasldkfjhsldjas\n" * 100
    test_file = os.path.join(DATA_STORAGE, "test.html")

    def default_docs(self) -> tuple[tidy.Document, tidy.Document, tidy.Document]:
        doc1 = tidy.parseString(self.input1)
        doc2 = tidy.parseString(self.input2)
        doc3 = tidy.parse(self.test_file, char_encoding="ascii")
        return (doc1, doc2, doc3)

    def test_bad_options(self) -> None:
        badopts = [{"foo": 1}]
        for opts in badopts:
            with self.assertRaisesRegex(
                tidy.InvalidOptionError,
                "not a valid Tidy option",
            ):
                tidy.parseString(self.input2, **opts)

    def test_bad_option_values(self) -> None:
        badopts: list[tidy.lib.OPTION_DICT_TYPE] = [
            {"indent": "---"},
            {"indent_spaces": None},
        ]
        for opts in badopts:
            with self.assertRaisesRegex(
                tidy.OptionArgError,
                "missing or malformed argument",
            ):
                tidy.parseString(self.input2, **opts)

    def test_encodings(self) -> None:
        text = (
            pathlib.Path(self.test_file)
            .read_bytes()
            .decode("utf8")
            .encode("ascii", "xmlcharrefreplace")
        )
        doc1u = tidy.parseString(text, input_encoding="ascii", output_encoding="latin1")
        self.assertTrue(doc1u.getvalue().find(b"\xe9") >= 0)
        doc2u = tidy.parseString(text, input_encoding="ascii", output_encoding="utf8")
        self.assertTrue(doc2u.getvalue().find(b"\xc3\xa9") >= 0)

    def test_error_lines(self) -> None:
        for doc in self.default_docs():
            self.assertEqual(doc.errors[0].line, 1)

    def test_nonexisting(self) -> None:
        os.environ.pop("IGNORE_MISSING_TIDY", None)
        doc = tidy.parse(os.path.join(DATA_STORAGE, "missing.html"))
        self.assertEqual(str(doc).strip(), "")
        self.assertIn("missing.html", doc.errors[0].message)
        if doc.errors[0].severity == "E":
            self.assertEqual(doc.errors[0].severity, "E")
            self.assertTrue(str(doc.errors[0]).startswith("Error"))
        else:
            # Tidy 5.5.19 and newer
            self.assertEqual(doc.errors[0].severity, "D")
            self.assertTrue(str(doc.errors[0]).startswith("Document"))

    def test_options(self) -> None:
        doc1 = tidy.parseString(
            self.input1,
            add_xml_decl=1,
            show_errors=1,
            newline="CR",
            output_xhtml=True,
        )
        self.assertIn("CDATA", str(doc1))
        doc2 = tidy.parseString(
            "<Html>",
            add_xml_decl=1,
            show_errors=1,
            newline="CR",
            output_xhtml=True,
        )
        self.assertTrue(str(doc2).startswith("<?xml"))
        self.assertFalse(len(doc2.errors) == 0)
        self.assertNotIn("\n", str(doc2))
        doc3 = tidy.parse(self.test_file, char_encoding="utf8", alt_text="foo")
        self.assertIn('alt="foo"', doc3.gettext())
        self.assertIn("é", doc3.gettext())

    def test_parse(self) -> None:
        doc1, doc2, doc3 = self.default_docs()
        self.assertIn("</html>", str(doc1))
        self.assertIn("</html>", str(doc2))
        self.assertIn("</html>", doc3.gettext())

    def test_big(self) -> None:
        text = "x" * 16384
        doc = tidy.parseString(f"<html><body>{text}</body></html>")
        self.assertIn(text, str(doc))

    def test_unicode(self) -> None:
        doc = tidy.parseString("<html><body>zkouška</body></html>")
        self.assertIn("zkouška", doc.gettext())

    def test_write(self) -> None:
        doc = tidy.parseString(self.input1)
        handle = io.BytesIO()
        doc.write(handle)
        self.assertEqual(doc.getvalue(), handle.getvalue())

    def test_errors(self) -> None:
        doc = tidy.parseString(self.input1)
        for error in doc.errors:
            self.assertTrue(str(error).startswith("line"))
            self.assertTrue(repr(error).startswith("ReportItem"))

    def test_report_item(self) -> None:
        item = tidy.ReportItem("Invalid: error")
        self.assertEqual(item.get_severity(), "Invalid")

    def test_missing_load(self) -> None:
        with self.assertRaises(OSError):
            tidy.lib.Loader(libnames=("not-existing-library",))

    def test_lib_from_environ(self) -> None:
        os.environ["TIDY_LIBRARY_FULL_PATH"] = "/foo/bar/tidy"
        loader = tidy.lib.Loader()
        expected_libnames = (
            "/foo/bar/tidy",
            "libtidy.so",
            "libtidy.dylib",
            "tidy",
            "cygtidy-0-99-0",
            "libtidy-0.99.so.0",
            "libtidy-0.99.so.0.0.0",
            "libtidy.so.5",
            "libtidy.so.58",
            "libtidy.so.5deb1",
            "libtidy",
            "tidylib",
        )
        self.assertEqual(loader.libnames, expected_libnames)

    def test_lib_version(self) -> None:
        self.assertEqual(len(tidy.lib.getTidyVersion().split(".")), 3)


if __name__ == "__main__":
    unittest.main()