proptest_macro/lib.rs
1use proc_macro::TokenStream;
2
3mod property_test;
4
5/// The `property_test` procedural macro simplifies the creation of property-based tests
6/// using the `proptest` crate. This macro provides a more concise syntax for writing tests
7/// that automatically generate test cases based on properties.
8///
9/// # Example
10///
11/// Using the `property_test` macro:
12///
13/// ```
14/// # use proptest_macro::property_test;
15/// #[property_test]
16/// fn foo(x: i32) {
17/// assert_eq!(x, x);
18/// }
19/// ```
20///
21/// is roughly equivalent to:
22///
23/// ```ignore
24/// proptest! {
25/// #[test]
26/// fn foo(x in any::<i32>()) {
27/// assert_eq!(x, x);
28/// }
29/// }
30/// ```
31///
32/// # Details
33///
34/// The `property_test` macro is used to define property-based tests, where the parameters
35/// of the test function are automatically generated by `proptest`. The macro takes care
36/// of setting up the test harness and generating input values, allowing the user to focus
37/// on writing the test logic.
38///
39/// ## Attributes
40///
41/// The `property_test` macro can take an optional `config` attribute, which allows you to
42/// customize the configuration of the `proptest` runner.
43///
44/// E.g. running 100 cases:
45///
46/// ```rust,ignore
47/// #[property_test(config = "ProptestConfig { cases: 100, .. ProptestConfig::default() }")]
48/// fn foo(x: i32) {
49/// assert_eq!(x, x);
50/// }
51/// ```
52///
53/// ## Custom strategies
54///
55/// By default, [`property_test`] will use the `Arbitrary` impl for parameters. However, you can
56/// provide a custom `Strategy` with `#[strategy = <expr>]` on an argument:
57///
58/// ```
59/// # use proptest_macro::property_test;
60/// #[property_test]
61/// fn foo(#[strategy = "[0-9]*"] s: String) {
62/// for c in s.chars() {
63/// assert!(c.is_numeric());
64/// }
65/// }
66/// ```
67/// Multiple `#[strategy = <expr>]` attributes on an argument are not allowed.
68///
69/// ## Semver guarantees of generated code
70///
71/// This macro generates a struct with a name derived from the name of the test function. Details
72/// of this struct, such as its name, field names, or even whether it exists are considered
73/// implementation details, and may change without a major version bump.
74#[proc_macro_attribute]
75pub fn property_test(attr: TokenStream, item: TokenStream) -> TokenStream {
76 property_test::property_test(attr.into(), item.into()).into()
77}