aboutsummaryrefslogtreecommitdiff
path: root/macros/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-11-11 14:05:51 +0100
committerHampusM <hampus@hampusmat.com>2022-11-11 14:05:51 +0100
commitafd19c6f07354e1d30fd9b765fac798fc68b018e (patch)
treecca0d9fbea51af21252c5cf5d674d1d6b9ace895 /macros/src
parent9ed051d56ef14290e532be2716b6f4fe6924b935 (diff)
test: add unit tests for parsing factory macro args
Diffstat (limited to 'macros/src')
-rw-r--r--macros/src/factory/macro_args.rs88
1 files changed, 87 insertions, 1 deletions
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::<Vec<_>>();
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<dyn Error>>
+ {
+ let input_args = quote! {
+ async = true
+ };
+
+ let factory_macro_args = parse2::<FactoryMacroArgs>(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<dyn Error>>
+ {
+ let input_args = quote! {
+ async = true, threadsafe = false
+ };
+
+ let factory_macro_args = parse2::<FactoryMacroArgs>(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::<FactoryMacroArgs>(input_args), Err(_)));
+ }
+
+ #[test]
+ fn cannot_parse_with_duplicate_flag()
+ {
+ assert!(matches!(
+ parse2::<FactoryMacroArgs>(quote! {
+ async = true, threadsafe = false, async = true
+ }),
+ Err(_)
+ ));
+
+ assert!(matches!(
+ parse2::<FactoryMacroArgs>(quote! {
+ async = true, threadsafe = false, async = false
+ }),
+ Err(_)
+ ));
+ }
+}