hegeltest 0.15.2

Property-based testing for Rust, built on Hypothesis
Documentation
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
mod common;

use common::project::TempRustProject;
use common::utils::{assert_matches_regex, expect_panic};
use hegel::TestCase;
use hegel::generators;

// ============================================================
// Compile error tests (via TempRustProject)
// ============================================================

#[test]
fn test_explicit_test_case_on_bare_function() {
    let code = r#"
#[hegel::explicit_test_case(x = 42)]
fn my_func(tc: hegel::TestCase) {
    let _ = tc;
}

fn main() {}
"#;
    TempRustProject::new()
        .main_file(code)
        .expect_failure("can only be used together with.*hegel::test")
        .cargo_run(&[]);
}

#[test]
fn test_explicit_test_case_wrong_order() {
    let code = r#"
#[hegel::explicit_test_case(x = 42)]
#[hegel::test]
fn my_test(tc: hegel::TestCase) {
    let _ = tc;
}

fn main() {}
"#;
    TempRustProject::new()
        .main_file(code)
        .expect_failure("must appear below.*hegel::test.*not above")
        .cargo_run(&[]);
}

#[test]
fn test_explicit_test_case_bad_syntax() {
    // Semicolon instead of comma should produce a compile error, not a silent empty case.
    let code = r#"
#[hegel::test]
#[hegel::explicit_test_case(x = 42;)]
fn my_test(tc: hegel::TestCase) {
    let x: i32 = tc.draw(hegel::generators::integers());
    let _ = x;
}

fn main() {}
"#;
    TempRustProject::new()
        .main_file(code)
        .expect_failure("expected `,`")
        .cargo_run(&[]);
}

#[test]
fn test_explicit_test_case_empty_args() {
    let code = r#"
#[hegel::test]
#[hegel::explicit_test_case()]
fn my_test(tc: hegel::TestCase) {
    let _ = tc;
}

fn main() {}
"#;
    TempRustProject::new()
        .main_file(code)
        .expect_failure("requires at least one")
        .cargo_run(&[]);
}

#[test]
fn test_explicit_test_case_no_parens() {
    let code = r#"
#[hegel::test]
#[hegel::explicit_test_case]
fn my_test(tc: hegel::TestCase) {
    let _ = tc;
}

fn main() {}
"#;
    TempRustProject::new()
        .main_file(code)
        .expect_failure("requires arguments")
        .cargo_run(&[]);
}

#[test]
fn test_explicit_test_case_rejects_threading_body() {
    // #[hegel::test] expands the body twice: once with `tc: TestCase` (Send)
    // and once with `tc: &ExplicitTestCase` (not Send). Spawning a thread that
    // moves `tc` compiles in the property-test version but must fail to
    // compile in the explicit version, because `&ExplicitTestCase` is not
    // Send. This test pins that behavior — if someone accidentally makes
    // ExplicitTestCase Send/Sync, the compile-fail check will stop firing.
    let code = r#"
use hegel::generators as gs;

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(x = true)]
fn my_test(tc: hegel::TestCase) {
    let tc_clone = tc.clone();
    let handle = std::thread::spawn(move || {
        let _: bool = tc_clone.draw(gs::booleans());
    });
    handle.join().unwrap();
    let _x: bool = tc.draw(gs::booleans());
}
"#;
    TempRustProject::new()
        .test_file("test_explicit_threading.rs", code)
        .expect_failure("cannot be shared between threads safely")
        .cargo_test(&["--test", "test_explicit_threading"]);
}

// ============================================================
// Success cases (inline #[hegel::test])
// ============================================================

#[hegel::test]
#[hegel::explicit_test_case(x = true)]
fn test_single_explicit_case(tc: TestCase) {
    let x = tc.draw(generators::booleans());
    let _ = x;
}

#[hegel::test]
#[hegel::explicit_test_case(x = true)]
#[hegel::explicit_test_case(x = false)]
fn test_multiple_explicit_cases(tc: TestCase) {
    let x = tc.draw(generators::booleans());
    let _ = x;
}

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(x = 42i32)]
fn test_explicit_case_with_property_test(tc: TestCase) {
    let x: i32 = tc.draw(generators::integers());
    assert_eq!(x, x);
}

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(x = 42u32)]
fn test_explicit_case_type_annotated_draw_uses_name(tc: TestCase) {
    // This verifies the draw is rewritten to __draw_named("x", ...) even with
    // a type annotation. If it fell back to "unnamed", the explicit test case
    // would panic with "no value provided for unnamed".
    let x: u32 = tc.draw(generators::integers());
    let _ = x;
}

#[derive(Debug, Clone, PartialEq)]
struct Point {
    x: i32,
    y: i32,
}

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(p = Point { x: 3, y: 4 })]
fn test_explicit_case_with_user_defined_struct(tc: TestCase) {
    let p: Point = tc.draw(generators::just(Point { x: 0, y: 0 }));
    assert_eq!(p, p);
}

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(p = Point { x: 3, y: 4 }, q = Point { x: -1, y: 0 })]
fn test_explicit_case_with_multiple_structs(tc: TestCase) {
    let p: Point = tc.draw(generators::just(Point { x: 0, y: 0 }));
    let q: Point = tc.draw(generators::just(Point { x: 0, y: 0 }));
    let _ = (p, q);
}

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(n = vec![1i32, 2, 3].into_iter().sum::<i32>())]
fn test_explicit_case_with_function_evaluation(tc: TestCase) {
    let n: i32 = tc.draw(generators::integers());
    let _ = n;
}

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(s = ["hello", "world"].join(" "))]
fn test_explicit_case_with_method_chain(tc: TestCase) {
    let s: String = tc.draw(generators::text());
    let _ = s;
}

// ============================================================
// Runtime panic tests
// ============================================================

#[test]
fn test_explicit_draw_unnamed() {
    let etc = hegel::ExplicitTestCase::new().with_value("draw", "42", 42i32);
    etc.run(|tc: &hegel::ExplicitTestCase| {
        let x: i32 = tc.draw(generators::integers());
        assert_eq!(x, 42);
    });
}

#[test]
fn test_explicit_note() {
    let etc = hegel::ExplicitTestCase::new().with_value("x", "true", true);
    etc.run(|tc: &hegel::ExplicitTestCase| {
        let _: bool = tc.__draw_named(generators::booleans(), "x", false);
        tc.note("some note");
    });
}

#[test]
fn test_explicit_notes_printed_on_panic_inline() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new().with_value("x", "42", 42i32);
            etc.run(|tc: &hegel::ExplicitTestCase| {
                let _: i32 = tc.__draw_named(generators::integers(), "x", false);
                tc.note("a note");
                panic!("intentional");
            });
        },
        "intentional",
    );
}

#[test]
fn test_explicit_assume_passes() {
    let etc = hegel::ExplicitTestCase::new();
    etc.assume(true);
}

#[test]
fn test_explicit_assume_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new();
            etc.assume(false);
        },
        "__HEGEL_ASSUME_FAIL",
    );
}

#[test]
fn test_explicit_reject_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new();
            etc.reject();
        },
        "__HEGEL_ASSUME_FAIL",
    );
}

#[test]
fn test_explicit_target_is_noop() {
    let etc = hegel::ExplicitTestCase::new();
    etc.target(42.0);
    etc.target_labelled(42.0, "label");
    etc.target_labelled(0.0, "");
}

#[test]
fn test_explicit_start_span_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new();
            etc.start_span(0);
        },
        "start_span is not supported in explicit test cases",
    );
}

#[test]
fn test_explicit_stop_span_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new();
            etc.stop_span(false);
        },
        "stop_span is not supported in explicit test cases",
    );
}

#[test]
fn test_explicit_draw_silent_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new().with_value("x", "true", true);
            etc.run(|tc: &hegel::ExplicitTestCase| {
                let _: bool = tc.draw_silent(generators::booleans());
            });
        },
        "draw_silent is not supported in explicit test cases",
    );
}

#[test]
fn test_explicit_type_mismatch_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new().with_value("x", "42", 42i32);
            etc.run(|tc: &hegel::ExplicitTestCase| {
                // Try to draw as String instead of i32
                let _: String = tc.__draw_named(generators::text(), "x", false);
            });
        },
        "type mismatch",
    );
}

#[test]
fn test_explicit_unused_values_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new()
                .with_value("x", "true", true)
                .with_value("y", "false", false);
            etc.run(|tc: &hegel::ExplicitTestCase| {
                let _: bool = tc.__draw_named(generators::booleans(), "x", false);
                // y is never drawn
            });
        },
        "never drawn",
    );
}

#[test]
fn test_explicit_unknown_name_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new().with_value("x", "true", true);
            etc.run(|tc: &hegel::ExplicitTestCase| {
                let _: bool = tc.__draw_named(generators::booleans(), "nonexistent", false);
            });
        },
        "no value provided for.*nonexistent",
    );
}

#[test]
fn test_explicit_double_consume_panics() {
    expect_panic(
        || {
            let etc = hegel::ExplicitTestCase::new().with_value("x", "true", true);
            etc.run(|tc: &hegel::ExplicitTestCase| {
                let _: bool = tc.__draw_named(generators::booleans(), "x", false);
                let _: bool = tc.__draw_named(generators::booleans(), "x", false);
            });
        },
        "already consumed",
    );
}

// ============================================================
// Output format tests (via TempRustProject)
// ============================================================

#[test]
fn test_explicit_output_format_with_comment() {
    let code = r#"
fn main() {
    let etc = hegel::ExplicitTestCase::new()
        .with_value("x", "compute()", 42i32);
    etc.run(|tc: &hegel::ExplicitTestCase| {
        let _: i32 = tc.__draw_named(hegel::generators::integers(), "x", false);
        panic!("intentional");
    });
}
"#;
    let output = TempRustProject::new()
        .main_file(code)
        .expect_failure("intentional")
        .cargo_run(&[]);

    // Source and debug differ, so comment should appear
    assert_matches_regex(&output.stderr, r"let x = compute\(\); // = 42");
}

#[test]
fn test_explicit_output_format_without_comment() {
    let code = r#"
fn main() {
    let etc = hegel::ExplicitTestCase::new()
        .with_value("x", "42", 42i32);
    etc.run(|tc: &hegel::ExplicitTestCase| {
        let _: i32 = tc.__draw_named(hegel::generators::integers(), "x", false);
        panic!("intentional");
    });
}
"#;
    let output = TempRustProject::new()
        .main_file(code)
        .expect_failure("intentional")
        .cargo_run(&[]);

    // Source "42" and debug "42" are the same, so no comment
    assert_matches_regex(&output.stderr, r"let x = 42;");
    assert!(
        !output.stderr.contains("// ="),
        "Should not have comment when source matches debug. Actual: {}",
        output.stderr
    );
}

#[test]
fn test_explicit_notes_printed_on_panic() {
    let code = r#"
fn main() {
    let etc = hegel::ExplicitTestCase::new()
        .with_value("x", "42", 42i32);
    etc.run(|tc: &hegel::ExplicitTestCase| {
        let _: i32 = tc.__draw_named(hegel::generators::integers(), "x", false);
        tc.note("important debug info");
        panic!("intentional");
    });
}
"#;
    let output = TempRustProject::new()
        .main_file(code)
        .expect_failure("intentional")
        .cargo_run(&[]);

    assert_matches_regex(&output.stderr, "important debug info");
}

// ============================================================
// Macro integration: output from #[hegel::explicit_test_case]
// ============================================================

#[test]
fn test_macro_explicit_case_output() {
    let code = r#"
#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(x = 42i32)]
fn test_explicit(tc: hegel::TestCase) {
    let x: i32 = tc.draw(hegel::generators::integers());
    panic!("fail: {}", x);
}
"#;
    TempRustProject::new()
        .test_file("test_etc.rs", code)
        .expect_failure("fail: 42")
        .cargo_test(&["--test", "test_etc"]);
}

#[test]
fn test_macro_explicit_case_with_struct() {
    let code = r#"
use hegel::generators as gs;

#[derive(Debug, Clone, PartialEq)]
struct Point { x: i32, y: i32 }

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(p = Point { x: 3, y: 4 })]
fn test_explicit(tc: hegel::TestCase) {
    let p: Point = tc.draw(gs::just(Point { x: 0, y: 0 }));
    panic!("fail: {:?}", p);
}
"#;
    TempRustProject::new()
        .test_file("test_struct.rs", code)
        .expect_failure(r"fail: Point \{ x: 3, y: 4 \}")
        .cargo_test(&["--test", "test_struct"]);
}

#[test]
fn test_macro_explicit_case_with_computed_expression() {
    let code = r#"
use hegel::generators as gs;

#[hegel::test(test_cases = 1)]
#[hegel::explicit_test_case(n = vec![10i32, 20, 30].into_iter().sum::<i32>())]
fn test_explicit(tc: hegel::TestCase) {
    let n: i32 = tc.draw(gs::integers());
    panic!("fail: {}", n);
}
"#;
    TempRustProject::new()
        .test_file("test_computed.rs", code)
        .expect_failure("fail: 60")
        .cargo_test(&["--test", "test_computed"]);
}