From 05d48555d402f1b30658fd6f566ea21582f0eacd Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Tue, 10 Mar 2026 01:40:48 +0000 Subject: [PATCH] Add mutations for logical and comparison operators in JS/TS --- crates/bough-core/src/language/javascript.rs | 115 +++++++++++++++++++ crates/bough-core/src/language/typescript.rs | 115 +++++++++++++++++++ crates/bough-core/src/mutant.rs | 17 ++- 3 files changed, 244 insertions(+), 3 deletions(-) diff --git a/crates/bough-core/src/language/javascript.rs b/crates/bough-core/src/language/javascript.rs index 3e3afe96..74227fc2 100644 --- a/crates/bough-core/src/language/javascript.rs +++ b/crates/bough-core/src/language/javascript.rs @@ -45,6 +45,15 @@ impl LanguageDriver for JavascriptDriver { "^" => BinaryOpMutationKind::BitXor, "<<" => BinaryOpMutationKind::Shl, ">>" => BinaryOpMutationKind::Shr, + "&&" => BinaryOpMutationKind::And, + "||" => BinaryOpMutationKind::Or, + + "==" => BinaryOpMutationKind::Eq, + "===" => BinaryOpMutationKind::StrictEq, + ">" => BinaryOpMutationKind::Gt, + ">=" => BinaryOpMutationKind::Gte, + "<" => BinaryOpMutationKind::Lt, + "<=" => BinaryOpMutationKind::Lte, _ => return None, }; Some((MutantKind::BinaryOp(kind), span_from_node(&op_node))) @@ -63,6 +72,8 @@ impl LanguageDriver for JavascriptDriver { "^=" => AssignMutationKind::BitXorAssign, "<<=" => AssignMutationKind::ShlAssign, ">>=" => AssignMutationKind::ShrAssign, + "&&=" => AssignMutationKind::AndAssign, + "||=" => AssignMutationKind::OrAssign, _ => return None, }; Some((MutantKind::Assign(kind), span_from_node(&op_node))) @@ -109,6 +120,15 @@ impl LanguageDriver for JavascriptDriver { MutantKind::BinaryOp(BinaryOpMutationKind::BitXor) => vec!["&".into(), "|".into()], MutantKind::BinaryOp(BinaryOpMutationKind::Shl) => vec![">>".into()], MutantKind::BinaryOp(BinaryOpMutationKind::Shr) => vec!["<<".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::And) => vec!["||".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Or) => vec!["&&".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Xor) => vec![], + MutantKind::BinaryOp(BinaryOpMutationKind::Eq) => vec!["!=".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::StrictEq) => vec!["!==".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Gt) => vec!["<=".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Gte) => vec!["<".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Lt) => vec![">=".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Lte) => vec![">".into()], // bough[impl mutation.subst.js.statement] MutantKind::StatementBlock => vec!["{}".into()], // bough[impl mutation.subst.js.cond.true] @@ -137,6 +157,12 @@ impl LanguageDriver for JavascriptDriver { MutantKind::Assign(AssignMutationKind::ShrAssign) => { vec!["<<=".into(), "=".into()] } + MutantKind::Assign(AssignMutationKind::AndAssign) => { + vec!["||=".into(), "=".into()] + } + MutantKind::Assign(AssignMutationKind::OrAssign) => { + vec!["&&=".into(), "=".into()] + } } } } @@ -378,4 +404,93 @@ mod tests { assert!(mutations.contains(&"x <<= 1".to_string())); assert!(mutations.contains(&"x = 1".to_string())); } + + #[test] + fn make_bin_op_and() { + let src = "const x = a && b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a || b".to_string())); + } + + #[test] + fn make_bin_op_or() { + let src = "const x = a || b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a && b".to_string())); + } + + #[test] + fn make_bin_op_xor() { + let src = "const x = a ?? b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 0); + } + + #[test] + fn make_bin_op_eq() { + let src = "const x = a == b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a != b".to_string())); + } + + #[test] + fn make_bin_op_strict_eq() { + let src = "const x = a === b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a !== b".to_string())); + } + + #[test] + fn make_bin_op_gt() { + let src = "const x = a > b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a <= b".to_string())); + } + + #[test] + fn make_bin_op_gte() { + let src = "const x = a >= b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a < b".to_string())); + } + + #[test] + fn make_bin_op_lt() { + let src = "const x = a < b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a >= b".to_string())); + } + + #[test] + fn make_bin_op_lte() { + let src = "const x = a <= b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a > b".to_string())); + } + + #[test] + fn make_and_assign() { + let src = "x &&= 1"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 2); + assert!(mutations.contains(&"x ||= 1".to_string())); + assert!(mutations.contains(&"x = 1".to_string())); + } + + #[test] + fn make_or_assign() { + let src = "x ||= 1"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 2); + assert!(mutations.contains(&"x &&= 1".to_string())); + assert!(mutations.contains(&"x = 1".to_string())); + } } diff --git a/crates/bough-core/src/language/typescript.rs b/crates/bough-core/src/language/typescript.rs index ff33b454..0ede3f93 100644 --- a/crates/bough-core/src/language/typescript.rs +++ b/crates/bough-core/src/language/typescript.rs @@ -39,6 +39,15 @@ impl LanguageDriver for TypescriptDriver { "^" => BinaryOpMutationKind::BitXor, "<<" => BinaryOpMutationKind::Shl, ">>" => BinaryOpMutationKind::Shr, + "&&" => BinaryOpMutationKind::And, + "||" => BinaryOpMutationKind::Or, + + "==" => BinaryOpMutationKind::Eq, + "===" => BinaryOpMutationKind::StrictEq, + ">" => BinaryOpMutationKind::Gt, + ">=" => BinaryOpMutationKind::Gte, + "<" => BinaryOpMutationKind::Lt, + "<=" => BinaryOpMutationKind::Lte, _ => return None, }; Some((MutantKind::BinaryOp(kind), span_from_node(&op_node))) @@ -57,6 +66,8 @@ impl LanguageDriver for TypescriptDriver { "^=" => AssignMutationKind::BitXorAssign, "<<=" => AssignMutationKind::ShlAssign, ">>=" => AssignMutationKind::ShrAssign, + "&&=" => AssignMutationKind::AndAssign, + "||=" => AssignMutationKind::OrAssign, _ => return None, }; Some((MutantKind::Assign(kind), span_from_node(&op_node))) @@ -101,6 +112,15 @@ impl LanguageDriver for TypescriptDriver { MutantKind::BinaryOp(BinaryOpMutationKind::BitXor) => vec!["&".into(), "|".into()], MutantKind::BinaryOp(BinaryOpMutationKind::Shl) => vec![">>".into()], MutantKind::BinaryOp(BinaryOpMutationKind::Shr) => vec!["<<".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::And) => vec!["||".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Or) => vec!["&&".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Xor) => vec![], + MutantKind::BinaryOp(BinaryOpMutationKind::Eq) => vec!["!=".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::StrictEq) => vec!["!==".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Gt) => vec!["<=".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Gte) => vec!["<".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Lt) => vec![">=".into()], + MutantKind::BinaryOp(BinaryOpMutationKind::Lte) => vec![">".into()], MutantKind::StatementBlock => vec!["{}".into()], MutantKind::Condition => vec!["true".into(), "false".into()], MutantKind::Assign(AssignMutationKind::NormalAssign) => { @@ -126,6 +146,12 @@ impl LanguageDriver for TypescriptDriver { MutantKind::Assign(AssignMutationKind::ShrAssign) => { vec!["<<=".into(), "=".into()] } + MutantKind::Assign(AssignMutationKind::AndAssign) => { + vec!["||=".into(), "=".into()] + } + MutantKind::Assign(AssignMutationKind::OrAssign) => { + vec!["&&=".into(), "=".into()] + } } } } @@ -367,4 +393,93 @@ mod tests { assert!(mutations.contains(&"x <<= 1".to_string())); assert!(mutations.contains(&"x = 1".to_string())); } + + #[test] + fn make_bin_op_and() { + let src = "const x = a && b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a || b".to_string())); + } + + #[test] + fn make_bin_op_or() { + let src = "const x = a || b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a && b".to_string())); + } + + #[test] + fn make_bin_op_xor() { + let src = "const x = a ?? b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 0); + } + + #[test] + fn make_bin_op_eq() { + let src = "const x = a == b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a != b".to_string())); + } + + #[test] + fn make_bin_op_strict_eq() { + let src = "const x = a === b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a !== b".to_string())); + } + + #[test] + fn make_bin_op_gt() { + let src = "const x = a > b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a <= b".to_string())); + } + + #[test] + fn make_bin_op_gte() { + let src = "const x = a >= b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a < b".to_string())); + } + + #[test] + fn make_bin_op_lt() { + let src = "const x = a < b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a >= b".to_string())); + } + + #[test] + fn make_bin_op_lte() { + let src = "const x = a <= b"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 1); + assert!(mutations.contains(&"const x = a > b".to_string())); + } + + #[test] + fn make_and_assign() { + let src = "x &&= 1"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 2); + assert!(mutations.contains(&"x ||= 1".to_string())); + assert!(mutations.contains(&"x = 1".to_string())); + } + + #[test] + fn make_or_assign() { + let src = "x ||= 1"; + let mutations = all_mutations(src); + assert_eq!(mutations.len(), 2); + assert!(mutations.contains(&"x &&= 1".to_string())); + assert!(mutations.contains(&"x = 1".to_string())); + } } diff --git a/crates/bough-core/src/mutant.rs b/crates/bough-core/src/mutant.rs index 13144566..be6a300d 100644 --- a/crates/bough-core/src/mutant.rs +++ b/crates/bough-core/src/mutant.rs @@ -273,27 +273,40 @@ impl Point { #[repr(u8)] pub enum BinaryOpMutationKind { Add, + And, BitAnd, BitOr, BitXor, Div, + Eq, + Gt, + Gte, + Lt, + Lte, Mul, + Or, Rem, Shl, Shr, + StrictEq, + Ne, + StrictNe, Sub, + Xor, } #[derive(Debug, Clone, PartialEq, Eq, bough_typed_hash::HashInto, Hash, facet::Facet)] #[repr(u8)] pub enum AssignMutationKind { - NormalAssign, AddAssign, + AndAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, MulAssign, + NormalAssign, + OrAssign, RemAssign, ShlAssign, ShrAssign, @@ -746,8 +759,6 @@ mod tests { ); } - - fn hash_mutant(mutant: &Mutant) -> [u8; 32] { use bough_typed_hash::sha2::Digest; let mut state = bough_typed_hash::ShaState::new(); -- knotmirror 0.0.1