Skip to content

Commit

Permalink
+ (optional, feature-gated) proptest::Arbitrary implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcronce committed Apr 15, 2022
1 parent 8dcebd4 commit 4f641eb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ exclude = [".cargo/**/*", ".github/**/*"]
all-features = true

[dependencies]
proptest = { version = "1.0.0", optional = true }
serde = { version = "1", features = ["derive"], optional = true }

[features]
serde1 = ["serde"]
proptest = ["dep:proptest"]
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ use std::{
sync::Arc,
};

#[cfg(feature = "proptest")]
mod proptest_impls;
#[cfg(feature = "serde1")]
mod serde_impls;
#[cfg(test)]
Expand Down
28 changes: 28 additions & 0 deletions src/proptest_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) The camino Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! [proptest::Arbitrary](Arbitrary) implementation for `Utf8PathBuf` and `Box<Utf8Path>`. Note
//! that implementions for `Rc<Utc8Path>` and `Arc<Utf8Path>` are not currently possible due to
//! orphan rules - this crate doesn't define `Rc`/`Arc` nor `Arbitrary`, so it can't define those
//! implementations.

use proptest::arbitrary::{any_with, Arbitrary, StrategyFor};
use proptest::strategy::{MapInto, Strategy};

use crate::{Utf8Path, Utf8PathBuf};

impl Arbitrary for Utf8PathBuf {
type Parameters = <String as Arbitrary>::Parameters;
type Strategy = MapInto<StrategyFor<String>, Self>;
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
any_with::<String>(args).prop_map_into()
}
}

impl Arbitrary for Box<Utf8Path> {
type Parameters = <Utf8PathBuf as Arbitrary>::Parameters;
type Strategy = MapInto<StrategyFor<Utf8PathBuf>, Self>;
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
any_with::<Utf8PathBuf>(args).prop_map_into()
}
}

0 comments on commit 4f641eb

Please sign in to comment.