aboutsummaryrefslogtreecommitdiff
path: root/macros/src/injectable
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-11-11 21:04:21 +0100
committerHampusM <hampus@hampusmat.com>2022-11-11 21:04:21 +0100
commitde5aca66532272a84aeb04cac02bd25bb7afc414 (patch)
tree772ed62101967de39a1a9d5b2c67b5482e330c5f /macros/src/injectable
parentd9d649f8578c616b0672d807ddbbeb09ba2d88ea (diff)
test: add unit tests for parsing injectable macro args
Diffstat (limited to 'macros/src/injectable')
-rw-r--r--macros/src/injectable/macro_args.rs141
1 files changed, 141 insertions, 0 deletions
diff --git a/macros/src/injectable/macro_args.rs b/macros/src/injectable/macro_args.rs
index 0692b99..d730e0d 100644
--- a/macros/src/injectable/macro_args.rs
+++ b/macros/src/injectable/macro_args.rs
@@ -74,3 +74,144 @@ impl Parse for InjectableMacroArgs
Ok(Self { interface, flags })
}
}
+
+#[cfg(test)]
+mod tests
+{
+ use std::error::Error;
+
+ use proc_macro2::Span;
+ use quote::{format_ident, quote};
+ use syn::{parse2, LitBool};
+
+ use super::*;
+ use crate::test_utils;
+
+ #[test]
+ fn can_parse_with_only_interface() -> Result<(), Box<dyn Error>>
+ {
+ let input_args = quote! {
+ IFoo
+ };
+
+ let injectable_macro_args = parse2::<InjectableMacroArgs>(input_args)?;
+
+ assert!(matches!(injectable_macro_args.interface, Some(interface)
+ if interface == TypePath {
+ qself: None,
+ path: test_utils::create_path(&[
+ test_utils::create_path_segment(format_ident!("IFoo"), &[])
+ ])
+ }
+ ));
+
+ assert!(injectable_macro_args.flags.is_empty());
+
+ Ok(())
+ }
+
+ #[test]
+ fn can_parse_with_nothing() -> Result<(), Box<dyn Error>>
+ {
+ let input_args = quote! {};
+
+ let injectable_macro_args = parse2::<InjectableMacroArgs>(input_args)?;
+
+ assert!(matches!(injectable_macro_args.interface, None));
+
+ assert!(injectable_macro_args.flags.is_empty());
+
+ Ok(())
+ }
+
+ #[test]
+ fn can_parse_with_interface_and_flags() -> Result<(), Box<dyn Error>>
+ {
+ let input_args = quote! {
+ IFoo, no_doc_hidden = true, async = false
+ };
+
+ let injectable_macro_args = parse2::<InjectableMacroArgs>(input_args)?;
+
+ assert!(matches!(injectable_macro_args.interface, Some(interface)
+ if interface == TypePath {
+ qself: None,
+ path: test_utils::create_path(&[
+ test_utils::create_path_segment(format_ident!("IFoo"), &[])
+ ])
+ }
+ ));
+
+ assert_eq!(
+ injectable_macro_args.flags,
+ Punctuated::from_iter(vec![
+ MacroFlag {
+ flag: format_ident!("no_doc_hidden"),
+ is_on: LitBool::new(true, Span::call_site())
+ },
+ MacroFlag {
+ flag: format_ident!("async"),
+ is_on: LitBool::new(false, Span::call_site())
+ }
+ ])
+ );
+
+ Ok(())
+ }
+
+ #[test]
+ fn can_parse_with_flags_only() -> Result<(), Box<dyn Error>>
+ {
+ let input_args = quote! {
+ async = false, no_declare_concrete_interface = false
+ };
+
+ let injectable_macro_args = parse2::<InjectableMacroArgs>(input_args)?;
+
+ assert!(matches!(injectable_macro_args.interface, None));
+
+ assert_eq!(
+ injectable_macro_args.flags,
+ Punctuated::from_iter(vec![
+ MacroFlag {
+ flag: format_ident!("async"),
+ is_on: LitBool::new(false, Span::call_site())
+ },
+ MacroFlag {
+ flag: format_ident!("no_declare_concrete_interface"),
+ is_on: LitBool::new(false, Span::call_site())
+ }
+ ])
+ );
+
+ Ok(())
+ }
+
+ #[test]
+ fn cannot_parse_with_invalid_flag()
+ {
+ let input_args = quote! {
+ IFoo, haha = true, async = false
+ };
+
+ assert!(matches!(parse2::<InjectableMacroArgs>(input_args), Err(_)));
+ }
+
+ #[test]
+ fn cannot_parse_with_duplicate_flag()
+ {
+ assert!(matches!(
+ parse2::<InjectableMacroArgs>(quote! {
+ IFoo, async = false, no_doc_hidden = true, async = false
+ }),
+ Err(_)
+ ));
+
+ assert!(matches!(
+ parse2::<InjectableMacroArgs>(quote! {
+ IFoo, async = true , no_doc_hidden = true, async = false
+ }),
+ Err(_)
+ ));
+ }
+}