Skip to content

Better output printing.#143

Open
DRMacIver wants to merge 3 commits intomainfrom
DRMacIver/named-draws
Open

Better output printing.#143
DRMacIver wants to merge 3 commits intomainfrom
DRMacIver/named-draws

Conversation

@DRMacIver
Copy link
Copy Markdown
Member

This introduces draw_named as the primitive for when we are actually printing, which provides much nicer output for values.

It then makes the hegel::test macro automatically rewrite draw() to draw_named where it is safe to do so.

The result is that if you have a test that looks something like:

use hegel::{generators as gs};

#[hegel::test]
fn my_test(tc: hegel::TestCase){
    let mut running_total = 0;

    let iters = tc.draw(gs::integers().min_value(0).max_value(10));

    for _ in 0..iters {
        let summand = tc.draw(gs::integers().min_value(0).max_value(100));
        running_total += summand;
        assert!(running_total <= 100);
    }
}

You will see output like:

let iters = 2;
let summand_1 = 1;
let summand_2 = 100;
thread 'my_test' (2) panicked at examples/new_format.rs:12:9:
assertion failed: running_total <= 100

If you were to write it like this without the intermediate variable:

use hegel::{generators as gs};

#[hegel::test]
fn my_test(tc: hegel::TestCase){
    let mut running_total = 0;

    let iters = tc.draw(gs::integers().min_value(0).max_value(10));

    for _ in 0..iters {
        running_total += tc.draw(gs::integers().min_value(0).max_value(100));
        assert!(running_total <= 100);
    }
}

You would see:

let iters = 2;
let unnamed_1 = 1;
let unnamed_2 = 100;
thread 'my_test' (2) panicked at examples/new_format.rs:11:9:
assertion failed: running_total <= 100

Users are able to name things directly if they want. It's a bit unergonomic but not awful. e.g.

use hegel::{generators as gs};

#[hegel::test]
fn my_test(tc: hegel::TestCase){
    let mut running_total = 0;

    let iters = tc.draw(gs::integers().min_value(0).max_value(10));

    for _ in 0..iters {
        running_total += tc.draw_named(gs::integers().min_value(0).max_value(100), "summand", true);
        assert!(running_total <= 100);
    }
}

is equivalent to the first example.

DRMacIver and others added 3 commits March 27, 2026 10:22
This introduces draw_named as the primitive for when we are actually
printing, which provides much nicer output for values.

It then makes the hegel::test macro automatically rewrite draw()
to draw_named where it is safe to do so.
- Remove explicit 'a lifetime in is_tc_draw_binding (clippy::needless_lifetimes)
- Move NOTE text outside ```no_run code fence in draw() doc comment
- Allow clippy::let_and_return in closure test (needed for macro rewrite testing)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@DRMacIver DRMacIver marked this pull request as ready for review March 27, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant