From 1f80ef972e2c1e2748a04f5ef42900ad1bb23bed Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Wed, 18 Mar 2026 20:11:26 +0000 Subject: [PATCH] py: bin-op-add --- .../bin-op-add/71223f603c5a.mutation.json | 36 +++++++++++ corpus/python/bin-op-add/71223f603c5a.py | 1 + .../bin-op-add/f8f54bbf6cf2.mutation.json | 36 +++++++++++ corpus/python/bin-op-add/f8f54bbf6cf2.py | 1 + crates/bough-core/src/language/python.rs | 63 +++++++++++++++++-- crates/bough-corpus-tests/build.rs | 2 + 6 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 corpus/python/bin-op-add/71223f603c5a.mutation.json create mode 100644 corpus/python/bin-op-add/71223f603c5a.py create mode 100644 corpus/python/bin-op-add/f8f54bbf6cf2.mutation.json create mode 100644 corpus/python/bin-op-add/f8f54bbf6cf2.py diff --git a/corpus/python/bin-op-add/71223f603c5a.mutation.json b/corpus/python/bin-op-add/71223f603c5a.mutation.json new file mode 100644 index 00000000..1acde10b --- /dev/null +++ b/corpus/python/bin-op-add/71223f603c5a.mutation.json @@ -0,0 +1,36 @@ +{ + "mutant": { + "lang": "py", + "twig": [ + "src/a.py" + ], + "kind": { + "BinaryOp": "Add" + }, + "subst_span": { + "start": { + "line": 0, + "col": 6, + "byte": 6 + }, + "end": { + "line": 0, + "col": 7, + "byte": 7 + } + }, + "effect_span": { + "start": { + "line": 0, + "col": 4, + "byte": 4 + }, + "end": { + "line": 0, + "col": 9, + "byte": 9 + } + } + }, + "subst": "-" +} \ No newline at end of file diff --git a/corpus/python/bin-op-add/71223f603c5a.py b/corpus/python/bin-op-add/71223f603c5a.py new file mode 100644 index 00000000..e75a8be3 --- /dev/null +++ b/corpus/python/bin-op-add/71223f603c5a.py @@ -0,0 +1 @@ +x = 1 - 2 diff --git a/corpus/python/bin-op-add/f8f54bbf6cf2.mutation.json b/corpus/python/bin-op-add/f8f54bbf6cf2.mutation.json new file mode 100644 index 00000000..f50df055 --- /dev/null +++ b/corpus/python/bin-op-add/f8f54bbf6cf2.mutation.json @@ -0,0 +1,36 @@ +{ + "mutant": { + "lang": "py", + "twig": [ + "src/a.py" + ], + "kind": { + "BinaryOp": "Add" + }, + "subst_span": { + "start": { + "line": 0, + "col": 6, + "byte": 6 + }, + "end": { + "line": 0, + "col": 7, + "byte": 7 + } + }, + "effect_span": { + "start": { + "line": 0, + "col": 4, + "byte": 4 + }, + "end": { + "line": 0, + "col": 9, + "byte": 9 + } + } + }, + "subst": "*" +} \ No newline at end of file diff --git a/corpus/python/bin-op-add/f8f54bbf6cf2.py b/corpus/python/bin-op-add/f8f54bbf6cf2.py new file mode 100644 index 00000000..9c1e0b79 --- /dev/null +++ b/corpus/python/bin-op-add/f8f54bbf6cf2.py @@ -0,0 +1 @@ +x = 1 * 2 diff --git a/crates/bough-core/src/language/python.rs b/crates/bough-core/src/language/python.rs index 06478b61..d58d8ddb 100644 --- a/crates/bough-core/src/language/python.rs +++ b/crates/bough-core/src/language/python.rs @@ -1,5 +1,6 @@ use super::LanguageDriver; -use crate::mutant::{MutantKind, Span}; +use crate::mutant::{BinaryOpMutationKind, MutantKind, Span, span_from_node}; +use tracing::trace; pub(crate) struct PythonDriver; @@ -10,17 +11,67 @@ impl LanguageDriver for PythonDriver { fn check_node( &self, - _node: &arborium_tree_sitter::Node<'_>, - _file_content: &[u8], + node: &arborium_tree_sitter::Node<'_>, + file_content: &[u8], ) -> Option<(MutantKind, Span, Span)> { - None + let result = match node.kind() { + "binary_operator" => { + let op_node = node.child_by_field_name("operator")?; + let op_text = op_node.utf8_text(file_content).ok()?; + let kind = match op_text { + "+" => BinaryOpMutationKind::Add, + _ => return None, + }; + Some((MutantKind::BinaryOp(kind), span_from_node(&op_node), span_from_node(node))) + } + _ => None, + }; + if let Some((ref kind, ref span, _)) = result { + trace!(node_kind = node.kind(), mutant_kind = ?kind, start_byte = span.start().byte(), "py: matched node"); + } + result } - fn substitutions(&self, _kind: &MutantKind) -> Vec { - vec![] + fn substitutions(&self, kind: &MutantKind) -> Vec { + match kind { + MutantKind::BinaryOp(BinaryOpMutationKind::Add) => vec!["-".into(), "*".into()], + _ => vec![], + } } fn is_context_boundary(&self, _node: &arborium_tree_sitter::Node<'_>) -> bool { false } } + +#[cfg(test)] +mod tests { + fn dump_tree(src: &str) { + let lang: arborium_tree_sitter::Language = arborium_python::language().into(); + let mut parser = arborium_tree_sitter::Parser::new(); + parser.set_language(&lang).unwrap(); + let tree = parser.parse(src.as_bytes(), None).unwrap(); + fn print_node(node: &arborium_tree_sitter::Node, src: &[u8], indent: usize) { + let text = node.utf8_text(src).unwrap_or(""); + let field = node.parent().and_then(|p| { + (0..p.child_count()) + .find(|&i| p.child(i as u32).map(|c| c.id() == node.id()).unwrap_or(false)) + .and_then(|i| p.field_name_for_child(i as u32)) + }); + let field_str = field.map(|f| format!("{f}: ")).unwrap_or_default(); + eprintln!("{:indent$}{field_str}{} [{}-{}] {text:?}", "", node.kind(), node.start_byte(), node.end_byte(), indent=indent); + for i in 0..node.child_count() { + if let Some(child) = node.child(i as u32) { + print_node(&child, src, indent + 2); + } + } + } + print_node(&tree.root_node(), src.as_bytes(), 0); + } + + #[test] + #[ignore] + fn debug_tree() { + dump_tree("x = 1 + 2"); + } +} diff --git a/crates/bough-corpus-tests/build.rs b/crates/bough-corpus-tests/build.rs index 3f7dd6d3..9ca36b1e 100644 --- a/crates/bough-corpus-tests/build.rs +++ b/crates/bough-corpus-tests/build.rs @@ -32,12 +32,14 @@ fn main() { let lang_id = match lang_str { "javascript" => "bough_core::LanguageId::Javascript", "typescript" => "bough_core::LanguageId::Typescript", + "python" => "bough_core::LanguageId::Python", _ => continue, }; let ext = match lang_str { "javascript" => "js", "typescript" => "ts", + "python" => "py", _ => continue, }; -- knotmirror 0.0.1