use crate::common_macro::schema_imports::*; use alloc::{boxed::Box, collections::BTreeMap, format, string::ToString, vec::Vec}; #[track_caller] fn test_ok() { let schema = BorshSchemaContainer::for_type::(); assert_eq!(Ok(()), schema.validate()); } #[track_caller] fn test_err(err: SchemaContainerValidateError) { let schema = BorshSchemaContainer::for_type::(); assert_eq!(Err(err), schema.validate()); } #[test] fn validate_for_derived_types() { #[derive(BorshSchema)] pub struct Empty; #[derive(BorshSchema)] pub struct Named { _foo: usize, _bar: [u8; 15], } #[derive(BorshSchema)] #[allow(unused)] pub struct Unnamed(usize, [u8; 15]); #[derive(BorshSchema)] #[allow(unused)] struct Recursive(Option>); #[derive(BorshSchema)] #[allow(unused)] struct RecursiveSequence(Vec); // thankfully, this one cannot be constructed #[derive(BorshSchema)] #[allow(unused)] struct RecursiveArray(Box<[RecursiveArray; 3]>); test_ok::(); test_ok::(); test_ok::(); test_ok::(); test_ok::(); test_ok::(); test_ok::(); test_ok::<[(); 300]>(); } #[test] fn validate_for_zst_sequences() { test_err::>>(SchemaContainerValidateError::ZSTSequence( "Vec<()>".to_string(), )); test_err::>(SchemaContainerValidateError::ZSTSequence( "Vec".to_string(), )); } #[test] fn validate_bound_vec() { #[allow(dead_code)] struct BoundVec; impl BorshSchema for BoundVec { fn declaration() -> Declaration { format!("BoundVec<{}, {}>", W, N) } fn add_definitions_recursively(definitions: &mut BTreeMap) { let definition = Definition::Sequence { length_width: W, length_range: 0..=N, elements: "u8".to_string(), }; add_definition(Self::declaration(), definition, definitions); u8::add_definitions_recursively(definitions); } } test_ok::>(); test_err::>(SchemaContainerValidateError::TagTooNarrow( "BoundVec<1, 65535>".to_string(), )); test_ok::>(); test_ok::>(); }