forge/mutation/mutators/
mutator_registry.rs1use crate::mutation::mutant::Mutant;
2use eyre::Report;
3use foundry_config::MutatorType;
4
5use super::{
6 MutationContext, Mutator, assembly_mutator, assignment_mutator, binary_op_mutator,
7 delete_expression_mutator, elim_delegate_mutator, function_body_mutator, require_mutator,
8 unary_op_mutator,
9};
10
11pub struct MutatorRegistry {
13 mutators: Vec<Box<dyn Mutator>>,
14}
15
16pub struct MutationGenerationResult {
17 pub mutations: Vec<Mutant>,
18 pub errors: Vec<Report>,
19}
20
21impl MutatorRegistry {
22 pub fn from_enabled(enabled: &[MutatorType]) -> Self {
23 let mut registry = Self { mutators: Vec::new() };
24
25 for ty in enabled {
26 match ty {
27 MutatorType::Assembly => {
28 registry.mutators.push(Box::new(assembly_mutator::AssemblyMutator::new()));
29 }
30 MutatorType::Assignment => {
31 registry.mutators.push(Box::new(assignment_mutator::AssignmentMutator));
32 }
33 MutatorType::BinaryOp => {
34 registry.mutators.push(Box::new(binary_op_mutator::BinaryOpMutator));
35 }
36 MutatorType::DeleteExpression => {
37 registry
38 .mutators
39 .push(Box::new(delete_expression_mutator::DeleteExpressionMutator));
40 }
41 MutatorType::ElimDelegate => {
42 registry.mutators.push(Box::new(elim_delegate_mutator::ElimDelegateMutator));
43 }
44 MutatorType::FunctionBody => {
45 registry.mutators.push(Box::new(function_body_mutator::FunctionBodyMutator));
46 }
47 MutatorType::Require => {
48 registry.mutators.push(Box::new(require_mutator::RequireMutator));
49 }
50 MutatorType::UnaryOp => {
51 registry.mutators.push(Box::new(unary_op_mutator::UnaryOpMutator));
52 }
53 }
54 }
55
56 registry
57 }
58
59 #[cfg(test)]
60 pub fn new_with_mutators(mutators: Vec<Box<dyn Mutator>>) -> Self {
61 Self { mutators }
62 }
63
64 pub fn generate_mutations(&self, context: &MutationContext<'_>) -> MutationGenerationResult {
67 let mut mutations = Vec::new();
68 let mut errors = Vec::new();
69 for mutator in self.mutators.iter().filter(|mutator| mutator.is_applicable(context)) {
70 match mutator.generate_mutants(context) {
71 Ok(generated) => mutations.extend(generated),
72 Err(err) => errors.push(err),
73 }
74 }
75 MutationGenerationResult { mutations, errors }
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use eyre::{Result, eyre};
82 use solar::ast::Span;
83
84 use super::*;
85
86 struct FailingMutator;
87
88 impl Mutator for FailingMutator {
89 fn generate_mutants(&self, _ctxt: &MutationContext<'_>) -> Result<Vec<Mutant>> {
90 Err(eyre!("synthetic mutator failure"))
91 }
92
93 fn is_applicable(&self, _ctxt: &MutationContext<'_>) -> bool {
94 true
95 }
96 }
97
98 #[test]
99 fn generate_mutations_collects_mutator_errors() {
100 let registry = MutatorRegistry::new_with_mutators(vec![Box::new(FailingMutator)]);
101 let context = MutationContext::builder()
102 .with_path("test.sol".into())
103 .with_span(Span::DUMMY)
104 .build()
105 .unwrap();
106
107 let result = registry.generate_mutations(&context);
108 assert!(result.mutations.is_empty());
109 let err = result.errors.into_iter().next().unwrap();
110 assert!(err.to_string().contains("synthetic mutator failure"));
111 }
112}