xref: /llvm-project/llvm/utils/filecheck_lint/filecheck_lint_test.py (revision 42ebf3eaafc2a5c3c9338020186c0ad44cc4edf7)
1# ===----------------------------------------------------------------------===##
2#
3# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#
7# ===----------------------------------------------------------------------===##
8import unittest
9
10import filecheck_lint as fcl
11
12
13class TestParser(unittest.TestCase):
14    def test_parse_all_additional_prefixes(self):
15        def run(content, expected_prefixes):
16            prefixes = set(fcl.parse_custom_prefixes(content))
17            for prefix in expected_prefixes:
18                self.assertIn(prefix, prefixes)
19
20        for content, expected_prefixes in [
21            ("-check-prefix=PREFIX", {"PREFIX"}),
22            ("-check-prefix='PREFIX'", {"PREFIX"}),
23            ('-check-prefix="PREFIX"', {"PREFIX"}),
24            ("-check-prefix PREFIX", {"PREFIX"}),
25            ("-check-prefix      PREFIX", {"PREFIX"}),
26            ("-check-prefixes=PREFIX1,PREFIX2", {"PREFIX1", "PREFIX2"}),
27            ("-check-prefixes PREFIX1,PREFIX2", {"PREFIX1", "PREFIX2"}),
28            (
29                """-check-prefix=PREFIX1 -check-prefix PREFIX2
30            -check-prefixes=PREFIX3,PREFIX4 -check-prefix=PREFIX5
31            -check-prefixes PREFIX6,PREFIX7 -check-prefixes=PREFIX8',
32         """,  # pylint: disable=bad-continuation
33                {f"PREFIX{i}" for i in range(1, 9)},
34            ),
35        ]:
36            run(content, expected_prefixes)
37
38    def test_additional_prefixes_uniquely(self):
39        lines = ["--check-prefix=SOME-PREFIX", "--check-prefix=SOME-PREFIX"]
40        prefixes = set(fcl.parse_custom_prefixes("\n".join(lines)))
41        assert len(prefixes) == 1
42
43
44class TestTypoDetection(unittest.TestCase):
45    def test_find_potential_directives_comment_prefix(self):
46        lines = ["junk; CHCK1:", "junk// CHCK2:", "SOME CHCK3:"]
47        content = "\n".join(lines)
48
49        results = list(fcl.find_potential_directives(content))
50        assert len(results) == 3
51        pos, match = results[0]
52        assert pos.as_str() == "1:7-11"
53        assert match == "CHCK1"
54
55        pos, match = results[1]
56        assert pos.as_str() == "2:8-12"
57        assert match == "CHCK2"
58
59        pos, match = results[2]
60        assert pos.as_str() == "3:1-10"
61        assert match == "SOME CHCK3"
62
63    def test_levenshtein(self):
64        for s1, s2, distance in [
65            ("Levenshtein", "Levenstin", 2),  # 2 insertions
66            ("Levenshtein", "Levenstherin", 3),  # 1 insertion, 2 deletions
67            ("Levenshtein", "Lenvinshtein", 2),  # 1 deletion, 1 substitution
68            ("Levenshtein", "Levenshtein", 0),  # identical strings
69        ]:
70            assert fcl.levenshtein(s1, s2) == distance
71