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\)\]""",
|
||||
re.DOTALL,
|
||||
)
|
||||
STRING_BODY = re.compile(rb'(?:[^"\\]|\\.)*"', re.DOTALL)
|
||||
COMMENT_EDGE = re.compile(rb"/\*|\*/")
|
||||
# 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*\{")
|
||||
@@ -37,9 +38,9 @@ def modules(source: bytes) -> dict[bytes, tuple[int, int]] | None:
|
||||
if token == b"/*":
|
||||
position = after_comment(source, position)
|
||||
elif token == b'"':
|
||||
position = after_string(source, position, b'"')
|
||||
position = after_string(source, position)
|
||||
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"{":
|
||||
depth += 1
|
||||
elif token == b"}":
|
||||
@@ -63,14 +64,17 @@ def after_comment(source: bytes, position: int) -> int | None:
|
||||
return position if level == 0 else None
|
||||
|
||||
|
||||
def after_string(source: bytes, position: int, closing: bytes, raw: bool = False) -> 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."""
|
||||
while (found := source.find(closing, position)) >= 0:
|
||||
between = source[position:found]
|
||||
if raw or (len(between) - len(between.rstrip(b"\\"))) % 2 == 0:
|
||||
return found + len(closing)
|
||||
position = found + 1
|
||||
return 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 backslash escapes the next byte."""
|
||||
closed = STRING_BODY.match(source, position)
|
||||
return closed.end() if closed else None
|
||||
|
||||
|
||||
def after_raw_string(source: bytes, position: int, hashes: bytes) -> int | 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:
|
||||
|
||||
@@ -73,18 +73,15 @@ class ModulesTest(unittest.TestCase):
|
||||
""")
|
||||
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):
|
||||
source = textwrap.dedent("""\
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
const A: &str = r"\\";
|
||||
const B: &str = "\\\\";
|
||||
const C: &str = "a\\"";
|
||||
const D: &str = "\\}\\}";
|
||||
}
|
||||
fn after() {}
|
||||
""")
|
||||
self.assertEqual(spelt(source), {"tests": source[:source.index("}\nfn after") + 1]})
|
||||
def test_reads_a_raw_string_that_ends_in_a_backslash(self):
|
||||
source = "#[cfg(test)]\nmod tests {\n const A: &str = r\"\\\";\n}\nfn after() {}\n"
|
||||
self.assertEqual(list(spelt(source)), ["tests"])
|
||||
|
||||
def test_reads_a_string_that_ends_in_two_backslashes_or_in_an_escaped_quote(self):
|
||||
for literal in ('"\\\\"', '"a\\""', '"\\}\\}"'):
|
||||
with self.subTest(literal=literal):
|
||||
source = "#[cfg(test)]\nmod tests {\n const A: &str = " + literal + ";\n}\nfn after() {}\n"
|
||||
self.assertEqual(list(spelt(source)), ["tests"])
|
||||
|
||||
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"), {})
|
||||
|
||||
@@ -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"})
|
||||
|
||||
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"})
|
||||
copy = self.tree("copy", {"real.txt": "text"})
|
||||
os.symlink("real.txt", os.path.join(copy, "lib.rs"))
|
||||
planted = self.tree("planted", {"z.rs": "fn a() {}\n#[cfg(test)]\nmod tests {}\n", "a.txt": "text"})
|
||||
copy = self.tree("copy", {"a.txt": "text"})
|
||||
os.symlink("a.txt", os.path.join(copy, "z.rs"))
|
||||
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):
|
||||
planted = self.tree("planted", {"calc.go": "bug"})
|
||||
|
||||
Reference in New Issue
Block a user