pub fn emulate_macro_expansion_fallible<F>(
    file: File,
    macro_path: &str,
    proc_macro_fn: F
) -> Result<(), Error>where
    F: Fn(TokenStream) -> TokenStream,
Expand description

Parses the given Rust source code file, searching for macro expansions that use macro_path. Each time it finds one, it calls proc_macro_fn, passing it the inner TokenStream just as if the macro were being expanded. The only effect is to verify that the macro doesn’t panic, as the expansion is not actually applied to the AST or the source code.

Note that this parser only handles Rust’s syntax, so it cannot resolve paths to see if they are equivalent to the given one. The paths used to reference the macro must be exactly equal to the one given in order to be expanded by this function. For example, if macro_path is "foo" and the file provided calls the macro using bar::foo!, this function will not know to expand it, and the macro’s code coverage will be underestimated.

Also, this function uses proc_macro2::TokenStream, not the standard but partly unstable proc_macro::TokenStream. You can convert between them using their into methods, as shown below.

Returns

Ok on success, or an instance of Error indicating any error that occurred when trying to read or parse the file.

Example

#[proc_macro]
fn remove(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
    // This macro just eats its input and replaces it with nothing.
    proc_macro::TokenStream::empty()
}
 
extern crate syn;
 
#[test]
fn macro_code_coverage() {
    let file = std::fs::File::open("tests/tests.rs");
    emulate_macro_expansion(file, "remove", |ts| remove(ts.into()).into());
}