Skip to content

Commit e417e40

Browse files
committed
Ignore mutations inside string literals
1 parent de76704 commit e417e40

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

app/utils/Mutation.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,41 @@
22

33
import json
44
import re
5-
from typing import Dict, List
5+
from typing import Dict, List, Tuple
66
from app.utils.Replacement import Replacement
77

88

9+
class StringLiteralFinder:
10+
def __init__(self, line):
11+
self.string_literals = [] # type: List[Tuple[int, int]]
12+
13+
for match in re.finditer(re.compile("\".*?\""), line):
14+
self.string_literals.append(match.span())
15+
16+
def is_in_string_literal(self, index):
17+
for (string_start, string_end) in self.string_literals:
18+
if string_start < index < string_end:
19+
return True
20+
21+
return False
22+
23+
24+
##############################################################################
25+
926
class SimplePattern:
1027
def __init__(self, replacement_patterns):
1128
self.replacement_patterns = replacement_patterns # type: Dict[str, List[str]]
1229

1330
def mutate(self, line):
1431
result = [] # type: List[Replacement]
1532

33+
string_finder = StringLiteralFinder(line)
34+
1635
for replacement_pattern in self.replacement_patterns.keys():
1736
for occurrence in [match for match in re.finditer(re.compile(replacement_pattern), line)]:
37+
if string_finder.is_in_string_literal(occurrence.start()):
38+
continue
39+
1840
for replacement_str in self.replacement_patterns[replacement_pattern]:
1941
result.append(Replacement(start_col=occurrence.start(),
2042
end_col=occurrence.end(),
@@ -244,7 +266,12 @@ def __init__(self):
244266
def find_mutations(self, line):
245267
result = [] # type: List[Replacement]
246268

269+
string_finder = StringLiteralFinder(line)
270+
247271
for occurrence in [match for match in re.finditer(re.compile(self.regex), line)]:
272+
if string_finder.is_in_string_literal(occurrence.start()):
273+
continue
274+
248275
number_str = occurrence.group(1)
249276

250277
# use JSON as means to cope with both int and double
@@ -274,7 +301,12 @@ def __init__(self):
274301
def find_mutations(self, line):
275302
result = [] # type: List[Replacement]
276303

304+
string_finder = StringLiteralFinder(line)
305+
277306
for occurrence in [match for match in re.finditer(re.compile(self.regex), line)]:
307+
if string_finder.is_in_string_literal(occurrence.start()):
308+
continue
309+
278310
number_str = occurrence.group()
279311

280312
# use JSON as means to cope with both int and double

0 commit comments

Comments
 (0)