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 PathBuf Arbitrary impl with tests #368

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 60 additions & 3 deletions proptest/src/arbitrary/_std/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,72 @@

//! Arbitrary implementations for `std::path`.

use std::path::*;
use std::{ffi::OsString, path::*};

// TODO: Figure out PathBuf and then Box/Rc/Box<Path>.
use crate::{
arbitrary::{SMapped, StrategyFor},
prelude::{any, any_with, Arbitrary, Strategy},
std_facade::{string::ToString, Arc, Box, Rc, Vec},
strategy::{statics::static_map, MapInto},
};

arbitrary!(StripPrefixError; Path::new("").strip_prefix("a").unwrap_err());

/// This implementation takes two parameters: a range for the number of components, and a regex to
/// determine the string. It generates either a relative or an absolute path with equal probability.
///
/// Currently, this implementation does not generate:
///
/// * Paths that are not valid UTF-8 (this is unlikely to change)
/// * Paths with a [`PrefixComponent`](std::path::PrefixComponent) on Windows, e.g. `C:\` (this may
/// change in the future)
impl Arbitrary for PathBuf {
type Parameters = <Vec<OsString> as Arbitrary>::Parameters;
type Strategy = SMapped<(bool, Vec<OsString>), Self>;

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
static_map(
(any::<bool>(), any_with::<Vec<OsString>>(args)),
Copy link
Member

Choose a reason for hiding this comment

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

i'm not familiar with the OsString arbitrary impl but I'm wondering if it can generate a string with a delimiter in it. will pull this locally and mess around more. otherwise this looks good.

Copy link
Member

Choose a reason for hiding this comment

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

OsString strategy does indeed generate strings with the delimiter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's okay, the number of delimiters is not a hard and fast rule here.

I'm more concerned about the number of components being too large by default.

I'm wondering if we should define a PathParameters struct with a custom default value for the number of delimiters.

Copy link
Member

Choose a reason for hiding this comment

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

I did notice that quite a few components were getting generated. I think a PathParameters struct with a default number and ensuring the output has that number (which may involve trimming any excess generated) could make sense. I think it'd be an un-intuitive if a user asks for n segments and we give them n + m.

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 makes sense. You're welcome to work on it, or I'll get around to it at some point later this week (speaking at RustConf in 3 days 😅)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matthew-russo OK, done. Let me know if this makes sense.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry work and personal life have kept me quite busy so I missed your last message. Thanks for getting back to this and congrats on the talk!

|(is_absolute, parts)| {
let mut out = PathBuf::new();
if is_absolute {
out.push(&MAIN_SEPARATOR.to_string());
}
for part in parts {
out.push(&part);
}
out
},
)
}
}

macro_rules! dst_wrapped {
($($w: ident),*) => {
$(
/// This implementation is identical to [the `Arbitrary` implementation for
/// `PathBuf`](trait.Arbitrary.html#impl-Arbitrary-for-PathBuf).
impl Arbitrary for $w<Path> {
type Parameters = <Vec<OsString> as Arbitrary>::Parameters;
type Strategy = MapInto<StrategyFor<PathBuf>, Self>;

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
any_with::<PathBuf>(args).prop_map_into()
}
}
)*
}
}

dst_wrapped!(Box, Rc, Arc);

#[cfg(test)]
mod test {
no_panic_test!(
strip_prefix_error => StripPrefixError
strip_prefix_error => StripPrefixError,
path_buf => PathBuf,
box_path => Box<Path>,
rc_path => Rc<Path>,
arc_path => Arc<Path>
);
}