Skip to content

Difficult to read test failure messages when assertions fail #13

@DavidAntliff

Description

@DavidAntliff

This is for assertables-9.8.0, with Rust 1.88.0.

I'm using assertables to create a custom test assertion. The intention is to compare lines of an assembly file with the "expected" output, but to ignore whitespace, thus the lines are compared "word by word". If it fails, I would like the test error message to clearly show the mismatched line.

Maybe I'm just using it incorrectly, but my error messages aren't very readable:

 a debug: `Err("assertion failed: `assert_eq!(a, b)`\nhttps://docs.rs/assertables/0.1.0/assertables/macro.assert_eq.html\n a label: `line_parts`,\n a debug: `[\"ret\"]`,\n b label: `expected_parts`,\n b debug: `[\"retfoo\"]`")`

Aside: I'm not sure why that bogus URL appears in the output - it's a 404.

Here's the code that caused that error (retfoo should be ret to pass):

        assert_ok!(listing_is_equivalent(
            &listing.unwrap(),
            &format!(
                r#"
                .text
            {simple}:
                retfoo
            "#
            )
        ));
    fn listing_is_equivalent(listing: &str, expected: &str) -> Result<(), String> {
        let listing = listing
            .lines()
            .filter(|line| !line.trim().is_empty())
            .map(str::trim)
            .collect::<Vec<_>>();
        let expected = expected
            .lines()
            .filter(|line| !line.trim().is_empty())
            .map(str::trim)
            .collect::<Vec<_>>();

        assert_eq_as_result!(listing.len(), expected.len())?;

        for (actual, expected) in listing.iter().zip(expected) {
            asm_line_equivalent(actual, expected)?;
        }
        Ok(())
    }

    fn asm_line_equivalent(line: &str, expected: &str) -> Result<(), String> {
        let line = line.trim();
        let expected = expected.trim();

        let line_parts = line.split_whitespace().collect::<Vec<_>>();
        let expected_parts = expected.split_whitespace().collect::<Vec<_>>();

        assert_eq_as_result!(line_parts, expected_parts)
    }

Incidentally, is there a way to annotate the assertion failures? For example, it would be nice in listing_is_equivalent() to be able to attach some info to the error returned when asm_line_equivalent() fails, to show that the internal assert_eq failed for "line 3", which I could determine in the zip-loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions