From 8036395385c32e8b2e143ad671ca55ab104a5df0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 11 Nov 2022 16:06:51 +0100 Subject: test: add unit test for parsing factory type aliases --- macros/src/factory/type_alias.rs | 53 +++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/macros/src/factory/type_alias.rs b/macros/src/factory/type_alias.rs index 64afe57..9ec2b3a 100644 --- a/macros/src/factory/type_alias.rs +++ b/macros/src/factory/type_alias.rs @@ -1,7 +1,7 @@ use quote::ToTokens; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; -use syn::{parse, ItemType, Token, Type}; +use syn::{parse2, ItemType, Token, Type}; use crate::fn_trait::FnTrait; @@ -17,13 +17,12 @@ impl Parse for FactoryTypeAlias { fn parse(input: ParseStream) -> syn::Result { - let type_alias = match input.parse::() { - Ok(type_alias) => Ok(type_alias), - Err(_) => Err(input.error("Expected a type alias")), - }?; + let type_alias = input + .parse::() + .map_err(|_| input.error("Expected a type alias"))?; let aliased_fn_trait = - parse::(type_alias.ty.as_ref().to_token_stream().into())?; + parse2::(type_alias.ty.as_ref().to_token_stream())?; Ok(Self { type_alias, @@ -33,3 +32,45 @@ impl Parse for FactoryTypeAlias }) } } + +#[cfg(test)] +mod tests +{ + use std::error::Error; + + use quote::{format_ident, quote}; + use syn::token::And; + use syn::{Path, PathSegment, TypePath, TypeReference}; + + use super::*; + use crate::test_utils; + + #[test] + fn can_parse() -> Result<(), Box> + { + let input_args = quote! { + type FooFactory = dyn Fn(String, &u32) -> Foo; + }; + + let factory_type_alias = parse2::(input_args)?; + + assert_eq!( + factory_type_alias.arg_types, + Punctuated::from_iter(vec![ + test_utils::create_type(test_utils::create_path(&[ + test_utils::create_path_segment(format_ident!("String"), &[]) + ])), + Type::Reference(TypeReference { + and_token: And::default(), + lifetime: None, + mutability: None, + elem: Box::new(test_utils::create_type(test_utils::create_path(&[ + test_utils::create_path_segment(format_ident!("u32"), &[]) + ]))) + }) + ]) + ); + + Ok(()) + } +} -- cgit v1.2.3-18-g5258