eupn / macrotest

Test harness for declarative and procedural macros expansion via `cargo-expand`
48 stars 9 forks source link

proc-macro for generating separate test cases for each generated output sample #37

Open mersinvald opened 4 years ago

mersinvald commented 4 years ago

It would be awesome to have a macro for generating separate test cases for each generated sample in the expanded/ directory.

Motivation for that is having more granular test results, which would show a test failure for a specific macro or specific macro invocation.

The ideal API, IMHO, would be something like

#[macrotest::tests("expanded/*")]
mod macro_tests;
eupn commented 4 years ago

@mersinvald pls correct me if I wrong.

An example of how the expansion of this procedural macro will probably look like:

Suppose we have 3 test cases in tests/expand/ directory: a.rs, b.rs, and c.rs.

In our tests/expand.rs we have the following code:

#[macrotest:tests("tests/expand/*.rs");
mod expansion_tests;

This should probably expand to something like this:

mod macro_tests {
    #[test]
    pub fn test_expand_a() {
        macrotest::expand("tests/expand/a.rs");
    }

    #[test]
    pub fn test_expand_b() {
        macrotest::expand("tests/expand/b.rs");
    }

    #[test]
    pub fn test_expand_c() {
        macrotest::expand("tests/expand/c.rs");
    }
}

NB: the #[macrotest:tests("glob-pattern")] must ignore the *.expanded.rs files in the directory specified. Otherwise, the macro will generate test cases for expanded code.

mersinvald commented 4 years ago

@eupn sorry I missed your response. Yes, that’s exactly the expansion I have had in mind.