1 # Copyright (C) 2020 Red Hat Inc.
4 # Eduardo Habkost <ehabkost@redhat.com>
6 # This work is licensed under the terms of the GNU GPL, version 2. See
7 # the COPYING file in the top-level directory.
8 from tempfile
import NamedTemporaryFile
9 from .patching
import FileInfo
, FileMatch
, Patch
, FileList
10 from .regexps
import *
12 class BasicPattern(FileMatch
):
19 def replacement(self
) -> str:
20 # replace match with the middle character repeated 5 times
21 return self
.group(0)[1].upper()*5
23 def test_pattern_patching():
24 of
= NamedTemporaryFile('wt')
25 of
.writelines(['one line\n',
26 'this pattern will be patched: defbbahij\n',
28 'another pattern: jihaabfed'])
32 f
= FileInfo(files
, of
.name
)
34 matches
= f
.matches_of_type(BasicPattern
)
35 assert len(matches
) == 2
38 # manually add patch, to see if .append() works:
39 f
.patches
.append(p2
.append('XXX'))
42 f
.gen_patches(matches
)
43 patched
= f
.get_patched_content()
44 assert patched
== ('one line\n'+
45 'this pattern will be patched: defBBBBBhij\n'+
47 'another pattern: jihAAAAAXXXfed')
49 class Function(FileMatch
):
50 regexp
= S(r
'BEGIN\s+', NAMED('name', RE_IDENTIFIER
), r
'\n',
53 class Statement(FileMatch
):
54 regexp
= S(r
'^\s*', NAMED('name', RE_IDENTIFIER
), r
'\(\)\n')
56 def test_container_match():
57 of
= NamedTemporaryFile('wt')
58 of
.writelines(['statement1()\n',
72 f
= FileInfo(files
, of
.name
)
74 assert len(f
.matches_of_type(Function
)) == 2
75 print(' '.join(m
.name
for m
in f
.matches_of_type(Statement
)))
76 assert len(f
.matches_of_type(Statement
)) == 7
78 f1
= f
.find_match(Function
, 'function1')
79 f2
= f
.find_match(Function
, 'function2')
80 st1
= f
.find_match(Statement
, 'statement1')
81 st2
= f
.find_match(Statement
, 'statement2')
82 st3
= f
.find_match(Statement
, 'statement3')
83 st4
= f
.find_match(Statement
, 'statement4')
84 st5
= f
.find_match(Statement
, 'statement5')
85 st6
= f
.find_match(Statement
, 'statement6')
86 st7
= f
.find_match(Statement
, 'statement7')
88 assert not f1
.contains(st1
)
89 assert not f1
.contains(st2
)
90 assert not f1
.contains(st2
)
91 assert f1
.contains(st3
)
92 assert f1
.contains(st4
)
93 assert not f1
.contains(st5
)
94 assert not f1
.contains(st6
)
95 assert not f1
.contains(st7
)
97 assert not f2
.contains(st1
)
98 assert not f2
.contains(st2
)
99 assert not f2
.contains(st2
)
100 assert not f2
.contains(st3
)
101 assert not f2
.contains(st4
)
102 assert f2
.contains(st5
)
103 assert f2
.contains(st6
)
104 assert not f2
.contains(st7
)