Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (optional, feature-gated) proptest::Arbitrary implementation #18

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]
proptest1 = ["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 = "proptest1")]
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()
}
}
Comment on lines +14 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like a very representative generation strategy since most strings won't have slashes in them -- seems like it should instead produce path components and then join them according to the platform's rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's something I would generally leave to individual library/application testers to provide, rather than assuming their data for them. It's also something that could be added on here in the future as a default if there's demand for it. These are essentially copies of the OsString and Box<OsStr> impls; anything further is outside my skillset as far as implementing Arbitrary.