Skip to content
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
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ rust-version = "1.70.0"
proc-macro = true

[dependencies]
syn = { version = "2.0.68", features = ["visit", "full", "extra-traits"] }
quote = "1.0.36"
proc-macro2 = "1.0.86"
syn = { version = "2.0.100", features = ["visit", "full", "extra-traits"] }
quote = "1.0.40"
proc-macro2 = "1.0.94"
structmeta = "0.3.0"

[dev-dependencies]
proptest = "1.6.0"
trybuild = "1.0.96"
tokio = { version = "1.38.0", features = ["rt-multi-thread"] }
trybuild = "1.0.104"
tokio = { version = "1.44.1", features = ["rt-multi-thread"] }
anyhow = "1.0.97"
googletest = "0.14.0"
42 changes: 39 additions & 3 deletions src/proptest_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,41 @@ use crate::syn_utils::{Arg, Args};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{
parse2, parse_quote, parse_str, spanned::Spanned, token, Block, Expr, Field, FieldMutability,
FnArg, Ident, ItemFn, LitStr, Pat, Result, ReturnType, Visibility,
parse2, parse_quote, parse_str, spanned::Spanned, token, Attribute, Block, Expr, Field,
FieldMutability, FnArg, Ident, ItemFn, LitStr, Pat, Result, ReturnType, Visibility,
};

// Check whether given attribute is a test attribute of forms:
// * `#[test]`
// * `#[core::prelude::*::test]` or `#[::core::prelude::*::test]`
// * `#[std::prelude::*::test]` or `#[::std::prelude::*::test]`
fn is_test_attribute(attr: &Attribute) -> bool {
let path = match &attr.meta {
syn::Meta::Path(path) => path,
_ => return false,
};
const CANDIDATES_LEN: usize = 4;

let candidates: [[&str; CANDIDATES_LEN]; 2] = [
["core", "prelude", "*", "test"],
["std", "prelude", "*", "test"],
];
if path.leading_colon.is_none()
&& path.segments.len() == 1
&& path.segments[0].arguments.is_none()
&& path.segments[0].ident == "test"
{
return true;
} else if path.segments.len() != candidates[0].len() {
return false;
}
candidates.into_iter().any(|segments| {
path.segments.iter().zip(segments).all(|(segment, path)| {
segment.arguments.is_none() && (path == "*" || segment.ident == path)
})
})
}

pub fn build_proptest(attr: TokenStream, mut item_fn: ItemFn) -> Result<TokenStream> {
let mut attr_args = None;
if !attr.is_empty() {
Expand Down Expand Up @@ -64,6 +95,12 @@ pub fn build_proptest(attr: TokenStream, mut item_fn: ItemFn) -> Result<TokenStr
};
item_fn.sig.inputs = parse_quote! { input: #args_type_ident };
item_fn.block = Box::new(parse2(block)?);
if !item_fn.attrs.iter().any(is_test_attribute) {
let test_attr: Attribute = parse_quote! {
#[::core::prelude::v1::test]
};
item_fn.attrs.push(test_attr);
}
let args_fields = args.iter().map(|arg| &arg.field);
let config = to_proptest_config(config_args);
let ts = quote! {
Expand All @@ -75,7 +112,6 @@ pub fn build_proptest(attr: TokenStream, mut item_fn: ItemFn) -> Result<TokenStr
#[cfg(test)]
proptest::proptest! {
#config
#[test]
#item_fn
}
};
Expand Down
12 changes: 12 additions & 0 deletions tests/proptest_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use proptest::{prelude::ProptestConfig, prop_assert};
use test_strategy::proptest;
use tokio::task::yield_now;

use googletest::expect_that;
use googletest::gtest;
use googletest::matchers::*;
use googletest::verify_that;

#[proptest]
fn example(_x: u32, #[strategy(1..10u32)] y: u32, #[strategy(0..#y)] z: u32) {
assert!(1 <= y);
Expand Down Expand Up @@ -291,3 +296,10 @@ async fn anyhow_result_bail_async(#[strategy(1..10u8)] x: u8) -> anyhow::Result<
yield_now().await;
anyhow::bail!("error");
}

#[proptest]
#[gtest]
fn googletest_result(#[strategy(1..10u8)] x: u8) -> googletest::Result<()> {
expect_that!(x, ge(1));
verify_that!(x, lt(10))
}