From de5aca66532272a84aeb04cac02bd25bb7afc414 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 11 Nov 2022 21:04:21 +0100 Subject: test: add unit tests for parsing injectable macro args --- macros/src/injectable/macro_args.rs | 141 ++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) (limited to 'macros/src') 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> + { + let input_args = quote! { + IFoo + }; + + let injectable_macro_args = parse2::(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> + { + let input_args = quote! {}; + + let injectable_macro_args = parse2::(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> + { + let input_args = quote! { + IFoo, no_doc_hidden = true, async = false + }; + + let injectable_macro_args = parse2::(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> + { + let input_args = quote! { + async = false, no_declare_concrete_interface = false + }; + + let injectable_macro_args = parse2::(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::(input_args), Err(_))); + } + + #[test] + fn cannot_parse_with_duplicate_flag() + { + assert!(matches!( + parse2::(quote! { + IFoo, async = false, no_doc_hidden = true, async = false + }), + Err(_) + )); + + assert!(matches!( + parse2::(quote! { + IFoo, async = true , no_doc_hidden = true, async = false + }), + Err(_) + )); + } +} -- cgit v1.2.3-18-g5258