From: Zhao Liu <zhao1.liu@intel.com>
To: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Cc: qemu-devel@nongnu.org, qemu-rust@nongnu.org,
"Alex Bennée" <alex.bennee@linaro.org>,
"Paolo Bonzini" <pbonzini@redhat.com>
Subject: Re: [PATCH 2/2] rust/qemu-api-macros: add unit tests
Date: Fri, 4 Jul 2025 20:37:25 +0800 [thread overview]
Message-ID: <aGfLBaW0p528ggCP@intel.com> (raw)
In-Reply-To: <20250704-rust_add_derive_macro_unit_tests-v1-2-ebd47fa7f78f@linaro.org>
On Fri, Jul 04, 2025 at 01:26:58PM +0300, Manos Pitsidianakis wrote:
> Date: Fri, 04 Jul 2025 13:26:58 +0300
> From: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> Subject: [PATCH 2/2] rust/qemu-api-macros: add unit tests
> X-Mailer: b4 0.14.2
>
> Add unit tests to check Derive macro output for expected error messages,
> or for expected correct codegen output.
>
> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> ---
> rust/qemu-api-macros/meson.build | 3 +
> rust/qemu-api-macros/src/lib.rs | 3 +
> rust/qemu-api-macros/src/tests.rs | 135 ++++++++++++++++++++++++++++++++++++++
> rust/qemu-api-macros/src/utils.rs | 1 +
> 4 files changed, 142 insertions(+)
>
> diff --git a/rust/qemu-api-macros/meson.build b/rust/qemu-api-macros/meson.build
> index 8610ce1c8440c4b6e38a8462d4975bf76d72fb05..2152bcb99b30e4bdcc1c5b887b7903a37f6181c3 100644
> --- a/rust/qemu-api-macros/meson.build
> +++ b/rust/qemu-api-macros/meson.build
> @@ -17,3 +17,6 @@ _qemu_api_macros_rs = rust.proc_macro(
> qemu_api_macros = declare_dependency(
> link_with: _qemu_api_macros_rs,
> )
> +
> +rust.test('rust-qemu-api-macros-tests', _qemu_api_macros_rs,
> + suite: ['unit', 'rust'])
> diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
> index 4b30bea9eafc7924bf593113c3f42c5b1010c4b9..6c6e9b683f047f79cb377e6d30e23490f66bd711 100644
> --- a/rust/qemu-api-macros/src/lib.rs
> +++ b/rust/qemu-api-macros/src/lib.rs
> @@ -15,6 +15,9 @@
> mod bits;
> use bits::BitsConstInternal;
>
> +#[cfg(test)]
> +mod tests;
> +
> fn get_fields<'a>(
> input: &'a DeriveInput,
> msg: &str,
> diff --git a/rust/qemu-api-macros/src/tests.rs b/rust/qemu-api-macros/src/tests.rs
> new file mode 100644
> index 0000000000000000000000000000000000000000..dfca7d4838f141783472a4e728312aebeb9b5a8b
> --- /dev/null
> +++ b/rust/qemu-api-macros/src/tests.rs
> @@ -0,0 +1,135 @@
> +// Copyright 2025, Linaro Limited
> +// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +use super::*;
> +use quote::quote;
> +
> +macro_rules! derive_compile_fail {
> + ($derive_fn:ident, $input:expr, $error_msg:expr) => {{
> + let input: proc_macro2::TokenStream = $input;
> + let error_msg: &str = $error_msg;
> + let derive_fn: fn(input: syn::DeriveInput) -> Result<proc_macro2::TokenStream, MacroError> =
> + $derive_fn;
> +
> + let input: syn::DeriveInput = syn::parse2(input).unwrap();
> + let result = derive_fn(input);
> + let MacroError::Message(err, _) = result.unwrap_err() else {
> + panic!()
> + };
> + assert_eq!(err, error_msg);
> + }};
> +}
> +
> +macro_rules! derive_compile {
> + ($derive_fn:ident, $input:expr, $($expected:tt)*) => {{
> + let input: proc_macro2::TokenStream = $input;
> + let expected: proc_macro2::TokenStream = $($expected)*;
> + let derive_fn: fn(input: syn::DeriveInput) -> Result<proc_macro2::TokenStream, MacroError> =
> + $derive_fn;
> +
> + let input: syn::DeriveInput = syn::parse2(input).unwrap();
> + let result = derive_fn(input).unwrap();
> + assert_eq!(result.to_string(), expected.to_string());
> + }};
> +}
Good examples to test macros. LGTM,
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
next prev parent reply other threads:[~2025-07-04 12:16 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-04 10:26 [PATCH 0/2] rust: add Derive macro unit tests Manos Pitsidianakis
2025-07-04 10:26 ` [PATCH 1/2] rust/qemu-api-macros: normalize TryInto output Manos Pitsidianakis
2025-07-04 12:16 ` Zhao Liu
2025-07-04 10:26 ` [PATCH 2/2] rust/qemu-api-macros: add unit tests Manos Pitsidianakis
2025-07-04 12:37 ` Zhao Liu [this message]
2025-07-04 12:35 ` [PATCH 0/2] rust: add Derive macro " Zhao Liu
2025-07-04 12:18 ` Manos Pitsidianakis
2025-07-08 9:59 ` Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aGfLBaW0p528ggCP@intel.com \
--to=zhao1.liu@intel.com \
--cc=alex.bennee@linaro.org \
--cc=manos.pitsidianakis@linaro.org \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).