|
| 1 | +use super::*; |
| 2 | +use quote::quote_spanned; |
| 3 | + |
| 4 | +/// Generate the arbitrary impl for the struct |
| 5 | +pub(super) fn gen_arbitrary_impl( |
| 6 | + fn_name: &Ident, |
| 7 | + args: &[Argument], |
| 8 | +) -> TokenStream { |
| 9 | + if args.iter().all(|arg| arg.strategy.is_none()) { |
| 10 | + no_custom_strategies(fn_name, args) |
| 11 | + } else { |
| 12 | + custom_strategies(fn_name, args) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +// we can avoid boxing strategies if there are no custom strategies, since we have types written |
| 17 | +// out in function args |
| 18 | +// |
| 19 | +// If there are custom strategies, we can't write the type, because we're only provided the |
| 20 | +// expression for the strategy (e.g. `#[strategy = my_custom_strategy()]` doesn't tell us the |
| 21 | +// return type of `my_custom_strategy`). In these cases, we just use `BoxedStrategy<Self>` |
| 22 | +fn no_custom_strategies(fn_name: &Ident, args: &[Argument]) -> TokenStream { |
| 23 | + let arg_types = args.iter().map(|arg| { |
| 24 | + let ty = &arg.pat_ty.ty; |
| 25 | + quote!(#ty,) |
| 26 | + }); |
| 27 | + |
| 28 | + let arg_types = quote! { #(#arg_types)* }; |
| 29 | + |
| 30 | + let arg_names = args.iter().enumerate().map(|(index, arg)| { |
| 31 | + let name = nth_field_name(arg.pat_ty.span(), index); |
| 32 | + quote!(#name,) |
| 33 | + }); |
| 34 | + |
| 35 | + let arg_names = quote! { #(#arg_names)* }; |
| 36 | + |
| 37 | + let strategy_type = quote! { |
| 38 | + ::proptest::strategy::Map<::proptest::arbitrary::StrategyFor<(#arg_types)>, fn((#arg_types)) -> Self> |
| 39 | + }; |
| 40 | + |
| 41 | + let strategy_expr = quote! { |
| 42 | + use ::proptest::strategy::Strategy; |
| 43 | + ::proptest::prelude::any::<(#arg_types)>().prop_map(|(#arg_names)| Self { #arg_names }) |
| 44 | + }; |
| 45 | + |
| 46 | + arbitrary_shared(fn_name, strategy_type, strategy_expr) |
| 47 | +} |
| 48 | + |
| 49 | +// if we have `fn foo(#[strategy = x] a: i32, b: i32) {}`, we want to generate something like this: |
| 50 | +// ```ignore |
| 51 | +// impl Arbitrary for FooArgs { |
| 52 | +// type Parameters = (); |
| 53 | +// type Strategy = BoxedStrategy<Self>; |
| 54 | +// |
| 55 | +// fn arbitrary_with((): Self::Parameters) -> Self::Strategy { |
| 56 | +// (x, any::<i32>()).prop_map(|(a, b)| Self { a, b }).boxed() |
| 57 | +// } |
| 58 | +// } |
| 59 | +// ``` |
| 60 | +fn custom_strategies(fn_name: &Ident, args: &[Argument]) -> TokenStream { |
| 61 | + let arg_strategies = args.iter().map(|arg| { |
| 62 | + arg.strategy |
| 63 | + .as_ref() |
| 64 | + .map(|s| s.to_token_stream()) |
| 65 | + .unwrap_or_else(|| { |
| 66 | + let ty = &arg.pat_ty.ty; |
| 67 | + quote_spanned! { |
| 68 | + ty.span() => ::proptest::prelude::any::<#ty>() |
| 69 | + } |
| 70 | + }) |
| 71 | + }); |
| 72 | + |
| 73 | + let arg_names: TokenStream = args |
| 74 | + .iter() |
| 75 | + .enumerate() |
| 76 | + .map(|(index, arg)| { |
| 77 | + let name = nth_field_name(arg.pat_ty.span(), index); |
| 78 | + quote!(#name,) |
| 79 | + }) |
| 80 | + .collect(); |
| 81 | + let arg_names = &arg_names; |
| 82 | + |
| 83 | + let strategy_expr = quote! { |
| 84 | + use ::proptest::strategy::Strategy; |
| 85 | + (#(#arg_strategies),*).prop_map(|(#arg_names)| Self { #arg_names }).boxed() |
| 86 | + }; |
| 87 | + |
| 88 | + let strategy_type = quote! { |
| 89 | + ::proptest::strategy::BoxedStrategy<Self> |
| 90 | + }; |
| 91 | + arbitrary_shared(fn_name, strategy_type, strategy_expr) |
| 92 | +} |
| 93 | + |
| 94 | +/// shared code between both boxed and unboxed paths |
| 95 | +fn arbitrary_shared( |
| 96 | + fn_name: &Ident, |
| 97 | + strategy_type: TokenStream, |
| 98 | + strategy_expr: TokenStream, |
| 99 | +) -> TokenStream { |
| 100 | + let struct_name = struct_name(fn_name); |
| 101 | + |
| 102 | + quote! { |
| 103 | + impl ::proptest::prelude::Arbitrary for #struct_name { |
| 104 | + type Parameters = (); |
| 105 | + type Strategy = #strategy_type; |
| 106 | + |
| 107 | + fn arbitrary_with((): Self::Parameters) -> Self::Strategy { |
| 108 | + #strategy_expr |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments