pub mod rust_value;
pub mod scale_value;
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use scale_info::{PortableRegistry, TypeInfo};
use crate::{scale_value, scale_value_from_seed};
fn make_type<T: TypeInfo + 'static>() -> (u32, PortableRegistry) {
let mut registry = scale_info::Registry::new();
let m = scale_info::MetaType::new::<T>();
let ty = registry.register_type(&m);
(ty.id, registry.into())
}
#[test]
fn recursion_does_not_cause_stack_overflow() {
#[allow(unused)]
#[derive(TypeInfo)]
struct Human {
name: String,
mom: Box<Human>,
dad: Box<Human>,
}
let (id, types) = make_type::<Human>();
assert!(scale_value(id, &types).is_err());
}
#[test]
fn seeding_works() {
#[allow(unused)]
#[derive(TypeInfo)]
struct Human {
name: String,
age: u32,
male: bool,
eye_color: Color,
}
#[allow(unused)]
#[derive(TypeInfo)]
enum Color {
Black,
White,
Green(i32),
}
let (id, types) = make_type::<Human>();
let a1 = scale_value_from_seed(id, &types, 20).unwrap();
let a2 = scale_value_from_seed(id, &types, 20).unwrap();
let a3 = scale_value_from_seed(id, &types, 20).unwrap();
let b1 = scale_value_from_seed(id, &types, 30).unwrap();
let b2 = scale_value_from_seed(id, &types, 30).unwrap();
assert_eq!(a1, a2);
assert_eq!(a1, a3);
assert_eq!(b1, b2);
assert_ne!(a1, b1);
}
}