pub struct Session { /* private fields */ }
Expand description
A mutation session and its configuration.
This type allows you to configure things like setting the RNG seed, or whether to only perform shrinking mutations.
A session should be reused while a particular value, or set of values, are being repeatedly mutated.
§Example
use mutatis::Session;
// Create a new mutation session.
let mut session = Session::new()
// Configure the RNG seed, changing which random mutations are chosen.
.seed(0x12345678);
// Mutate a value a few times inside this session.
let mut x = 93;
for _ in 0..3 {
session.mutate(&mut x)?;
println!("mutated x is {x}");
}
// Example output:
//
// mutated x is -906367562
// mutated x is 766527557
// mutated x is 132130383
Implementations§
Source§impl Session
impl Session
Sourcepub fn shrink(self, shrink: bool) -> Self
pub fn shrink(self, shrink: bool) -> Self
Set whether to only perform shrinking mutations or not.
Defaults to false
.
Sourcepub fn mutate<T>(&mut self, value: &mut T) -> Result<()>where
T: DefaultMutate,
pub fn mutate<T>(&mut self, value: &mut T) -> Result<()>where
T: DefaultMutate,
Mutate the given value
with its default mutator and within the
constraints of this Session
’s configuration.
The default mutator for a type is defined by the DefaultMutate
trait
implementation for that type.
To use a custom mutator, rather than the default mutator, use the
mutate_with
method instead.
§Example
use mutatis::Session;
let mut x = Some(1234i32);
let mut session = Session::new().seed(0xaabbccdd);
for _ in 0..5 {
session.mutate(&mut x)?;
println!("mutated x is {x:?}");
}
// Example output:
//
// mutated x is None
// mutated x is Some(-688796504)
// mutated x is None
// mutated x is Some(-13390771)
// mutated x is Some(1208312368)
Sourcepub fn mutate_with<T>(
&mut self,
mutator: &mut impl Mutate<T>,
value: &mut T,
) -> Result<()>
pub fn mutate_with<T>( &mut self, mutator: &mut impl Mutate<T>, value: &mut T, ) -> Result<()>
Mutate the given value
with the given mutator
and within the
constraints of this Session
’s configuration.
This is similar to the mutate
method, but allows
you to specify a custom mutator to use instead of the default mutator
for value
’s type.
§Example
use mutatis::{mutators as m, Session};
let mut res = Ok(1234i32);
// Create a custom mutator for `Result<i32, bool>` values.
let mut mutator = m::result(m::range(-10..=10), m::just(true));
let mut session = Session::new().seed(0x1984);
for _ in 0..5 {
session.mutate_with(&mut mutator, &mut res)?;
println!("mutated res is {res:?}");
}
// Example output:
//
// mutated res is Err(true)
// mutated res is Err(true)
// mutated res is Ok(9)
// mutated res is Err(true)
// mutated res is Ok(-6)