use syn::parse_quote;
use crate::shared::utils::*;
fn check_struct_implements_copy(input: syn::DeriveInput) -> bool {
struct_implements_copy(&input)
}
#[test]
fn test_struct_implements_copy() {
if let Ok(mut cache) = COPY_IMPL_CACHE.lock() {
cache.clear();
}
let input: syn::DeriveInput = parse_quote! {
#[derive(Copy, Clone)]
struct EmptyStruct {}
};
assert!(
check_struct_implements_copy(input),
"EmptyStruct should implement Copy with #[derive(Copy)]"
);
let input: syn::DeriveInput = parse_quote! {
#[derive(Copy, Clone)]
struct SimpleStruct {
a: u8,
b: u16,
}
};
assert!(
check_struct_implements_copy(input),
"SimpleStruct should implement Copy with #[derive(Copy)]"
);
let input: syn::DeriveInput = parse_quote! {
#[derive(Clone)]
struct StructWithoutCopy {
a: u8,
b: u16,
}
};
assert!(
!check_struct_implements_copy(input),
"StructWithoutCopy should not implement Copy without #[derive(Copy)]"
);
let input: syn::DeriveInput = parse_quote! {
#[derive(Copy, Clone)]
struct StructWithVec {
a: u8,
b: Vec<u8>,
}
};
assert!(
check_struct_implements_copy(input),
"StructWithVec has #[derive(Copy)] so our function returns true"
);
let input: syn::DeriveInput = parse_quote! {
struct StructWithCopyFields {
a: u8,
b: u16,
c: i32,
d: bool,
}
};
assert!(
!check_struct_implements_copy(input),
"StructWithCopyFields should not implement Copy without #[derive(Copy)]"
);
let input: syn::DeriveInput = parse_quote! {
struct UnitStructWithoutCopy;
};
assert!(
!check_struct_implements_copy(input),
"UnitStructWithoutCopy should not implement Copy without #[derive(Copy)]"
);
let input: syn::DeriveInput = parse_quote! {
#[derive(Copy, Clone)]
struct UnitStructWithCopy;
};
assert!(
check_struct_implements_copy(input),
"UnitStructWithCopy should implement Copy with #[derive(Copy)]"
);
let input: syn::DeriveInput = parse_quote! {
#[derive(Copy, Clone)]
struct TupleStruct(u32, bool, char);
};
assert!(
check_struct_implements_copy(input),
"TupleStruct should implement Copy with #[derive(Copy)]"
);
let input: syn::DeriveInput = parse_quote! {
#[derive(Debug, PartialEq, Copy, Clone)]
struct MultipleDerivesStruct {
a: u8,
}
};
assert!(
check_struct_implements_copy(input),
"MultipleDerivesStruct should implement Copy with #[derive(Copy)]"
);
}