Skip to content

Commit

Permalink
tests: Use custom Arbitrary impl for Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
duesee committed Feb 17, 2024
1 parent 0984414 commit c364bf6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
41 changes: 38 additions & 3 deletions imap-types/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use arbitrary::{Arbitrary, Unstructured};
use chrono::{FixedOffset, TimeZone};

#[cfg(feature = "ext_sort_thread")]
use crate::extensions::sort::SortAlgorithm;
#[cfg(feature = "ext_sort_thread")]
use crate::extensions::thread::ThreadingAlgorithm;
use crate::extensions::{
sort::SortAlgorithm,
thread::{Thread, ThreadingAlgorithm},
};
use crate::{
auth::AuthMechanism,
body::{
Expand Down Expand Up @@ -471,6 +472,40 @@ impl<'a> Arbitrary<'a> for NaiveDate {
}
}

#[cfg(feature = "ext_sort_thread")]
impl<'a> Arbitrary<'a> for Thread {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
// We cheat a bit: Start from a leaf ...
let mut current = Thread::Members {
prefix: Arbitrary::arbitrary(u)?,
answers: None,
};

// ... and build up the thread to the top (with max depth == 8)
for _ in 0..7 {
match u.int_in_range(0..=2)? {
0 => {
current = Thread::Members {
prefix: Arbitrary::arbitrary(u)?,
answers: Some(Vec2::unvalidated(vec![current.clone(), current])),
};
}
1 => {
current = Thread::Nested {
answers: Vec2::unvalidated(vec![current.clone(), current]),
};
}
2 => {
return Ok(current);
}
_ => unreachable!(),
}
}

Ok(current)
}
}

#[cfg(test)]
mod tests {
use arbitrary::{Arbitrary, Error, Unstructured};
Expand Down
3 changes: 0 additions & 3 deletions imap-types/src/extensions/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ use std::{
num::NonZeroU32,
};

#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
#[cfg(feature = "bounded-static")]
use bounded_static::ToStatic;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::core::{Atom, Vec1, Vec2};

#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(feature = "bounded-static", derive(ToStatic))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down

0 comments on commit c364bf6

Please sign in to comment.