Giter Site home page Giter Site logo

Comments (1)

yutannihilation avatar yutannihilation commented on June 4, 2024

I've been wondering how I can use insta for snapshot tests on macro expansion, and I finally found it can be done with prettyplease, which converts an AST into a String of the formatted Rust code.

I write a helper function like this.

    fn assert_snapshot_helper(orig: syn::ItemFn) {
        let result: syn::ItemFn = generate_fn(&orig);
        let formatted = prettyplease::unparse(&parse_quote!(#result));
        let lines = formatted.lines().collect::<Vec<&str>>();
        insta::assert_yaml_snapshot!(lines);
    }

    #[test]
    fn test_generate_inner_fn() {
        assert_snapshot_helper(parse_quote!(
            #[my_macro]
            fn foo() -> Bar {
                bar()
            }
        ));

c.f. yutannihilation/savvy#175

extendr-macros' function also takes ItemFn or so and returns TokenStream, so I think the similar thing can be done.

pub fn extendr_function(mut func: ItemFn, opts: &wrappers::ExtendrOptions) -> TokenStream {
let mut wrappers: Vec<ItemFn> = Vec::new();
let res =
wrappers::make_function_wrappers(opts, &mut wrappers, "", &func.attrs, &mut func.sig, None);
if let Err(e) = res {
return e.into_compile_error().into();
};
TokenStream::from(quote! {
#func
# ( #wrappers )*
})
}

from extendr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.