trybuild | Test harness for ui tests of compiler diagnostics | Testing library
kandi X-RAY | trybuild Summary
kandi X-RAY | trybuild Summary
[] [] [] [] Trybuild is a test harness for invoking rustc on a set of test cases and asserting that any resulting error messages are the ones intended. . Such tests are commonly useful for testing error reporting involving procedural macros. We would write test cases triggering either errors detected by the macro or errors detected by the Rust compiler in the resulting expanded code, and compare against the expected errors to ensure that they remain user-friendly. This style of testing is sometimes called ui tests because they test aspects of the user’s interaction with a library outside of what would be covered by ordinary API tests. Nothing here is specific to macros; trybuild would work equally well for testing misuse of non-macro APIs.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of trybuild
trybuild Key Features
trybuild Examples and Code Snippets
Community Discussions
Trending Discussions on trybuild
QUESTION
I'm trying to implement the builder pattern from the proc macro workshop I'm creating a proc macro which parses a struct, extracts its name
, field_names
and field_types
. It should reproduce the struct itself and also create a builder struct with the same field_names
but with optional types.
My problem is that field_name
and field_type
are iterators that I would have to use twice in order to create two structs out of one.
This is my source tree
...ANSWER
Answered 2021-Jan-06 at 21:28Iterators can only be used zero or one time, not multiple; that's standard Rust and doesn't involve the quote!
macro:
QUESTION
From the Syn documentation:
Syn operates on the token representation provided by the
proc-macro2
crate from crates.io rather than using the compiler's built inproc-macro
crate directly. This enables code usingSyn
to execute outside of the context of a procedural macro, such as in unit tests orbuild.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. 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.
ANSWER
Answered 2020-Dec-12 at 20:46It does not work with the
proc_macro2::TokenStream
type, but it won't work with theproc_macro::TokenStream
because we are not in a proc-macro context.
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. This is the reason why proc_macro2
exists in the first place.
You need to use multiple crates in order to write tests for code that uses syn
and proc_macro2
:
- Your public crate that declares the macros with
#[proc_macro]
etc., and does very little except convert aproc_macro::TokenStream
into aproc_macro2::TokenStream
and vice versa. - An "internal" crate, containing most of the actual code, which depends on
proc_macro2
but notproc_macro
. Your tests can go in here.
The error you are seeing is because syn::parse
accepts a proc_macro::TokenStream
. You can instead use syn::parse2
, which is identical except that it accepts a proc_macro2::TokenStream
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install trybuild
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page