1"""
2    test_util_matching
3    ~~~~~~~~~~~~~~~~~~
4
5    Tests sphinx.util.matching functions.
6
7    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8    :license: BSD, see LICENSE for details.
9"""
10from sphinx.util.matching import Matcher, compile_matchers
11
12
13def test_compile_matchers():
14    # exact matching
15    pat = compile_matchers(['hello.py']).pop()
16    assert pat('hello.py')
17    assert not pat('hello-py')
18    assert not pat('subdir/hello.py')
19
20    # wild card (*)
21    pat = compile_matchers(['hello.*']).pop()
22    assert pat('hello.py')
23    assert pat('hello.rst')
24
25    pat = compile_matchers(['*.py']).pop()
26    assert pat('hello.py')
27    assert pat('world.py')
28    assert not pat('subdir/hello.py')
29
30    # wild card (**)
31    pat = compile_matchers(['hello.**']).pop()
32    assert pat('hello.py')
33    assert pat('hello.rst')
34    assert pat('hello.py/world.py')
35
36    pat = compile_matchers(['**.py']).pop()
37    assert pat('hello.py')
38    assert pat('world.py')
39    assert pat('subdir/hello.py')
40
41    pat = compile_matchers(['**/hello.py']).pop()
42    assert not pat('hello.py')
43    assert pat('subdir/hello.py')
44    assert pat('subdir/subdir/hello.py')
45
46    # wild card (?)
47    pat = compile_matchers(['hello.?']).pop()
48    assert pat('hello.c')
49    assert not pat('hello.py')
50
51    # pattern ([...])
52    pat = compile_matchers(['hello[12\\].py']).pop()
53    assert pat('hello1.py')
54    assert pat('hello2.py')
55    assert pat('hello\\.py')
56    assert not pat('hello3.py')
57
58    pat = compile_matchers(['hello[^12].py']).pop()  # "^" is not negative identifier
59    assert pat('hello1.py')
60    assert pat('hello2.py')
61    assert pat('hello^.py')
62    assert not pat('hello3.py')
63
64    # negative pattern ([!...])
65    pat = compile_matchers(['hello[!12].py']).pop()
66    assert not pat('hello1.py')
67    assert not pat('hello2.py')
68    assert not pat('hello/.py')  # negative pattern does not match to "/"
69    assert pat('hello3.py')
70
71    # non patterns
72    pat = compile_matchers(['hello[.py']).pop()
73    assert pat('hello[.py')
74    assert not pat('hello.py')
75
76    pat = compile_matchers(['hello[].py']).pop()
77    assert pat('hello[].py')
78    assert not pat('hello.py')
79
80    pat = compile_matchers(['hello[!].py']).pop()
81    assert pat('hello[!].py')
82    assert not pat('hello.py')
83
84
85def test_Matcher():
86    matcher = Matcher(['hello.py', '**/world.py'])
87    assert matcher('hello.py')
88    assert not matcher('subdir/hello.py')
89    assert matcher('world.py')
90    assert matcher('subdir/world.py')
91