proptest/path.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
//! Strategies for generating [`PathBuf`] and related path types.
//!
//! [`PathParams`] in this module is used as the argument to the
//! [`Arbitrary`](crate::arbitrary::Arbitrary) implementation for [`PathBuf`].
use crate::{collection::SizeRange, string::StringParam};
/// Parameters for the [`Arbitrary`] implementation for [`PathBuf`].
///
/// By default, this generates paths with 0 to 8 components uniformly at random, each of which is a
/// default [`StringParam`].
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PathParams {
/// The number of components in the path.
components: SizeRange,
/// The regular expression to generate individual components.
component_regex: StringParam,
}
impl PathParams {
/// Gets the number of components in the path.
pub fn components(&self) -> SizeRange {
self.components.clone()
}
/// Sets the number of components in the path.
pub fn with_components(mut self, components: impl Into<SizeRange>) -> Self {
self.components = components.into();
self
}
/// Gets the regular expression to generate individual components.
pub fn component_regex(&self) -> StringParam {
self.component_regex
}
/// Sets the regular expression to generate individual components.
pub fn with_component_regex(
mut self,
component_regex: impl Into<StringParam>,
) -> Self {
self.component_regex = component_regex.into();
self
}
}
impl Default for PathParams {
fn default() -> Self {
Self {
components: (0..8).into(),
// This is the default regex for `any::<String>()`.
component_regex: StringParam::default(),
}
}
}