1 # ===----------------------------------------------------------------------===##
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
7 # ===----------------------------------------------------------------------===##
10 import filecheck_lint
as fcl
13 class 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
)
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"}),
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)},
36 run(content
, expected_prefixes
)
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
44 class 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
)
49 results
= list(fcl
.find_potential_directives(content
))
50 assert len(results
) == 3
51 pos
, match
= results
[0]
54 and pos
.start_column
== len("junk; ") + 1
55 and pos
.end_column
== len(lines
[0]) - 1
57 assert match
== "CHCK1"
59 pos
, match
= results
[1]
62 and pos
.start_column
== len("junk// ") + 1
63 and pos
.end_column
== len(lines
[1]) - 1
65 assert match
== "CHCK2"
67 pos
, match
= results
[2]
70 and pos
.start_column
== 1
71 and pos
.end_column
== len(lines
[2]) - 1
73 assert match
== "SOME CHCK3"
75 def test_levenshtein(self
):
76 for s1
, s2
, distance
in [
77 ("Levenshtein", "Levenstin", 2), # 2 insertions
78 ("Levenshtein", "Levenstherin", 3), # 1 insertion, 2 deletions
79 ("Levenshtein", "Lenvinshtein", 2), # 1 deletion, 1 substitution
80 ("Levenshtein", "Levenshtein", 0), # identical strings
82 assert fcl
.levenshtein(s1
, s2
) == distance