xref: /trunk/test/aoo-test/tests/selftest/test_formulacheck.py (revision 2cb6d6f119bbe2f78742f1c5c8538cb0d4e72ede)
1################################################################
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20################################################################
21
22"""formula-cases.tsv: every row gives exactly the expected findings from formulacheck.check_stream()."""
23import collections
24import functools
25from xml.sax.saxutils import quoteattr
26
27import pytest
28
29from aootest.checkers import formulacheck
30from aootest.paths import DATA
31
32pytestmark = pytest.mark.selftest
33
34CASES = [l.rstrip("\n").split("\t") for l in open(DATA / "formula-cases.tsv", encoding="utf-8") if not l.startswith("#") and l.strip()]
35NSDECL = ('xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" '
36          'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" '
37          'xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:ooow="http://openoffice.org/2004/writer"')
38
39
40@functools.cache
41def findings():
42    """All cases in one document; row n is on line n + 2."""
43    lines = ["<office:document-content %s><office:body>" % NSDECL]
44    for st, attr, val, msg in CASES:
45        val = val.replace("\\n", "\n").replace("\\u00a0", " ")
46        name, _, ctx = attr.partition("@")
47        tag = "office:text" if ctx == "text" else "office:spreadsheet"
48        lines.append("<%s><table:table-cell %s=%s/></%s>" % (tag, name, quoteattr(val, {"\n": "&#10;"}), tag))
49    lines.append("</office:body></office:document-content>")
50    got = collections.defaultdict(list)
51    for line, col, level, rule, m in formulacheck.check_stream("\n".join(lines).encode("utf-8"), collections.Counter()):
52        got[line - 2].append((level + ":" + rule, m))
53    return got
54
55
56@pytest.mark.parametrize("n", range(len(CASES)), ids=lambda n: "%03d %s %s" % (n, CASES[n][1], CASES[n][2][:40].replace("\\n", " ")))
57def test_formula_case(n):
58    st, attr, val, msg = CASES[n]
59    want = [] if st == "ok" else st.split("+")
60    got = findings()[n]
61    assert sorted(r for r, _ in got) == sorted(want), (attr, val, got)
62    assert not msg or any(msg in m for _, m in got), (attr, val, msg, got)
63
64
65def test_formula_case_table_is_not_empty():
66    assert len(CASES) > 100
67