1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Derive macros for protest-stateful
//!
//! This crate provides procedural macros for the protest-stateful stateful property testing library.
use TokenStream;
/// Derive macro for automatically implementing the Operation trait
///
/// This macro can be applied to enums to automatically generate implementations
/// of the Operation trait for stateful testing.
///
/// # Basic Usage
///
/// ```
/// use protest_stateful::{Operation, operations::OperationSequence, dsl::StatefulTest};
///
/// #[derive(Debug, Clone, Operation)]
/// #[operation(state = "Vec<i32>")]
/// enum StackOp {
/// #[execute("state.push(*field_0)")]
/// Push(i32),
/// #[execute("state.pop()")]
/// Pop,
/// #[execute("state.clear()")]
/// Clear,
/// }
/// ```
///
/// By default, the state type is inferred from the `#[operation(state = "...")]` attribute,
/// and each variant executes based on its structure.
///
/// # Customization
///
/// The derive macro supports various customization attributes:
///
/// ```
/// use protest_stateful::Operation;
///
/// #[derive(Debug, Clone, Operation)]
/// #[operation(state = "Vec<i32>")]
/// enum StackOp {
/// #[execute("state.push(*field_0)")]
/// Push(i32),
///
/// #[execute("state.pop()")]
/// #[precondition("!state.is_empty()")]
/// Pop,
///
/// #[execute("state.clear()")]
/// Clear,
/// }
/// ```
///
/// # Supported Attributes
///
/// ## Container-level attributes:
/// - `#[operation(state = "Type")]`: Specifies the state type (required)
///
/// ## Variant-level attributes:
/// - `#[execute("expression")]`: Custom execution expression
/// - `#[precondition("expression")]`: Precondition check expression
/// - `#[weight(N)]`: Weight for generation (higher = more frequent)
/// - `#[description("text")]`: Custom description for the operation
///
/// # Weight-based Generation
///
/// Use the `#[weight(N)]` attribute to control operation frequency:
///
/// ```
/// use protest_stateful::Operation;
///
/// #[derive(Debug, Clone, Operation)]
/// #[operation(state = "Vec<i32>")]
/// enum StackOp {
/// #[execute("state.push(*field_0)")]
/// #[weight(10)] // Generated 10x more often
/// Push(i32),
///
/// #[execute("state.pop()")]
/// #[weight(5)] // Generated 5x more often
/// Pop,
///
/// #[execute("state.clear()")]
/// #[weight(1)] // Generated least often
/// Clear,
/// }
/// ```
///
/// # Field Access
///
/// In `#[execute]` and `#[precondition]` expressions:
/// - Named fields: access by name (e.g., `self.value`)
/// - Unnamed fields: access by position (e.g., `self.0`, `self.1`)
/// - Unit variants: no field access needed
///
/// # Example with All Features
///
/// ```
/// use protest_stateful::Operation;
/// use std::collections::HashMap;
///
/// #[derive(Debug, Clone, Operation)]
/// #[operation(state = "HashMap<String, i32>")]
/// enum MapOp {
/// #[execute("state.insert(key.clone(), *value)")]
/// #[weight(5)]
/// #[description("Insert key-value pair")]
/// Insert { key: String, value: i32 },
///
/// #[execute("state.remove(key)")]
/// #[precondition("state.contains_key(key)")]
/// #[weight(3)]
/// Remove { key: String },
///
/// #[execute("state.clear()")]
/// #[weight(1)]
/// Clear,
/// }
/// ```
/// Declarative macro for creating stateful property tests
///
/// This macro provides a convenient DSL for defining stateful property tests
/// with less boilerplate than manually constructing StatefulTest instances.
///
/// # Basic Usage
///
/// ```
/// use protest_stateful::stateful_test;
/// use protest_stateful::prelude::Operation;
///
/// #[derive(Debug, Clone)]
/// enum CounterOp {
/// Increment,
/// Decrement,
/// }
///
/// impl Operation for CounterOp {
/// type State = i32;
///
/// fn execute(&self, state: &mut Self::State) {
/// match self {
/// CounterOp::Increment => *state += 1,
/// CounterOp::Decrement => *state -= 1,
/// }
/// }
/// }
///
/// stateful_test! {
/// name: counter_test,
/// state: i32 = 0,
/// operations: CounterOp,
/// invariants: {
/// "non_negative" => |state| *state >= 0,
/// "bounded" => |state| *state <= 1000,
/// },
/// config: {
/// iterations: 100,
/// max_sequence_length: 20,
/// }
/// }
/// ```
///
/// # Configuration Options
///
/// - `name`: Test function name (required)
/// - `state`: Initial state value and type (required)
/// - `operations`: Operation enum type (required)
/// - `invariants`: Named invariant checks (optional)
/// - `config`: Test configuration (optional)
/// - `iterations`: Number of test iterations (default: 100)
/// - `max_sequence_length`: Maximum operation sequence length (default: 10)
/// - `min_sequence_length`: Minimum operation sequence length (default: 1)
/// - `seed`: Random seed for reproducibility (optional)
///
/// # Complete Example
///
/// ```
/// use protest_stateful::stateful_test;
/// use protest_stateful::prelude::Operation;
/// use protest_stateful_derive::Operation;
///
/// #[derive(Debug, Clone, Operation)]
/// #[operation(state = "Vec<i32>")]
/// enum StackOp {
/// #[execute("state.push(*field_0)")]
/// Push(i32),
/// #[execute("state.pop()")]
/// Pop,
/// }
///
/// stateful_test! {
/// name: stack_properties,
/// state: Vec<i32> = vec![],
/// operations: StackOp,
/// invariants: {
/// "size_matches" => |state| state.len() <= 100,
/// },
/// config: {
/// iterations: 200,
/// max_sequence_length: 50,
/// seed: 42,
/// }
/// }
/// ```