From afd19c6f07354e1d30fd9b765fac798fc68b018e Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 11 Nov 2022 14:05:51 +0100 Subject: test: add unit tests for parsing factory macro args --- macros/src/factory/macro_args.rs | 88 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) (limited to 'macros/src/factory') diff --git a/macros/src/factory/macro_args.rs b/macros/src/factory/macro_args.rs index dd80c1c..bd09cdf 100644 --- a/macros/src/factory/macro_args.rs +++ b/macros/src/factory/macro_args.rs @@ -36,9 +36,95 @@ impl Parse for FactoryMacroArgs .collect::>(); if let Some(dupe_flag_name) = flag_names.iter().find_duplicate() { - return Err(input.error(format!("Duplicate flag '{}'", dupe_flag_name))); + return Err(input.error(format!("Duplicate flag '{dupe_flag_name}'"))); } Ok(Self { flags }) } } + +#[cfg(test)] +mod tests +{ + use std::error::Error; + + use proc_macro2::Span; + use quote::{format_ident, quote}; + use syn::{parse2, LitBool}; + + use super::*; + + #[test] + fn can_parse_with_single_flag() -> Result<(), Box> + { + let input_args = quote! { + async = true + }; + + let factory_macro_args = parse2::(input_args)?; + + assert_eq!( + factory_macro_args.flags, + Punctuated::from_iter(vec![MacroFlag { + flag: format_ident!("async"), + is_on: LitBool::new(true, Span::call_site()) + }]) + ); + + Ok(()) + } + + #[test] + fn can_parse_with_multiple_flags() -> Result<(), Box> + { + let input_args = quote! { + async = true, threadsafe = false + }; + + let factory_macro_args = parse2::(input_args)?; + + assert_eq!( + factory_macro_args.flags, + Punctuated::from_iter(vec![ + MacroFlag { + flag: format_ident!("async"), + is_on: LitBool::new(true, Span::call_site()) + }, + MacroFlag { + flag: format_ident!("threadsafe"), + is_on: LitBool::new(false, Span::call_site()) + } + ]) + ); + + Ok(()) + } + + #[test] + fn cannot_parse_with_invalid_flag() + { + let input_args = quote! { + async = true, threadsafe = false, foo = true + }; + + assert!(matches!(parse2::(input_args), Err(_))); + } + + #[test] + fn cannot_parse_with_duplicate_flag() + { + assert!(matches!( + parse2::(quote! { + async = true, threadsafe = false, async = true + }), + Err(_) + )); + + assert!(matches!( + parse2::(quote! { + async = true, threadsafe = false, async = false + }), + Err(_) + )); + } +} -- cgit v1.2.3-18-g5258