xref: /llvm-project/llvm/utils/filecheck_lint/filecheck_lint_test.py (revision f702c75995db9ea3978d39e3f3885b2113356f68)
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
15  def test_parse_all_additional_prefixes(self):
16
17    def run(content, expected_prefixes):
18      prefixes = set(fcl.parse_custom_prefixes(content))
19      for prefix in expected_prefixes:
20        self.assertIn(prefix, prefixes)
21
22    for content, expected_prefixes in [
23        ('-check-prefix=PREFIX', {'PREFIX'}),
24        ('-check-prefix=\'PREFIX\'', {'PREFIX'}),
25        ('-check-prefix="PREFIX"', {'PREFIX'}),
26        ('-check-prefix PREFIX', {'PREFIX'}),
27        ('-check-prefix      PREFIX', {'PREFIX'}),
28        ('-check-prefixes=PREFIX1,PREFIX2', {'PREFIX1', 'PREFIX2'}),
29        ('-check-prefixes PREFIX1,PREFIX2', {'PREFIX1', 'PREFIX2'}),
30        (
31            """-check-prefix=PREFIX1 -check-prefix PREFIX2
32            -check-prefixes=PREFIX3,PREFIX4 -check-prefix=PREFIX5
33            -check-prefixes PREFIX6,PREFIX7 -check-prefixes=PREFIX8',
34         """,  # pylint: disable=bad-continuation
35            {f'PREFIX{i}' for i in range(1, 9)}),
36    ]:
37      run(content, expected_prefixes)
38
39  def test_additional_prefixes_uniquely(self):
40    lines = ['--check-prefix=SOME-PREFIX', '--check-prefix=SOME-PREFIX']
41    prefixes = set(fcl.parse_custom_prefixes('\n'.join(lines)))
42    assert len(prefixes) == 1
43
44
45class TestTypoDetection(unittest.TestCase):
46
47  def test_find_potential_directives_comment_prefix(self):
48    lines = ['junk; CHCK1:', 'junk// CHCK2:', 'SOME CHCK3:']
49    content = '\n'.join(lines)
50
51    results = list(fcl.find_potential_directives(content))
52    assert len(results) == 3
53    pos, match = results[0]
54    assert (pos.line == 1 and
55            pos.start_column == len('junk; ') + 1 and
56            pos.end_column == len(lines[0]) - 1)
57    assert match == 'CHCK1'
58
59    pos, match = results[1]
60    assert (pos.line == 2 and
61            pos.start_column == len('junk// ') + 1 and
62            pos.end_column == len(lines[1]) - 1)
63    assert match == 'CHCK2'
64
65    pos, match = results[2]
66    assert (pos.line == 3 and
67            pos.start_column == 1 and
68            pos.end_column == len(lines[2]) - 1)
69    assert match == 'SOME CHCK3'
70
71  def test_levenshtein(self):
72    for s1, s2, distance in [
73        ('Levenshtein', 'Levenstin', 2),  # 2 insertions
74        ('Levenshtein', 'Levenstherin', 3),  # 1 insertion, 2 deletions
75        ('Levenshtein', 'Lenvinshtein', 2),  # 1 deletion, 1 substitution
76        ('Levenshtein', 'Levenshtein', 0),  # identical strings
77    ]:
78      assert fcl.levenshtein(s1, s2) == distance
79