#![allow(unused_parens)] use functor_derive::Functor; use std::any::{Any, TypeId}; use std::collections::{BTreeMap, HashMap}; // recursion, // mutual recursion, #[test] fn struct_simple() { #[derive(Functor)] struct StructSimple { field_1: A, field_2: u32, } let x = StructSimple:: { field_1: 42, field_2: 13, }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn struct_chained() { #[derive(Functor)] struct StructChained { field_1: Option, field_2: Option, field_3: u32, } let x = StructChained:: { field_1: Some(42), field_2: None, field_3: 13, }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn struct_tuple() { #[derive(Functor)] struct StructTuple { field_1: (A, u8, A), field_2: u32, } let x = StructTuple:: { field_1: (3, 5, 8), field_2: 13, }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn struct_hashmap() { #[derive(Functor)] struct StructHashMap { field_1: A, field_2: HashMap, } let x = StructHashMap:: { field_1: 42, field_2: HashMap::from([(13, 255)]), }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn struct_btreemap() { #[derive(Functor)] struct StructBTreeMap { field_1: A, field_2: BTreeMap, } let x = StructBTreeMap:: { field_1: 42, field_2: BTreeMap::from([(13, 255)]), }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn struct_array() { #[derive(Functor)] struct StructArray { field_1: [A; 3], field_2: u8, } let x = StructArray:: { field_1: [1, 2, 3], field_2: 42, }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn struct_paren() { #[derive(Functor)] struct StructParen { field_1: (A), field_2: u8, } let x = StructParen:: { field_1: 1, field_2: 42, }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn enum_tuple() { #[derive(Functor)] enum EnumTuple { Var1(A), Var2(bool), Var3, } let x = EnumTuple::Var1(18usize); assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn enum_struct() { #[derive(Functor)] enum EnumStruct { Var1 { x: A }, Var2 { y: bool }, Var3, } let x = EnumStruct::Var1 { x: 18usize }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn enum_mixed() { #[derive(Functor)] enum EnumMixed { Var1 { x: A }, Var2(bool), Var3, } let x = EnumMixed::Var1 { x: 18usize }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn generic_overload() { #[derive(Functor)] struct StructLifeTimes<'a, 'b, const N: usize, A, T> { field_1: A, field_2: &'a u32, field_3: &'b bool, field_4: T, field_5: [A; N], } let x = StructLifeTimes::<2, usize, bool> { field_1: 42, field_2: &13, field_3: &false, field_4: true, field_5: [1, 2], }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn indirect_generic() { #[derive(Functor)] struct IndirectGeneric { field_1: Option>, } let x = IndirectGeneric { field_1: Some(Some(18usize)), }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn indirect_tuple_generic() { #[derive(Functor)] struct IndirectTupleGeneric { field_1: Vec, Vec>, usize)>>, } let x = IndirectTupleGeneric:: { field_1: vec![vec![(18, vec![18], vec![vec![42]], 9)]], }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn chained_fmap() { #[derive(Functor)] struct StructSimple { field_1: ApplePie, } // This incredible struct name was brought to you by Jonathan :)! #[derive(Functor)] struct ApplePie { apple: A, pie: B, } let x = StructSimple:: { field_1: ApplePie { apple: 15, pie: 17 }, }; assert_eq!( x.fmap(|x| x as u64).type_id(), TypeId::of::>() ); } #[test] fn explicit_path() { #[allow(unused)] #[derive(Functor)] struct Test(core::option::Option); }