use fallow_output::{MutationReplayOutcome, MutationReplaySide, MutationReplayVerdict};
use crate::mutation::protocol::{ReplayCall, ReplayOutcome, ReplaySide};
const TAG: &str = "$replay";
fn sort_members(map: &mut serde_json::Map<String, serde_json::Value>, field: &str) {
if let Some(serde_json::Value::Array(items)) = map.get_mut(field) {
items.sort_by_cached_key(serde_json::Value::to_string);
}
}
pub fn canonical(value: &serde_json::Value) -> String {
fn sorted(value: &serde_json::Value) -> serde_json::Value {
match value {
serde_json::Value::Object(map) => {
let mut entries: Vec<_> = map.iter().collect();
entries.sort_by(|left, right| left.0.cmp(right.0));
let mut object: serde_json::Map<String, serde_json::Value> = entries
.into_iter()
.map(|(key, item)| (key.clone(), sorted(item)))
.collect();
match object.get(TAG).and_then(serde_json::Value::as_str) {
Some("set") => sort_members(&mut object, "values"),
Some("map") => sort_members(&mut object, "entries"),
_ => {}
}
serde_json::Value::Object(object)
}
serde_json::Value::Array(items) => {
serde_json::Value::Array(items.iter().map(sorted).collect())
}
other => other.clone(),
}
}
sorted(value).to_string()
}
fn key(call: &ReplayCall) -> (ReplayOutcome, String) {
(call.outcome, canonical(&call.value))
}
fn stable_key(calls: &[ReplayCall], side: ReplaySide) -> Option<(ReplayOutcome, String)> {
let mut keys = calls.iter().filter(|call| call.side == side).map(key);
let first = keys.next()?;
keys.all(|other| other == first).then_some(first)
}
pub fn verdict(calls: &[ReplayCall]) -> MutationReplayVerdict {
if calls
.iter()
.any(|call| call.outcome == ReplayOutcome::NotCallable)
{
return MutationReplayVerdict::NotCallable;
}
if calls
.iter()
.any(|call| call.outcome == ReplayOutcome::TimedOut)
{
return MutationReplayVerdict::Timeout;
}
match (
stable_key(calls, ReplaySide::Original),
stable_key(calls, ReplaySide::Mutant),
) {
(Some(original), Some(mutant)) if original != mutant => MutationReplayVerdict::Witness,
(Some(_), Some(_)) => MutationReplayVerdict::NoWitnessFound,
_ => MutationReplayVerdict::Unstable,
}
}
pub fn check_calls(calls: &[ReplayCall], calls_per_side: u32) -> Result<(), String> {
let expected = (1..=calls_per_side)
.flat_map(|index| [(ReplaySide::Original, index), (ReplaySide::Mutant, index)]);
let actual: Vec<_> = calls.iter().map(|call| (call.side, call.index)).collect();
if actual.iter().copied().eq(expected) {
Ok(())
} else {
Err(format!(
"the replay result has {} calls out of the contract order; expected {calls_per_side} calls of each side in turns",
calls.len()
))
}
}
pub const fn output_side(side: ReplaySide) -> MutationReplaySide {
match side {
ReplaySide::Original => MutationReplaySide::Original,
ReplaySide::Mutant => MutationReplaySide::Mutant,
}
}
pub const fn output_outcome(outcome: ReplayOutcome) -> MutationReplayOutcome {
match outcome {
ReplayOutcome::Returned => MutationReplayOutcome::Returned,
ReplayOutcome::Threw => MutationReplayOutcome::Threw,
ReplayOutcome::TimedOut => MutationReplayOutcome::TimedOut,
ReplayOutcome::NotCallable => MutationReplayOutcome::NotCallable,
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn call(
side: ReplaySide,
index: u32,
outcome: ReplayOutcome,
value: serde_json::Value,
) -> ReplayCall {
ReplayCall {
side,
index,
outcome,
value,
text: String::new(),
}
}
fn turns(original: [serde_json::Value; 3], mutant: [serde_json::Value; 3]) -> Vec<ReplayCall> {
original
.into_iter()
.zip(mutant)
.enumerate()
.flat_map(|(index, (left, right))| {
let index = u32::try_from(index).unwrap_or(0) + 1;
[
call(ReplaySide::Original, index, ReplayOutcome::Returned, left),
call(ReplaySide::Mutant, index, ReplayOutcome::Returned, right),
]
})
.collect()
}
#[test]
fn stable_sides_that_differ_give_a_witness() {
let calls = turns(
[json!(5), json!(5), json!(5)],
[json!(-1), json!(-1), json!(-1)],
);
assert_eq!(verdict(&calls), MutationReplayVerdict::Witness);
}
#[test]
fn equal_sides_give_no_witness_found_and_never_equivalent() {
let calls = turns(
[json!(5), json!(5), json!(5)],
[json!(5), json!(5), json!(5)],
);
assert_eq!(verdict(&calls), MutationReplayVerdict::NoWitnessFound);
}
#[test]
fn a_side_that_changes_between_calls_is_unstable_even_when_the_sides_differ() {
let calls = turns(
[json!(1), json!(2), json!(3)],
[json!(9), json!(9), json!(9)],
);
assert_eq!(verdict(&calls), MutationReplayVerdict::Unstable);
let mutant = turns(
[json!(1), json!(1), json!(1)],
[json!(9), json!(8), json!(9)],
);
assert_eq!(verdict(&mutant), MutationReplayVerdict::Unstable);
}
#[test]
fn a_thrown_error_differs_from_a_returned_value_with_the_same_text() {
let mut calls = turns(
[json!(1), json!(1), json!(1)],
[json!(1), json!(1), json!(1)],
);
for call in calls
.iter_mut()
.filter(|call| call.side == ReplaySide::Mutant)
{
call.outcome = ReplayOutcome::Threw;
}
assert_eq!(verdict(&calls), MutationReplayVerdict::Witness);
}
#[test]
fn the_key_order_of_an_object_is_no_difference() {
let calls = turns(
[
json!({"a": 1, "b": 2}),
json!({"a": 1, "b": 2}),
json!({"a": 1, "b": 2}),
],
[
json!({"b": 2, "a": 1}),
json!({"b": 2, "a": 1}),
json!({"b": 2, "a": 1}),
],
);
assert_eq!(verdict(&calls), MutationReplayVerdict::NoWitnessFound);
}
#[test]
fn the_order_of_a_set_or_a_map_is_no_difference() {
let set = |values: serde_json::Value| json!({"$replay": "set", "values": values});
let map = |entries: serde_json::Value| json!({"$replay": "map", "entries": entries});
let calls = turns(
[set(json!([1, 2])), set(json!([1, 2])), set(json!([1, 2]))],
[set(json!([2, 1])), set(json!([2, 1])), set(json!([2, 1]))],
);
assert_eq!(verdict(&calls), MutationReplayVerdict::NoWitnessFound);
let calls = turns(
[
map(json!([["a", 1], ["b", 2]])),
map(json!([["a", 1], ["b", 2]])),
map(json!([["a", 1], ["b", 2]])),
],
[
map(json!([["b", 2], ["a", 1]])),
map(json!([["b", 2], ["a", 1]])),
map(json!([["b", 2], ["a", 1]])),
],
);
assert_eq!(verdict(&calls), MutationReplayVerdict::NoWitnessFound);
let other = turns(
[set(json!([1, 2])), set(json!([1, 2])), set(json!([1, 2]))],
[set(json!([1, 3])), set(json!([1, 3])), set(json!([1, 3]))],
);
assert_eq!(verdict(&other), MutationReplayVerdict::Witness);
}
#[test]
fn a_timeout_or_a_missing_function_decides_before_the_values() {
let mut calls = turns(
[json!(1), json!(1), json!(1)],
[json!(2), json!(2), json!(2)],
);
calls[3].outcome = ReplayOutcome::TimedOut;
assert_eq!(verdict(&calls), MutationReplayVerdict::Timeout);
calls[0].outcome = ReplayOutcome::NotCallable;
assert_eq!(verdict(&calls), MutationReplayVerdict::NotCallable);
}
#[test]
fn the_call_order_must_follow_the_contract() {
let calls = turns(
[json!(1), json!(1), json!(1)],
[json!(2), json!(2), json!(2)],
);
assert!(check_calls(&calls, 3).is_ok());
assert!(check_calls(&calls, 2).is_err());
let mut swapped = calls.clone();
swapped.swap(0, 1);
assert!(check_calls(&swapped, 3).is_err());
assert!(check_calls(&calls[..5], 3).is_err());
}
}