use crate::{parse_custom_list_path, parse_custom_type_path, CodeGenError, RepeatedRepr};
use quote::quote;
#[test]
fn substitutes_element_into_template() {
let got = parse_custom_list_path("::my_crate::SmallList<*>", "e! { u32 }).unwrap();
assert_eq!(
got.to_string(),
quote! { ::my_crate::SmallList<u32> }.to_string()
);
}
#[test]
fn substitutes_into_array_wrapped_generic() {
let got = parse_custom_list_path("::smallvec::SmallVec<[*; 4]>", "e! { i64 }).unwrap();
assert_eq!(
got.to_string(),
quote! { ::smallvec::SmallVec<[i64; 4]> }.to_string()
);
}
#[test]
fn substitutes_nested_generic_element() {
let elem = quote! { ::buffa::EnumValue<my::Color> };
let got = parse_custom_list_path("::my_crate::List<*>", &elem).unwrap();
assert_eq!(
got.to_string(),
quote! { ::my_crate::List<::buffa::EnumValue<my::Color> > }.to_string()
);
}
#[test]
fn missing_placeholder_is_a_distinct_error() {
let err = parse_custom_list_path("::smallvec::SmallVec<u32>", "e! { u32 }).unwrap_err();
assert!(matches!(err, CodeGenError::MissingListPlaceholder(_)));
}
#[test]
fn unparseable_substitution_is_invalid_type_path() {
let err = parse_custom_list_path("List<*", "e! { u32 }).unwrap_err();
assert!(matches!(err, CodeGenError::InvalidTypePath(_)));
}
#[test]
fn non_path_collection_is_invalid_type_path() {
for template in [
"[*; 4]",
"(*,)",
"&'static [*]",
"impl Iterator<Item = *>",
"([*; 4])",
] {
let err = parse_custom_list_path(template, "e! { u32 }).unwrap_err();
assert!(
matches!(&err, CodeGenError::InvalidTypePath(msg) if msg.contains(template)),
"{template}: {err:?}"
);
}
}
#[test]
fn custom_type_path_accepts_paths_including_projections_and_parentheses() {
for (path, expected) in [
("crate::SecretBytes", "crate::SecretBytes"),
("::smol_str::SmolStr", "::smol_str::SmolStr"),
("Local", "Local"),
("crate::Wrap<u8>", "crate::Wrap<u8>"),
("<crate::A as crate::B>::C", "<crate::A as crate::B>::C"),
("(crate::SecretBytes)", "crate::SecretBytes"),
("((<crate::A as crate::B>::C))", "<crate::A as crate::B>::C"),
("crate::Wrap::<(u8)>", "crate::Wrap<u8>"),
("crate::r#type", "crate::r#type"),
(
"crate::Wrap<&'static (dyn core::fmt::Debug + Sync)>",
"crate::Wrap<&'static (dyn core::fmt::Debug + Sync)>",
),
(
"crate::Wrap<{crate::size::<u8>()}>",
"crate::Wrap<{crate::size::<u8>()}>",
),
] {
let got = parse_custom_type_path(path).unwrap();
let expected: syn::Type = syn::parse_str(expected).unwrap();
assert_eq!(got.to_string(), quote! { #expected }.to_string());
}
}
#[test]
fn custom_type_path_rejects_types_that_cannot_be_a_field_representation() {
for path in [
"_",
"()",
"[u8; 4]",
"&'static [u8]",
"impl AsRef<[u8]>",
"dyn AsRef<[u8]>",
"fn() -> u8",
"(())",
"([u8; 4])",
"crate::",
] {
let err = parse_custom_type_path(path).unwrap_err();
assert!(
matches!(&err, CodeGenError::InvalidTypePath(msg) if msg.contains(path)),
"{path}: {err:?}"
);
}
}
#[test]
fn custom_list_path_accepts_projections_and_parentheses() {
for template in [
"<crate::A as crate::B>::List<*>",
"<* as crate::B>::List",
"(crate::List<*>)",
"((<crate::A as crate::B>::List<*>))",
] {
let got = parse_custom_list_path(template, "e! { u32 }).unwrap();
let expected = crate::context::normalized_rust_type(&template.replace('*', "u32")).unwrap();
assert_eq!(got.to_string(), quote! { #expected }.to_string());
}
}
#[test]
fn vec_repr_is_default_custom_is_not() {
assert!(RepeatedRepr::default().is_default());
assert!(RepeatedRepr::Vec.is_default());
assert!(!RepeatedRepr::Custom("::x::L<*>".to_string()).is_default());
}