diff --git a/crates/bough-core/src/language/javascript.rs b/crates/bough-core/src/language/javascript.rs index bb972f96..4c6b73f7 100644 --- a/crates/bough-core/src/language/javascript.rs +++ b/crates/bough-core/src/language/javascript.rs @@ -34,7 +34,7 @@ impl LanguageDriver for JavascriptDriver { "-" => BinaryOpMutationKind::Sub, _ => return None, }; - Some((MutantKind::BinaryOp(kind), span_from_node(node))) + Some((MutantKind::BinaryOp(kind), span_from_node(&op_node))) } _ => None, }; diff --git a/crates/bough-core/src/mutant.rs b/crates/bough-core/src/mutant.rs index f7f4c63d..956e8860 100644 --- a/crates/bough-core/src/mutant.rs +++ b/crates/bough-core/src/mutant.rs @@ -669,6 +669,7 @@ mod tests { #[test] fn js_finds_add_binary_op() { let js = "const x = a + b;"; + // ^ byte 12..13 let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) @@ -679,12 +680,14 @@ mod tests { .filter(|m| matches!(m.kind(), MutantKind::BinaryOp(BinaryOpMutationKind::Add))) .collect(); assert_eq!(adds.len(), 1); + assert_eq!(*adds[0].span(), Span::new(Point::new(0, 12, 12), Point::new(0, 13, 13))); } // bough[verify mutant.twig-iter.find.js.binary.sub] #[test] fn js_finds_sub_binary_op() { let js = "const x = a - b;"; + // ^ byte 12..13 let (_dir, base) = make_js_base(js); let twig = Twig::new(PathBuf::from("src/a.js")).unwrap(); let mutants: Vec<_> = TwigMutantsIter::new(LanguageId::Javascript, &base, &twig) @@ -695,6 +698,7 @@ mod tests { .filter(|m| matches!(m.kind(), MutantKind::BinaryOp(BinaryOpMutationKind::Sub))) .collect(); assert_eq!(subs.len(), 1); + assert_eq!(*subs[0].span(), Span::new(Point::new(0, 12, 12), Point::new(0, 13, 13))); } // bough[verify mutant.twig-iter.find.js.binary.add] diff --git a/crates/bough-core/src/mutation.rs b/crates/bough-core/src/mutation.rs index 18e9f5a4..2bc9d023 100644 --- a/crates/bough-core/src/mutation.rs +++ b/crates/bough-core/src/mutation.rs @@ -57,6 +57,12 @@ impl Mutation { pub fn subst(&self) -> &str { &self.subst } + + pub fn apply_to_complete_src_string(&self, src: &str) -> String { + let start = self.mutant.span().start().byte(); + let end = self.mutant.span().end().byte(); + format!("{}{}{}", &src[..start], self.subst, &src[end..]) + } } #[cfg(test)] @@ -359,4 +365,60 @@ mod tests { .unwrap(); (dir, base) } + + #[test] + fn apply_to_complete_src_string_replaces_binary_op() { + let src = "return a + b;"; + // ^ byte 9..10 + let mutant = Mutant::new( + crate::LanguageId::Javascript, + Twig::new(PathBuf::from("src/a.js")).unwrap(), + MutantKind::BinaryOp(BinaryOpMutationKind::Add), + Span::new(Point::new(0, 9, 9), Point::new(0, 10, 10)), + ); + let mutation = Mutation { mutant, subst: "-".into() }; + assert_eq!(mutation.apply_to_complete_src_string(src), "return a - b;"); + } + + #[test] + fn apply_to_complete_src_string_multiline() { + let src = "function f() {\n return a + b;\n}"; + // ^ byte 28..29 + let mutant = Mutant::new( + crate::LanguageId::Javascript, + Twig::new(PathBuf::from("src/a.js")).unwrap(), + MutantKind::BinaryOp(BinaryOpMutationKind::Add), + Span::new(Point::new(1, 13, 28), Point::new(1, 14, 29)), + ); + let mutation = Mutation { mutant, subst: "*".into() }; + assert_eq!(mutation.apply_to_complete_src_string(src), "function f() {\n return a * b;\n}"); + } + + #[test] + fn apply_to_complete_src_string_different_length_subst() { + let src = "if (x) { foo(); }"; + // ^^^^^^^^^^ bytes 7..17 + let mutant = Mutant::new( + crate::LanguageId::Javascript, + Twig::new(PathBuf::from("src/a.js")).unwrap(), + MutantKind::StatementBlock, + Span::new(Point::new(0, 7, 7), Point::new(0, 17, 17)), + ); + let mutation = Mutation { mutant, subst: "{}".into() }; + assert_eq!(mutation.apply_to_complete_src_string(src), "if (x) {}"); + } + + #[test] + fn apply_to_complete_src_string_condition() { + let src = "if (a > 0) { return 1; }"; + // ^^^^^^^ bytes 3..10 is "(a > 0)" + let mutant = Mutant::new( + crate::LanguageId::Javascript, + Twig::new(PathBuf::from("src/a.js")).unwrap(), + MutantKind::Condition, + Span::new(Point::new(0, 3, 3), Point::new(0, 10, 10)), + ); + let mutation = Mutation { mutant, subst: "true".into() }; + assert_eq!(mutation.apply_to_complete_src_string(src), "if true { return 1; }"); + } }