diff options
author | HampusM <hampus@hampusmat.com> | 2022-10-15 18:40:20 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-10-15 18:40:20 +0200 |
commit | 740ef47d49e02ae2f2184f4c347d8eba8aee38fd (patch) | |
tree | 4a9cbf8f067b6e99f1f95dab774216d804829051 /macros/src/test_utils.rs | |
parent | fdbd28fc18a5d2019132413b6699ff7691968fc2 (diff) |
refactor: improve internals of macros & add unit tests
Diffstat (limited to 'macros/src/test_utils.rs')
-rw-r--r-- | macros/src/test_utils.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/macros/src/test_utils.rs b/macros/src/test_utils.rs new file mode 100644 index 0000000..471dc74 --- /dev/null +++ b/macros/src/test_utils.rs @@ -0,0 +1,99 @@ +use proc_macro2::{Ident, TokenStream}; +use syn::punctuated::Punctuated; +use syn::token::{Colon, Gt, Lt, Paren, RArrow}; +use syn::{ + AngleBracketedGenericArguments, + Attribute, + FnArg, + GenericArgument, + GenericParam, + Generics, + Pat, + PatType, + Path, + PathArguments, + PathSegment, + Signature, + Type, + TypePath, +}; + +pub fn create_path(segments: &[PathSegment]) -> Path +{ + Path { + leading_colon: None, + segments: segments.iter().cloned().collect(), + } +} + +pub fn create_path_segment(ident: Ident, generic_arg_types: &[Type]) -> PathSegment +{ + PathSegment { + ident, + arguments: if generic_arg_types.is_empty() { + PathArguments::None + } else { + PathArguments::AngleBracketed(AngleBracketedGenericArguments { + colon2_token: None, + lt_token: Lt::default(), + args: generic_arg_types + .iter() + .map(|generic_arg_type| { + GenericArgument::Type(generic_arg_type.clone()) + }) + .collect(), + gt_token: Gt::default(), + }) + }, + } +} + +pub fn create_type(path: Path) -> Type +{ + Type::Path(TypePath { qself: None, path }) +} + +pub fn create_generics<Params>(params: Params) -> Generics +where + Params: IntoIterator<Item = GenericParam>, +{ + Generics { + lt_token: None, + params: Punctuated::from_iter(params), + gt_token: None, + where_clause: None, + } +} + +pub fn create_signature<ArgTypes>( + ident: Ident, + arg_types: ArgTypes, + return_type: Type, +) -> Signature +where + ArgTypes: IntoIterator<Item = (Type, Vec<Attribute>)>, +{ + Signature { + constness: None, + asyncness: None, + unsafety: None, + abi: None, + fn_token: syn::token::Fn::default(), + ident, + generics: create_generics(vec![]), + paren_token: Paren::default(), + inputs: arg_types + .into_iter() + .map(|(arg_type, attrs)| { + FnArg::Typed(PatType { + attrs, + pat: Box::new(Pat::Verbatim(TokenStream::new())), + colon_token: Colon::default(), + ty: Box::new(arg_type), + }) + }) + .collect(), + variadic: None, + output: syn::ReturnType::Type(RArrow::default(), Box::new(return_type)), + } +} |