-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f3af3b
commit ce60070
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,70 @@ | ||
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()) | ||
} | ||
|
||
/// for certain types we cannot create any valid scale_value type examples because they are infinitely nested. We need to return an error in those cases. | ||
/// Otherwise we would get a stack overflow and the program crashes... | ||
#[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>(); | ||
// Make sure recursion does not panic. An error should be yielded instead. | ||
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(); | ||
|
||
// The examples can be checked manually by comparing them side by side: | ||
// println!("{b2}\n{a1}"); | ||
|
||
assert_eq!(a1, a2); | ||
assert_eq!(a1, a3); | ||
assert_eq!(b1, b2); | ||
assert_ne!(a1, b1); | ||
} | ||
} |