简体   繁体   English

单元测试 Rust Syn crate

[英]Unit testing Rust Syn crate

From the Syn documentation :来自Syn 文档

Syn operates on the token representation provided by the proc-macro2 crate from crates.io rather than using the compiler's built in proc-macro crate directly. Syn 对 crates.io 中的proc-macro2 crate 提供的令牌表示进行操作,而不是直接使用编译器内置的proc-macro crate。 This enables code using Syn to execute outside of the context of a procedural macro, such as in unit tests or build.rs这使得使用Syn的代码可以在过程宏的上下文之外执行,例如在单元测试或build.rs

I am trying to enable unit testing for some Syn functions, however I can't get it to work no matter what I have tried.我正在尝试为某些 Syn 功能启用单元测试,但是无论我尝试了什么,我都无法让它工作。 It does not work with the proc_macro2::TokenStream type, but it won't work with the proc_macro::TokenStream because we are not in a proc-macro context.它不适用于proc_macro2::TokenStream类型,但它不适用于proc_macro::TokenStream因为我们不在 proc-macro 上下文中。

link to playground 链接到游乐场

use quote::quote;
use syn;

fn test() {
    // let stream: syn::export::TokenStream = quote!{fn foo() {};}.into(); // doesn't work
    let stream: proc_macro2::TokenStream = quote!{fn foo() {};}.into(); // doesn't work
    // let item = parse_macro_input!(stream as Item); // doesn't work
    let item = syn::parse(stream).unwrap();

}

fn main() {
    test();
}

Any help on how to test syn functions outside of the proc-macro context would be appreciated.任何有关如何在 proc-macro 上下文之外测试 syn 函数的帮助将不胜感激。 I am aware of the trybuild crate, but I would like to be able to unit test the macro's functions first.我知道trybuild crate,但我希望能够先对宏的功能进行单元测试。

It does not work with the proc_macro2::TokenStream type, but it won't work with the proc_macro::TokenStream because we are not in a proc-macro context.它不适用于proc_macro2::TokenStream类型,但它不适用于proc_macro::TokenStream因为我们不在 proc-macro 上下文中。

Yes, and that's the whole point, Crates that export procedural macros can't export anything else, but proc_macro can only be used in crates that export macros.是的,这就是重点,导出过程宏的板条箱不能导出其他任何东西,但proc_macro只能用于导出宏的板条箱中。 This is the reason why proc_macro2 exists in the first place.这就是proc_macro2存在的原因。

You need to use multiple crates in order to write tests for code that uses syn and proc_macro2 :您需要使用多个 crate 才能为使用synproc_macro2的代码编写测试:

  • Your public crate that declares the macros with #[proc_macro] etc., and does very little except convert a proc_macro::TokenStream into a proc_macro2::TokenStream and vice versa.您的公共 crate 使用#[proc_macro]等声明宏,除了将proc_macro::TokenStream转换为 proc_macro2::TokenStream 外,它几乎没有做任何proc_macro2::TokenStream ,反之亦然。
  • An "internal" crate, containing most of the actual code, which depends on proc_macro2 but not proc_macro .一个“内部”板条箱,包含大部分实际代码,它取决于proc_macro2而不是proc_macro Your tests can go in here.你的测试可以在这里 go 。

The error you are seeing is because syn::parse accepts a proc_macro::TokenStream .您看到的错误是因为syn::parse接受proc_macro::TokenStream You can instead use syn::parse2 , which is identical except that it accepts a proc_macro2::TokenStream .您可以改用syn::parse2 ,除了它接受proc_macro2::TokenStream之外,它是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM