Read a Rust string with a regex and kill the surviving mutants
CI / tests (pull_request) Failing after 5m39s
CI / tests (pull_request) Failing after 5m39s
This commit is contained in:
@@ -13,6 +13,7 @@ SCAN = re.compile(
|
|||||||
rb"""|\#\[cfg\(test\)\]""",
|
rb"""|\#\[cfg\(test\)\]""",
|
||||||
re.DOTALL,
|
re.DOTALL,
|
||||||
)
|
)
|
||||||
|
STRING_BODY = re.compile(rb'(?:[^"\\]|\\.)*"', re.DOTALL)
|
||||||
COMMENT_EDGE = re.compile(rb"/\*|\*/")
|
COMMENT_EDGE = re.compile(rb"/\*|\*/")
|
||||||
# What must follow the attribute for it to start an inline module: other attributes, a visibility, then `mod name {`.
|
# What must follow the attribute for it to start an inline module: other attributes, a visibility, then `mod name {`.
|
||||||
MODULE = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*\{")
|
MODULE = re.compile(rb"\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\([^)]*\))?\s+)?mod\s+(?P<name>\w+)\s*\{")
|
||||||
@@ -37,9 +38,9 @@ def modules(source: bytes) -> dict[bytes, tuple[int, int]] | None:
|
|||||||
if token == b"/*":
|
if token == b"/*":
|
||||||
position = after_comment(source, position)
|
position = after_comment(source, position)
|
||||||
elif token == b'"':
|
elif token == b'"':
|
||||||
position = after_string(source, position, b'"')
|
position = after_string(source, position)
|
||||||
elif match.group("hashes") is not None:
|
elif match.group("hashes") is not None:
|
||||||
position = after_string(source, position, b'"' + match.group("hashes"), raw=True)
|
position = after_raw_string(source, position, match.group("hashes"))
|
||||||
elif token == b"{":
|
elif token == b"{":
|
||||||
depth += 1
|
depth += 1
|
||||||
elif token == b"}":
|
elif token == b"}":
|
||||||
@@ -63,14 +64,17 @@ def after_comment(source: bytes, position: int) -> int | None:
|
|||||||
return position if level == 0 else None
|
return position if level == 0 else None
|
||||||
|
|
||||||
|
|
||||||
def after_string(source: bytes, position: int, closing: bytes, raw: bool = False) -> int | None:
|
def after_string(source: bytes, position: int) -> int | None:
|
||||||
"""The offset after the string whose opening quote ends at position, None where it is never closed. A raw string has no escapes."""
|
"""The offset after the string whose opening quote ends at position, None where it is never closed. A backslash escapes the next byte."""
|
||||||
while (found := source.find(closing, position)) >= 0:
|
closed = STRING_BODY.match(source, position)
|
||||||
between = source[position:found]
|
return closed.end() if closed else None
|
||||||
if raw or (len(between) - len(between.rstrip(b"\\"))) % 2 == 0:
|
|
||||||
return found + len(closing)
|
|
||||||
position = found + 1
|
def after_raw_string(source: bytes, position: int, hashes: bytes) -> int | None:
|
||||||
return None
|
"""The offset after the raw string, opened by `r`, hashes and a quote, that ends at position, None where it is never closed. It has no escapes."""
|
||||||
|
closing = b'"' + hashes
|
||||||
|
found = source.find(closing, position)
|
||||||
|
return found + len(closing) if found >= 0 else None
|
||||||
|
|
||||||
|
|
||||||
def with_tests_of(planted: bytes, copy: bytes) -> bytes:
|
def with_tests_of(planted: bytes, copy: bytes) -> bytes:
|
||||||
|
|||||||
@@ -73,18 +73,15 @@ class ModulesTest(unittest.TestCase):
|
|||||||
""")
|
""")
|
||||||
self.assertEqual(spelt(source), {"tests": source[:source.index("}\nfn after")+1]})
|
self.assertEqual(spelt(source), {"tests": source[:source.index("}\nfn after")+1]})
|
||||||
|
|
||||||
def test_reads_a_string_that_ends_in_backslashes_or_in_an_escaped_quote(self):
|
def test_reads_a_raw_string_that_ends_in_a_backslash(self):
|
||||||
source = textwrap.dedent("""\
|
source = "#[cfg(test)]\nmod tests {\n const A: &str = r\"\\\";\n}\nfn after() {}\n"
|
||||||
#[cfg(test)]
|
self.assertEqual(list(spelt(source)), ["tests"])
|
||||||
mod tests {
|
|
||||||
const A: &str = r"\\";
|
def test_reads_a_string_that_ends_in_two_backslashes_or_in_an_escaped_quote(self):
|
||||||
const B: &str = "\\\\";
|
for literal in ('"\\\\"', '"a\\""', '"\\}\\}"'):
|
||||||
const C: &str = "a\\"";
|
with self.subTest(literal=literal):
|
||||||
const D: &str = "\\}\\}";
|
source = "#[cfg(test)]\nmod tests {\n const A: &str = " + literal + ";\n}\nfn after() {}\n"
|
||||||
}
|
self.assertEqual(list(spelt(source)), ["tests"])
|
||||||
fn after() {}
|
|
||||||
""")
|
|
||||||
self.assertEqual(spelt(source), {"tests": source[:source.index("}\nfn after") + 1]})
|
|
||||||
|
|
||||||
def test_leaves_out_a_module_a_comment_or_a_char_stands_before(self):
|
def test_leaves_out_a_module_a_comment_or_a_char_stands_before(self):
|
||||||
self.assertEqual(modules("// helpers\nmod util {}\nfn a() { let c = 'x'; }\nmod more {}\n"), {})
|
self.assertEqual(modules("// helpers\nmod util {}\nfn a() { let c = 'x'; }\nmod more {}\n"), {})
|
||||||
|
|||||||
@@ -736,11 +736,11 @@ class CheckedTreeTest(unittest.TestCase):
|
|||||||
self.assertEqual(self.held("check"), {"src/lib.rs": "fn a() { 1 }\n#[cfg(test)]\nmod tests { fn t() {} }\n"})
|
self.assertEqual(self.held("check"), {"src/lib.rs": "fn a() { 1 }\n#[cfg(test)]\nmod tests { fn t() {} }\n"})
|
||||||
|
|
||||||
def test_leaves_a_rust_link_as_a_link_whatever_it_points_at(self):
|
def test_leaves_a_rust_link_as_a_link_whatever_it_points_at(self):
|
||||||
planted = self.tree("planted", {"lib.rs": "fn a() {}\n#[cfg(test)]\nmod tests {}\n", "real.txt": "text"})
|
planted = self.tree("planted", {"z.rs": "fn a() {}\n#[cfg(test)]\nmod tests {}\n", "a.txt": "text"})
|
||||||
copy = self.tree("copy", {"real.txt": "text"})
|
copy = self.tree("copy", {"a.txt": "text"})
|
||||||
os.symlink("real.txt", os.path.join(copy, "lib.rs"))
|
os.symlink("a.txt", os.path.join(copy, "z.rs"))
|
||||||
checked_tree(planted, copy, str(self.root / "check"))
|
checked_tree(planted, copy, str(self.root / "check"))
|
||||||
self.assertEqual((os.readlink(self.root / "check/lib.rs"), self.held("check")["real.txt"]), ("real.txt", "text"))
|
self.assertEqual((os.readlink(self.root / "check/z.rs"), self.held("check")["a.txt"]), ("a.txt", "text"))
|
||||||
|
|
||||||
def test_copies_a_link_as_a_link(self):
|
def test_copies_a_link_as_a_link(self):
|
||||||
planted = self.tree("planted", {"calc.go": "bug"})
|
planted = self.tree("planted", {"calc.go": "bug"})
|
||||||
|
|||||||
Reference in New Issue
Block a user