aboutsummaryrefslogtreecommitdiff
path: root/macros/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-11-11 16:06:51 +0100
committerHampusM <hampus@hampusmat.com>2022-11-11 16:06:51 +0100
commit8036395385c32e8b2e143ad671ca55ab104a5df0 (patch)
tree47a39f4c1b1abe54908ddf5cc948ebdcad6c12f3 /macros/src
parent65ddefdb61eb0bcdf36571cc34c8a1ae56b57d5f (diff)
test: add unit test for parsing factory type aliases
Diffstat (limited to 'macros/src')
-rw-r--r--macros/src/factory/type_alias.rs53
1 files 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<Self>
{
- let type_alias = match input.parse::<ItemType>() {
- Ok(type_alias) => Ok(type_alias),
- Err(_) => Err(input.error("Expected a type alias")),
- }?;
+ let type_alias = input
+ .parse::<ItemType>()
+ .map_err(|_| input.error("Expected a type alias"))?;
let aliased_fn_trait =
- parse::<FnTrait>(type_alias.ty.as_ref().to_token_stream().into())?;
+ parse2::<FnTrait>(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<dyn Error>>
+ {
+ let input_args = quote! {
+ type FooFactory = dyn Fn(String, &u32) -> Foo;
+ };
+
+ let factory_type_alias = parse2::<FactoryTypeAlias>(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(())
+ }
+}