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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Hegel is a property-based testing library for Rust. Hegel is based on [Hypothesis](https://github.com/hypothesisworks/hypothesis), using the [Hegel](https://hegel.dev/) protocol.
//!
//! # Getting started with Hegel for Rust
//!
//! This guide walks you through the basics of installing Hegel and writing your first tests.
//!
//! ## Prerequisites
//!
//! You will need [`uv`](https://docs.astral.sh/uv/) installed and on your PATH.
//!
//! ## Install Hegel
//!
//! Add `hegel-rust` to your `Cargo.toml` as a dev dependency using cargo:
//!
//! ```bash
//! cargo add --dev hegeltest
//! ```
//!
//! ## Write your first test
//!
//! You're now ready to write your first test. We'll use Cargo as a test runner for the
//! purposes of this guide. Create a new test in the project's `tests/` directory:
//!
//! ```no_run
//! use hegel::TestCase;
//! use hegel::generators as gs;
//!
//! #[hegel::test]
//! fn test_integer_self_equality(tc: TestCase) {
//! let n = tc.draw(gs::integers::<i32>());
//! assert_eq!(n, n); // integers should always be equal to themselves
//! }
//! ```
//!
//! Now run the test using `cargo test --test <filename>`. You should see that this test passes.
//!
//! Let's look at what's happening in more detail. The `#[hegel::test]` attribute runs your test
//! many times (100, by default). The test function (in this case `test_integer_self_equality`)
//! takes a [`TestCase`] parameter, which provides a [`draw`](TestCase::draw) method for drawing
//! different values. This test draws a random integer and checks that it should be equal to itself.
//!
//! Next, try a test that fails:
//!
//! ```no_run
//! # use hegel::TestCase;
//! # use hegel::generators as gs;
//! #[hegel::test]
//! fn test_integers_always_below_50(tc: TestCase) {
//! let n = tc.draw(gs::integers::<i32>());
//! assert!(n < 50); // this will fail!
//! }
//! ```
//!
//! This test asserts that any integer is less than 50, which is obviously incorrect. Hegel will
//! find a test case that makes this assertion fail, and then shrink it to find the smallest
//! counterexample — in this case, `n = 50`.
//!
//! To fix this test, you can constrain the integers you generate with the `min_value` and
//! `max_value` functions:
//!
//! ```no_run
//! # use hegel::TestCase;
//! # use hegel::generators as gs;
//! #[hegel::test]
//! fn test_bounded_integers_always_below_50(tc: TestCase) {
//! let n = tc.draw(gs::integers::<i32>()
//! .min_value(0)
//! .max_value(49));
//! assert!(n < 50);
//! }
//! ```
//!
//! Run the test again. It should now pass.
//!
//! ## Use generators
//!
//! Hegel provides a rich library of generators that you can use out of the box. There are
//! primitive generators, such as [`integers`](generators::integers),
//! [`floats`](generators::floats), and [`text`](generators::text), and combinators that allow
//! you to make generators out of other generators, such as [`vecs`](generators::vecs) and
//! [`tuples`].
//!
//! For example, you can use [`vecs`](generators::vecs) to generate a vector of integers:
//!
//! ```no_run
//! # use hegel::TestCase;
//! use hegel::generators as gs;
//!
//! #[hegel::test]
//! fn test_append_increases_length(tc: TestCase) {
//! let mut vector = tc.draw(gs::vecs(gs::integers::<i32>()));
//! let initial_length = vector.len();
//! vector.push(tc.draw(gs::integers::<i32>()));
//! assert!(vector.len() > initial_length);
//! }
//! ```
//!
//! This test checks that appending an element to a random vector of integers should always
//! increase its length.
//!
//! You can also define custom generators. For example, say you have a `Person` struct that
//! we want to generate:
//!
//! ```no_run
//! # use hegel::TestCase;
//! # use hegel::generators as gs;
//! #[derive(Debug)]
//! struct Person {
//! age: i32,
//! name: String,
//! }
//!
//! #[hegel::composite]
//! fn generate_person(tc: TestCase) -> Person {
//! let age = tc.draw(gs::integers::<i32>());
//! let name = tc.draw(gs::text());
//! Person { age, name }
//! }
//! ```
//!
//! Note that you can feed the results of a `draw` to subsequent calls. For example, say that
//! you extend the `Person` struct to include a `driving_license` boolean field:
//!
//! ```no_run
//! # use hegel::TestCase;
//! # use hegel::generators as gs;
//! #[derive(Debug)]
//! struct Person {
//! age: i32,
//! name: String,
//! driving_license: bool,
//! }
//!
//! #[hegel::composite]
//! fn generate_person(tc: TestCase) -> Person {
//! let age = tc.draw(gs::integers::<i32>());
//! let name = tc.draw(gs::text());
//! let driving_license = if age >= 18 {
//! tc.draw(gs::booleans())
//! } else {
//! false
//! };
//! Person { age, name, driving_license }
//! }
//! ```
//!
//! ## Debug your failing test cases
//!
//! Use the [`note`](TestCase::note) method to attach debug information:
//!
//! ```no_run
//! # use hegel::TestCase;
//! # use hegel::generators as gs;
//! #[hegel::test]
//! fn test_with_notes(tc: TestCase) {
//! let x = tc.draw(gs::integers::<i32>());
//! let y = tc.draw(gs::integers::<i32>());
//! tc.note(&format!("x + y = {}, y + x = {}", x + y, y + x));
//! assert_eq!(x + y, y + x);
//! }
//! ```
//!
//! Notes only appear when Hegel replays the minimal failing example.
//!
//! ## Change the number of test cases
//!
//! By default Hegel runs 100 test cases. To override this, pass the `test_cases` argument
//! to the `test` attribute:
//!
//! ```no_run
//! # use hegel::TestCase;
//! # use hegel::generators as gs;
//! #[hegel::test(test_cases = 500)]
//! fn test_integers_many(tc: TestCase) {
//! let n = tc.draw(gs::integers::<i32>());
//! assert_eq!(n, n);
//! }
//! ```
//!
//! ## Threading
//!
//! [`TestCase`] is `Send` but not `Sync`: you can clone it and move the clone
//! to another thread to drive generation from there.
//!
//! ```no_run
//! use hegel::TestCase;
//! use hegel::generators as gs;
//!
//! #[hegel::test]
//! fn test_with_worker_thread(tc: TestCase) {
//! let tc_worker = tc.clone();
//! let handle = std::thread::spawn(move || {
//! tc_worker.draw(gs::vecs(gs::integers::<i32>()).max_size(10))
//! });
//! let xs = handle.join().unwrap();
//! let more: bool = tc.draw(gs::booleans());
//! let _ = (xs, more);
//! }
//! ```
//!
//! Clones share the same backend connection — they are views onto one test
//! case, not independent test cases. Individual backend calls are serialised
//! by a shared mutex, so code like "spawn worker, worker draws, join, main
//! thread draws" is deterministic.
//!
//! **Using threads is currently extremely fragile and should only be used with
//! extreme caution right now.** You are liable to get flaky test failures when
//! multiple threads draw concurrently. We intend to support this use case
//! increasingly well over time, but right now it is a significant footgun —
//! see [`TestCase`]'s documentation for the full contract and the patterns
//! that are safe to rely on.
//!
//! ## Learning more
//!
//! - Browse the [`generators`] module for the full list of available generators.
//! - See [`Settings`] for more configuration settings to customise how your test runs.
pub
pub
pub
pub
pub
pub
pub
pub use currently_in_test_context;
pub use ExplicitTestCase;
pub use Generator;
pub use TestCase;
// re-export for macro use
pub use ciborium;
pub use paste;
pub use ;
// re-export public api
pub use TestLocation;
/// Derive a generator for a struct or enum.
///
/// This implements [`DefaultGenerator`](generators::DefaultGenerator) for the type,
/// allowing it to be used with [`default`](generators::default) via `default::<T>()`.
///
/// For structs, the generated generator has:
/// - `<field>(generator)` - builder method to customize each field's generator
///
/// For enums, the generated generator has:
/// - `default_<VariantName>()` - methods returning default variant generators
/// - `<VariantName>(generator)` - builder methods to customize variant generation
///
/// # Struct Example
///
/// ```ignore
/// use hegel::DefaultGenerator;
/// use hegel::generators::{self as gs, DefaultGenerator as _, Generator as _};
///
/// #[derive(DefaultGenerator)]
/// struct Person {
/// name: String,
/// age: u32,
/// }
///
/// #[hegel::test]
/// fn generates_people(tc: hegel::TestCase) {
/// let generator = gs::default::<Person>()
/// .age(gs::integers::<u32>().min_value(0).max_value(120));
/// let person: Person = tc.draw(generator);
/// }
/// ```
///
/// # Enum Example
///
/// ```ignore
/// use hegel::DefaultGenerator;
/// use hegel::generators::{self as gs, DefaultGenerator as _, Generator as _};
///
/// #[derive(DefaultGenerator)]
/// enum Status {
/// Pending,
/// Active { since: String },
/// Error { code: i32, message: String },
/// }
///
/// #[hegel::test]
/// fn generates_statuses(tc: hegel::TestCase) {
/// let generator = gs::default::<Status>()
/// .Active(
/// gs::default::<Status>()
/// .default_Active()
/// .since(gs::text().max_size(20))
/// );
/// let status: Status = tc.draw(generator);
/// }
/// ```
pub use DefaultGenerator;
/// Define a composite generator from a function.
///
/// The first parameter must be a [`TestCase`] and is passed automatically
/// when the generator is drawn. Any additional parameters become parameters
/// of the returned factory function. The function must have an explicit
/// return type.
///
/// ```ignore
/// use hegel::generators as gs;
///
/// #[hegel::composite]
/// fn sorted_vec(tc: hegel::TestCase, min_len: usize) -> Vec<i32> {
/// let mut v: Vec<i32> = tc.draw(gs::vecs(gs::integers()).min_size(min_len));
/// v.sort();
/// v
/// }
///
/// #[hegel::test]
/// fn test_sorted(tc: hegel::TestCase) {
/// let v = tc.draw(sorted_vec(3));
/// assert!(v.len() >= 3);
/// assert!(v.windows(2).all(|w| w[0] <= w[1]));
/// }
/// ```
pub use composite;
pub use explicit_test_case;
pub use rewrite_draws;
/// Derive a [`StateMachine`](crate::stateful::StateMachine) implementation from an `impl` block.
///
/// See the [`stateful`] module docs for more information.
pub use state_machine;
/// The main entrypoint into Hegel.
///
/// The function must take exactly one parameter of type [`TestCase`]. The test case can be
/// used to generate values via [`TestCase::draw`].
///
/// The `#[test]` attribute is added automatically and must not be present on the function.
///
/// ```ignore
/// #[hegel::test]
/// fn my_test(tc: TestCase) {
/// let x: i32 = tc.draw(integers());
/// assert!(x + 0 == x);
/// }
/// ```
///
/// You can set settings using attributes on [`test`], corresponding to methods on [`Settings`]:
///
/// ```ignore
/// #[hegel::test(test_cases = 500)]
/// fn test_runs_many_more_times(tc: TestCase) {
/// let x: i32 = tc.draw(integers());
/// assert!(x + 0 == x);
/// }
/// ```
pub use test;
/// Turn a function into a standalone Hegel binary entry point.
///
/// The function must take exactly one parameter of type [`TestCase`]. Behaves
/// like [`test`] — draws are rewritten to record variable names, and any
/// `#[hegel::explicit_test_case]` attributes are run first — but instead of
/// producing a `#[test]` it produces a plain function body that parses CLI
/// arguments and runs a [`Hegel`] driver.
///
/// Supported CLI flags (with defaults taken from the attribute args):
/// `--test-cases`, `--seed`, `--verbosity`, `--derandomize`, `--database`,
/// `--suppress-health-check`, `-h` / `--help`.
///
/// ```ignore
/// use hegel::TestCase;
/// use hegel::generators as gs;
///
/// #[hegel::main(test_cases = 500)]
/// fn main(tc: TestCase) {
/// let n: i32 = tc.draw(gs::integers());
/// assert_eq!(n + 0, n);
/// }
/// ```
pub use main;
/// Rewrite a function taking a [`TestCase`] plus additional arguments into
/// one that takes just those arguments and internally runs Hegel.
///
/// Behaves like [`test`] for name rewriting, explicit test cases, and
/// settings parsing. The generated function has the original signature
/// with the `TestCase` parameter removed, and its body is run as an
/// [`FnMut`] closure inside [`Hegel::run`].
///
/// ```ignore
/// use hegel::TestCase;
/// use hegel::generators as gs;
///
/// #[hegel::standalone_function(test_cases = 10)]
/// fn check_addition_commutative(tc: TestCase, increment: i32) {
/// let n: i32 = tc.draw(gs::integers());
/// assert_eq!(n + increment, increment + n);
/// }
///
/// // callers invoke it as a normal function:
/// # fn _example() {
/// check_addition_commutative(5);
/// # }
/// ```
pub use standalone_function;
pub use CliOutcome;
pub use apply_cli_args as __apply_cli_args;
pub use __test_kill_server;
pub use format_log_excerpt;
pub use hegel;
pub use ;